Skip to content

Instantly share code, notes, and snippets.

@stvoidit
Created August 10, 2022 10:30

Revisions

  1. stvoidit created this gist Aug 10, 2022.
    21 changes: 21 additions & 0 deletions BufferSyncPool.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    func NewBufferPool(bufsize int) BufferPool {
    if bufsize <= 0 {
    bufsize = bytes.MinRead
    }
    return BufferPool{pool: &sync.Pool{New: func() any {
    return bytes.NewBuffer(make([]byte, 0, bufsize))
    }}}
    }

    type BufferPool struct {
    pool *sync.Pool
    }

    func (bp BufferPool) Get() *bytes.Buffer {
    return bp.pool.Get().(*bytes.Buffer)
    }

    func (bp BufferPool) Put(b *bytes.Buffer) {
    b.Reset()
    bp.pool.Put(b)
    }