@@ -28,13 +28,14 @@ const MaxStackingBytes = 32 * 1024 // 32KB
2828// list-buffer if the data size of ring-buffer reaches the maximum(MaxStackingBytes), list-buffer is more
2929// flexible and scalable, which helps the application reduce memory footprint.
3030type Buffer struct {
31- ringBuffer * ringbuffer.RingBuffer
32- listBuffer listbuffer.ListBuffer
31+ maxStackingBytes int
32+ ringBuffer * ringbuffer.RingBuffer
33+ listBuffer listbuffer.ListBuffer
3334}
3435
3536// New instantiates a mixedbuffer.Buffer and returns it.
36- func New () * Buffer {
37- return & Buffer {ringBuffer : rbPool .Get ()}
37+ func New (maxTopBufCap int ) * Buffer {
38+ return & Buffer {maxStackingBytes : maxTopBufCap , ringBuffer : rbPool .Get ()}
3839}
3940
4041// Peek returns all bytes as [][]byte, these bytes won't be discarded until Buffer.Discard() is called.
@@ -55,18 +56,22 @@ func (mb *Buffer) Discard(n int) {
5556}
5657
5758// Write appends data to this buffer.
58- func (mb * Buffer ) Write (p []byte ) (int , error ) {
59- if ! mb .listBuffer .IsEmpty () || mb .ringBuffer .Length () >= MaxStackingBytes {
59+ func (mb * Buffer ) Write (p []byte ) (n int , err error ) {
60+ if ! mb .listBuffer .IsEmpty () || mb .ringBuffer .Length () >= mb . maxStackingBytes {
6061 mb .listBuffer .PushBytesBack (p )
6162 return len (p ), nil
6263 }
63- return mb .ringBuffer .Write (p )
64+ n , err = mb .ringBuffer .Write (p )
65+ if n > mb .maxStackingBytes {
66+ mb .maxStackingBytes = n
67+ }
68+ return
6469}
6570
6671// Writev appends multiple byte slices to this buffer.
6772func (mb * Buffer ) Writev (bs [][]byte ) (int , error ) {
6873 var n int
69- if ! mb .listBuffer .IsEmpty () || mb .ringBuffer .Length () >= MaxStackingBytes {
74+ if ! mb .listBuffer .IsEmpty () || mb .ringBuffer .Length () >= mb . maxStackingBytes {
7075 for _ , b := range bs {
7176 mb .listBuffer .PushBytesBack (b )
7277 n += len (b )
@@ -77,6 +82,9 @@ func (mb *Buffer) Writev(bs [][]byte) (int, error) {
7782 _ , _ = mb .ringBuffer .Write (b )
7883 n += len (b )
7984 }
85+ if n > mb .maxStackingBytes {
86+ mb .maxStackingBytes = n
87+ }
8088 return n , nil
8189}
8290
0 commit comments