Files
mochi/pkg/bytepool/bufferpool.go
Lawrence, Rendall a9d1642615 (wip) sanitize and refactor code
* replace bencode calls in http response with static generated values
* move bytepool to shared folder
* change receivers for `Scrapes` and `RequestAddresses` (bored of compile warnings)
2022-10-24 19:07:47 +03:00

28 lines
580 B
Go

package bytepool
import (
"bytes"
"sync"
)
// ByteBufferPool is a cached pool of reusable byte buffers.
type ByteBufferPool struct {
sync.Pool
}
// NewBufferPool allocates a new ByteBufferPool.
func NewBufferPool() *ByteBufferPool {
return &ByteBufferPool{sync.Pool{New: func() any { return new(bytes.Buffer) }}}
}
// Get returns a bytes.Buffer from the pool.
func (bbp *ByteBufferPool) Get() *bytes.Buffer {
return bbp.Pool.Get().(*bytes.Buffer)
}
// Put returns bytes.Buffer to the pool.
func (bbp *ByteBufferPool) Put(b *bytes.Buffer) {
b.Reset()
bbp.Pool.Put(b)
}