1
0
buffer/pool.go

45 lines
589 B
Go
Raw Normal View History

2026-02-17 16:58:00 +01:00
package buffer
2026-03-14 23:29:29 +01:00
import (
"errors"
"sync"
)
2026-02-22 18:45:57 +01:00
2026-02-22 16:30:37 +01:00
type noPool struct {
2026-02-17 16:58:00 +01:00
allocSize int
}
2026-02-22 18:45:57 +01:00
type pool struct {
sp *sync.Pool
}
2026-02-17 16:58:00 +01:00
2026-02-22 18:45:57 +01:00
func newPool(allocSize int) *pool {
sp := &sync.Pool{
New: func() any {
return make([]byte, allocSize)
},
}
return &pool{sp: sp}
2026-02-17 16:58:00 +01:00
}
func (p noPool) Get() ([]byte, error) {
return make([]byte, p.allocSize), nil
}
func (noPool) Put([]byte) {
}
func (p *pool) Get() ([]byte, error) {
2026-03-14 23:29:29 +01:00
b, ok := p.sp.Get().([]byte)
if !ok {
return nil, errors.New("invalid resource received from pool")
}
return b, nil
2026-02-17 16:58:00 +01:00
}
func (p *pool) Put(b []byte) {
2026-02-22 18:45:57 +01:00
p.sp.Put(b)
2026-02-17 16:58:00 +01:00
}