-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg_pool.go
60 lines (51 loc) · 1.38 KB
/
msg_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
package gearman
import (
"sync"
)
// MessagePool keeps the recyclable Message objects to prevent re-allocate
// It wraps sync.Pool
type MessagePool struct {
pool *sync.Pool
}
// NewMessagePool creates a new MessagePool object
func NewMessagePool() *MessagePool {
return &MessagePool{
pool: &sync.Pool{
New: func() interface{} {
return &Message{}
},
},
}
}
// Get returns a free Message object from the pool or creates a new one if the pool is empty
func (p *MessagePool) Get() *Message {
return p.pool.Get().(*Message)
}
// Put puts a free Message object back to the pool
func (p *MessagePool) Put(msg *Message) {
msg.Arguments = nil
p.pool.Put(msg)
}
// MessageHeaderPool keeps the recyclable message header slice to prevent re-allocate
// It wraps sync.Pool
type MessageHeaderPool struct {
pool *sync.Pool
}
// NewMessageHeaderPool creates a new MessageHeaderPool object
func NewMessageHeaderPool() *MessageHeaderPool {
return &MessageHeaderPool{
pool: &sync.Pool{
New: func() interface{} {
return new([headerSize]byte)
},
},
}
}
// Get returns a free Message header slice from the pool or creates a new one if the pool is empty
func (p *MessageHeaderPool) Get() *[headerSize]byte {
return p.pool.Get().(*[headerSize]byte)
}
// Put puts a free Message header slice back to the pool
func (p *MessageHeaderPool) Put(header *[headerSize]byte) {
p.pool.Put(header)
}