-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.go
99 lines (79 loc) · 1.64 KB
/
command.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 memcache
import (
"sync"
)
type commandType int
const (
commandTypeMGet commandType = iota + 1
commandTypeMSet
commandTypeMDel
commandTypeFlushAll
)
// =====================
// Pool of Bytes
// =====================
var requestBytesPool = bytesPool{
pool: sync.Pool{
New: func() any {
return make([]byte, 0, 256)
},
},
}
var responseBytesPool = bytesPool{
pool: sync.Pool{
New: func() any {
return make([]byte, 0, 1024)
},
},
}
type bytesPool struct {
pool sync.Pool
}
func (p *bytesPool) get() []byte {
return p.pool.Get().([]byte)
}
func (p *bytesPool) put(data []byte) {
data = data[:0]
p.pool.Put(data)
}
type commandData struct {
cmdCount int
sibling *commandData // for commands of the same pipeline
link *commandData // for linking to form a list
requestData []byte
responseData []byte
responseBinaries [][]byte // mget binary responses
conn *senderConnection
lastErr error
ch chan error
}
func newCommandChannel() chan error {
return make(chan error, 1)
}
func newCommand() *commandData {
c := &commandData{
requestData: requestBytesPool.get(),
responseData: responseBytesPool.get(),
}
c.ch = newCommandChannel()
return c
}
func freeCommandResponseData(cmd *commandData) {
responseBytesPool.put(cmd.responseData)
cmd.responseData = nil
for i := range cmd.responseBinaries {
cmd.responseBinaries[i] = nil
}
cmd.responseBinaries = nil
}
func freeCommandRequestData(cmd *commandData) {
requestBytesPool.put(cmd.requestData)
cmd.requestData = nil
}
func (c *commandData) waitCompleted() {
err := <-c.ch
c.lastErr = err
}
func (c *commandData) setCompleted(err error) {
c.ch <- err
}