github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/pkg/bufferpool/bufferpool.go (about)

     1  package bufferpool
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  // BufferPool implements the interface of httputil.BufferPool in order
     8  // to improve memory utilization in the reverse proxy.
     9  type BufferPool struct {
    10  	s sync.Pool
    11  }
    12  
    13  // NewPool creates a new BufferPool.
    14  func NewPool(size int) *BufferPool {
    15  	return &BufferPool{
    16  		s: sync.Pool{
    17  			New: func() interface{} {
    18  				return make([]byte, size)
    19  			},
    20  		},
    21  	}
    22  }
    23  
    24  // Get gets a buffer from the pool.
    25  func (b *BufferPool) Get() []byte {
    26  	return b.s.Get().([]byte)
    27  }
    28  
    29  // Put returns the buffer to the pool.
    30  func (b *BufferPool) Put(buf []byte) {
    31  	b.s.Put(buf) // nolint
    32  }