package utl import ( "sync" ) var ( DefBufPool *BufPool ) func init() { DefBufPool = NewBufPool(uint32(1024 * 2)) } // Pool of buffer type BufPool struct { p *sync.Pool size uint32 } // Create a pool of buffers. // the size of each buffer is specified by `size` func NewBufPool(size uint32) *BufPool { return &BufPool{ p: &sync.Pool{ New: func() interface{} { return make([]byte, size) }, }, size: size, } } // Get a sized buffer from pool func (self *BufPool) Get() []byte { return self.p.Get().([]byte) } // Get default buffer size of the pool. which is passed by NewBufPool() func (self *BufPool) GetSize() uint32 { return self.size } // Return the buffer retrieve by Get() func (self *BufPool) Put(data []byte) { self.p.Put(data) }