forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncp.go
253 lines (212 loc) · 4.84 KB
/
syncp.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package rueidis
import (
"time"
"github.com/redis/rueidis/internal/util"
)
var (
resultsp = util.NewPool(func(capacity int) *redisresults {
return &redisresults{s: make([]RedisResult, 0, capacity)}
})
mgetcmdsp = util.NewPool(func(capacity int) *mgetcmds {
return &mgetcmds{s: make([]Completed, 0, capacity)}
})
retryp = util.NewPool(func(capacity int) *retry {
return &retry{
cIndexes: make([]int, 0, capacity),
commands: make([]Completed, 0, capacity),
}
})
mgetcachecmdsp = util.NewPool(func(capacity int) *mgetcachecmds {
return &mgetcachecmds{s: make([]CacheableTTL, 0, capacity)}
})
retrycachep = util.NewPool(func(capacity int) *retrycache {
return &retrycache{
cIndexes: make([]int, 0, capacity),
commands: make([]CacheableTTL, 0, capacity),
}
})
batchcachep = util.NewPool(func(capacity int) *batchcache {
return &batchcache{
cIndexes: make([]int, 0, capacity),
commands: make([]CacheableTTL, 0, capacity),
}
})
batchcachemaps = util.NewPool(func(capacity int) *batchcachemap {
return &batchcachemap{m: make(map[uint16]*batchcache, capacity), n: capacity}
})
muxslotsp = util.NewPool(func(capacity int) *muxslots {
return &muxslots{s: make([]int, 0, capacity)}
})
connretryp = util.NewPool(func(capacity int) *connretry {
return &connretry{m: make(map[conn]*retry, capacity), n: capacity}
})
conncountp = util.NewPool(func(capacity int) *conncount {
return &conncount{m: make(map[conn]int, capacity), n: capacity}
})
connretrycachep = util.NewPool(func(capacity int) *connretrycache {
return &connretrycache{m: make(map[conn]*retrycache, capacity), n: capacity}
})
)
type muxslots struct {
s []int
}
func (r *muxslots) Capacity() int {
return cap(r.s)
}
func (r *muxslots) ResetLen(n int) {
clear(r.s)
r.s = r.s[:n]
}
func (r *muxslots) LessThen(n int) bool {
count := 0
for _, value := range r.s {
if value > 0 {
if count++; count == n {
return false
}
}
}
return true
}
type redisresults struct {
s []RedisResult
}
func (r *redisresults) Capacity() int {
return cap(r.s)
}
func (r *redisresults) ResetLen(n int) {
clear(r.s)
r.s = r.s[:n]
}
type cacheentries struct {
e map[int]CacheEntry
c int
}
func (c *cacheentries) Capacity() int {
return c.c
}
func (c *cacheentries) ResetLen(n int) {
clear(c.e)
}
var entriesp = util.NewPool(func(capacity int) *cacheentries {
return &cacheentries{e: make(map[int]CacheEntry, capacity), c: capacity}
})
type mgetcachecmds struct {
s []CacheableTTL
}
func (r *mgetcachecmds) Capacity() int {
return cap(r.s)
}
func (r *mgetcachecmds) ResetLen(n int) {
clear(r.s)
r.s = r.s[:n]
}
type mgetcmds struct {
s []Completed
}
func (r *mgetcmds) Capacity() int {
return cap(r.s)
}
func (r *mgetcmds) ResetLen(n int) {
clear(r.s)
r.s = r.s[:n]
}
type retry struct {
cIndexes []int
commands []Completed
aIndexes []int
cAskings []Completed
}
func (r *retry) Capacity() int {
return cap(r.commands)
}
func (r *retry) ResetLen(n int) {
clear(r.cIndexes)
clear(r.commands)
clear(r.aIndexes)
clear(r.cAskings)
r.cIndexes = r.cIndexes[:n]
r.commands = r.commands[:n]
r.aIndexes = r.aIndexes[:0]
r.cAskings = r.cAskings[:0]
}
type retrycache struct {
cIndexes []int
commands []CacheableTTL
aIndexes []int
cAskings []CacheableTTL
}
func (r *retrycache) Capacity() int {
return cap(r.commands)
}
func (r *retrycache) ResetLen(n int) {
clear(r.cIndexes)
clear(r.commands)
clear(r.aIndexes)
clear(r.cAskings)
r.cIndexes = r.cIndexes[:n]
r.commands = r.commands[:n]
r.aIndexes = r.aIndexes[:0]
r.cAskings = r.cAskings[:0]
}
type batchcache struct {
cIndexes []int
commands []CacheableTTL
}
func (r *batchcache) Capacity() int {
return cap(r.commands)
}
func (r *batchcache) ResetLen(n int) {
clear(r.cIndexes)
clear(r.commands)
r.cIndexes = r.cIndexes[:n]
r.commands = r.commands[:n]
}
type batchcachemap struct {
m map[uint16]*batchcache
n int
}
func (r *batchcachemap) Capacity() int {
return r.n
}
func (r *batchcachemap) ResetLen(n int) {
clear(r.m)
}
type conncount struct {
m map[conn]int
n int
}
func (r *conncount) Capacity() int {
return r.n
}
func (r *conncount) ResetLen(n int) {
clear(r.m)
}
type connretry struct {
m map[conn]*retry
n int
Redirects uint32 // NOTE: This is not thread-safe.
RetryDelay time.Duration // NOTE: It is not thread-safe.
}
func (r *connretry) Capacity() int {
return r.n
}
func (r *connretry) ResetLen(n int) {
clear(r.m)
r.Redirects = 0
r.RetryDelay = time.Duration(-1) // No retry.
}
type connretrycache struct {
m map[conn]*retrycache
n int
Redirects uint32 // NOTE: This is not thread-safe.
RetryDelay time.Duration // NOTE: It is not thread-safe.
}
func (r *connretrycache) Capacity() int {
return r.n
}
func (r *connretrycache) ResetLen(n int) {
clear(r.m)
r.Redirects = 0
r.RetryDelay = time.Duration(-1) // No retry.
}