-
Notifications
You must be signed in to change notification settings - Fork 37
/
pool.go
99 lines (83 loc) · 1.91 KB
/
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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package nps_mux
import (
"sync"
)
const (
poolSizeBuffer = 4096 // a mux packager total length
poolSizeWindow = poolSizeBuffer - 2 - 4 - 4 - 1 // content length
)
type windowBufferPool struct {
pool sync.Pool
}
func newWindowBufferPool() *windowBufferPool {
return &windowBufferPool{
pool: sync.Pool{
New: func() interface{} {
return make([]byte, poolSizeWindow, poolSizeWindow)
},
},
}
}
//func trace(buf []byte, ty string) {
// pc := make([]uintptr, 10) // at least 1 entry needed
// n := runtime.Callers(0, pc)
// for i := 0; i < n; i++ {
// f := runtime.FuncForPC(pc[i])
// file, line := f.FileLine(pc[i])
// log.Printf("%v %p %s:%d %s\n", ty, buf, file, line, f.Name())
// }
//}
func (Self *windowBufferPool) Get() (buf []byte) {
buf = Self.pool.Get().([]byte)
//trace(buf, "get")
return buf[:poolSizeWindow]
}
func (Self *windowBufferPool) Put(x []byte) {
//trace(x, "put")
Self.pool.Put(x[:poolSizeWindow]) // make buf to full
}
type muxPackagerPool struct {
pool sync.Pool
}
func newMuxPackagerPool() *muxPackagerPool {
return &muxPackagerPool{
pool: sync.Pool{
New: func() interface{} {
pack := muxPackager{}
return &pack
},
},
}
}
func (Self *muxPackagerPool) Get() *muxPackager {
return Self.pool.Get().(*muxPackager)
}
func (Self *muxPackagerPool) Put(pack *muxPackager) {
pack.reset()
Self.pool.Put(pack)
}
type listElementPool struct {
pool sync.Pool
}
func newListElementPool() *listElementPool {
return &listElementPool{
pool: sync.Pool{
New: func() interface{} {
element := listElement{}
return &element
},
},
}
}
func (Self *listElementPool) Get() *listElement {
return Self.pool.Get().(*listElement)
}
func (Self *listElementPool) Put(element *listElement) {
element.Reset()
Self.pool.Put(element)
}
var (
muxPack = newMuxPackagerPool()
windowBuff = newWindowBufferPool()
listEle = newListElementPool()
)