gopenfusion/cnet/protocol/pool.go

22 lines
350 B
Go

package protocol
import (
"bytes"
"sync"
)
var allocator = &sync.Pool{
New: func() any { return new(bytes.Buffer) },
}
// grabs a *bytes.Buffer from the pool
func GetBuffer() *bytes.Buffer {
return allocator.Get().(*bytes.Buffer)
}
// returns a *bytes.Buffer to the pool
func PutBuffer(buf *bytes.Buffer) {
buf.Reset()
allocator.Put(buf)
}