github.com/pachyderm/pachyderm@v1.13.4/src/client/pkg/grpcutil/buffer.go (about)

     1  package grpcutil
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  // BufPool is a wrapper around sync.Pool that makes it a little nicer to use
     8  // for []byte by doing the casting for you and defining the `New` function.
     9  type BufPool struct {
    10  	sync.Pool
    11  }
    12  
    13  // NewBufPool creates a new BufPool that returns buffers of the given size.
    14  func NewBufPool(size int) *BufPool {
    15  	return &BufPool{sync.Pool{
    16  		New: func() interface{} { return make([]byte, size) },
    17  	}}
    18  }
    19  
    20  // GetBuffer returns a buffer.  The buffer may or may not be freshly
    21  // allocated, and it may or may not be zero-ed.
    22  func (b *BufPool) GetBuffer() []byte {
    23  	return b.Get().([]byte)
    24  }
    25  
    26  // PutBuffer returns the buffer to the pool.
    27  func (b *BufPool) PutBuffer(buf []byte) {
    28  	b.Put(buf) //lint:ignore SA6002 []byte is sufficiently pointer-like for our purposes
    29  }
    30  
    31  // bufPool is a pool of buffers that are sized for grpc connections
    32  // This buffer size is:
    33  // 1. Reasonably smaller than the max gRPC size
    34  // 2. Small enough that having hundreds of these buffers won't
    35  // overwhelm the node
    36  // 3. Large enough for message-sending to be efficient
    37  var bufPool = NewBufPool(MaxMsgSize / 10)
    38  
    39  // GetBuffer returns a buffer.  The buffer may or may not be freshly
    40  // allocated, and it may or may not be zero-ed.
    41  func GetBuffer() []byte {
    42  	return bufPool.GetBuffer()
    43  }
    44  
    45  // PutBuffer returns the buffer to the pool.
    46  func PutBuffer(buf []byte) {
    47  	bufPool.PutBuffer(buf)
    48  }