-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.go
More file actions
66 lines (55 loc) · 1.72 KB
/
pool.go
File metadata and controls
66 lines (55 loc) · 1.72 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package fastwire
import "sync"
// DefaultMTU is the default maximum transmission unit size in bytes.
const DefaultMTU = 1200
var bufferPool = sync.Pool{
New: func() any {
buf := make([]byte, DefaultMTU)
return buf
},
}
// GetBuffer returns a []byte of length DefaultMTU from the buffer pool.
func GetBuffer() []byte {
return bufferPool.Get().([]byte)
}
// PutBuffer returns buf to the buffer pool.
func PutBuffer(buf []byte) {
//nolint:staticcheck // SA6002: we intentionally store []byte in sync.Pool
bufferPool.Put(buf)
}
// getSendBuffer returns a buffer of at least size bytes. Buffers that fit
// within DefaultMTU come from the shared pool; larger ones are heap-allocated.
func getSendBuffer(size int) []byte {
if size <= DefaultMTU {
return bufferPool.Get().([]byte)[:size]
}
return make([]byte, size)
}
// putSendBuffer returns a send buffer to the pool. Only buffers with
// cap >= DefaultMTU (i.e. those originally obtained from the pool) are recycled.
func putSendBuffer(buf []byte) {
if cap(buf) >= DefaultMTU {
//nolint:staticcheck // SA6002: we intentionally store []byte in sync.Pool
bufferPool.Put(buf[:cap(buf)])
}
}
// decryptPool provides reusable buffers for AEAD decrypt output.
var decryptPool = sync.Pool{
New: func() any {
buf := make([]byte, DefaultMTU)
return buf
},
}
func getDecryptBuffer() []byte {
return decryptPool.Get().([]byte)
}
func putDecryptBuffer(buf []byte) {
if cap(buf) >= DefaultMTU {
//nolint:staticcheck // SA6002: we intentionally store []byte in sync.Pool
decryptPool.Put(buf[:cap(buf)])
}
}
// newReadPool creates a sync.Pool for read buffers of the given size.
func newReadPool(size int) *sync.Pool {
return &sync.Pool{New: func() any { return make([]byte, size) }}
}