1
0
buffer/pool.go
2026-03-14 23:29:29 +01:00

45 lines
589 B
Go

package buffer
import (
"errors"
"sync"
)
type noPool struct {
allocSize int
}
type pool struct {
sp *sync.Pool
}
func newPool(allocSize int) *pool {
sp := &sync.Pool{
New: func() any {
return make([]byte, allocSize)
},
}
return &pool{sp: sp}
}
func (p noPool) Get() ([]byte, error) {
return make([]byte, p.allocSize), nil
}
func (noPool) Put([]byte) {
}
func (p *pool) Get() ([]byte, error) {
b, ok := p.sp.Get().([]byte)
if !ok {
return nil, errors.New("invalid resource received from pool")
}
return b, nil
}
func (p *pool) Put(b []byte) {
p.sp.Put(b)
}