forked from things-go/go-socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
40 lines (34 loc) · 788 Bytes
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package bufferpool
import (
"sync"
)
// BufPool is an interface for getting and returning temporary
// byte slices for use by io.CopyBuffer.
type BufPool interface {
Get() []byte
Put([]byte)
}
type pool struct {
size int
pool *sync.Pool
}
// NewPool new buffer pool for getting and returning temporary
// byte slices for use by io.CopyBuffer.
func NewPool(size int) BufPool {
return &pool{
size,
&sync.Pool{
New: func() interface{} { return make([]byte, 0, size) }},
}
}
// Get implement interface BufPool
func (sf *pool) Get() []byte {
return sf.pool.Get().([]byte)
}
// Put implement interface BufPool
func (sf *pool) Put(b []byte) {
if cap(b) != sf.size {
panic("invalid buffer size that's put into leaky buffer")
}
sf.pool.Put(b[:0]) //nolint: staticcheck
}