-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlbpool.go
More file actions
57 lines (48 loc) · 1.15 KB
/
lbpool.go
File metadata and controls
57 lines (48 loc) · 1.15 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
package cbytebuf
import "github.com/koykov/lbpool"
// LBPool is a pool implementation based on lbpool.Pool.
type LBPool struct {
Size uint
ReleaseFactor float32
p lbpool.Pool
}
var (
// LBP is a default instance of LB pool for simple cases.
// Just call cbytebuf.LBAcquire() and cbytebuf.LBRelease().
LBP = LBPool{Size: 1000}
_, _ = LBAcquire, LBRelease
)
// Get old byte buffer from the LB pool or create a new byte buffer.
func (p *LBPool) Get() *CByteBuf {
p.p.Size = p.Size
p.p.ReleaseFactor = p.ReleaseFactor
v := p.p.Get()
if v != nil {
if b, ok := v.(*CByteBuf); ok {
metricsHandler.PoolAcquire(uint64(b.h.Cap))
return b
}
}
return &CByteBuf{}
}
// Put byte buffer back to the LB pool.
//
// Using data returned from the buffer after putting is unsafe.
func (p *LBPool) Put(b *CByteBuf) {
if b.h.Data == 0 {
return
}
b.ResetLen()
add := p.p.Put(b)
if add {
metricsHandler.PoolRelease(uint64(b.h.Cap))
}
}
// LBAcquire gets byte buffer from default LB pool instance.
func LBAcquire() *CByteBuf {
return LBP.Get()
}
// LBRelease puts byte buffer back to default LB pool instance.
func LBRelease(b *CByteBuf) {
LBP.Put(b)
}