amuz.es/src/infra/goutils@v0.1.3/buf/atomic_buf_pool.go (about)

     1  package buf
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  // atomicBuffer pool to reduce GC
     8  var atoBuffers = sync.Pool{
     9  	// New is called when a new instance is needed
    10  	New: func() interface{} {
    11  		return new(atomicBuffer)
    12  	},
    13  }
    14  
    15  // GetBuffer fetches a atomicBuffer from the pool
    16  func GetAtoBuffer() AtomicBuffers {
    17  	return atoBuffers.Get().(AtomicBuffers)
    18  }
    19  
    20  // PutBuffer returns a atomicBuffer to the pool
    21  func PutAtoBuffer(buf AtomicBuffers) {
    22  	bufPtr, ok := buf.(*atomicBuffer)
    23  	if !ok {
    24  		return
    25  	}
    26  	bufPtr.Reset()
    27  	atoBuffers.Put(bufPtr)
    28  }