forked from valkey-io/valkey-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
394 lines (342 loc) · 9.18 KB
/
mux.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package valkey
import (
"context"
"net"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/valkey-io/valkey-go/internal/cmds"
"github.com/valkey-io/valkey-go/internal/util"
)
type connFn func(dst string, opt *ClientOption) conn
type dialFn func(dst string, opt *ClientOption) (net.Conn, error)
type wireFn func() wire
type singleconnect struct {
w wire
e error
g sync.WaitGroup
}
type conn interface {
Do(ctx context.Context, cmd Completed) ValkeyResult
DoCache(ctx context.Context, cmd Cacheable, ttl time.Duration) ValkeyResult
DoMulti(ctx context.Context, multi ...Completed) *valkeyresults
DoMultiCache(ctx context.Context, multi ...CacheableTTL) *valkeyresults
Receive(ctx context.Context, subscribe Completed, fn func(message PubSubMessage)) error
DoStream(ctx context.Context, cmd Completed) ValkeyResultStream
DoMultiStream(ctx context.Context, multi ...Completed) MultiValkeyResultStream
Info() map[string]ValkeyMessage
Version() int
Error() error
Close()
Dial() error
Override(conn)
Acquire() wire
Store(w wire)
Addr() string
SetOnCloseHook(func(error))
}
var _ conn = (*mux)(nil)
type mux struct {
init wire
dead wire
clhks atomic.Value
dpool *pool
spool *pool
wireFn wireFn
dst string
wire []atomic.Value
sc []*singleconnect
mu []sync.Mutex
maxp int
maxm int
usePool bool
}
func makeMux(dst string, option *ClientOption, dialFn dialFn) *mux {
dead := deadFn()
connFn := func() (net.Conn, error) {
return dialFn(dst, option)
}
wireFn := func(pipeFn pipeFn) func() wire {
return func() (w wire) {
w, err := pipeFn(connFn, option)
if err != nil {
dead.error.Store(&errs{error: err})
w = dead
}
return w
}
}
return newMux(dst, option, (*pipe)(nil), dead, wireFn(newPipe), wireFn(newPipeNoBg))
}
func newMux(dst string, option *ClientOption, init, dead wire, wireFn wireFn, wireNoBgFn wireFn) *mux {
var multiplex int
if option.PipelineMultiplex >= 0 {
multiplex = 1 << option.PipelineMultiplex
} else {
multiplex = 1
}
m := &mux{dst: dst, init: init, dead: dead, wireFn: wireFn,
wire: make([]atomic.Value, multiplex),
mu: make([]sync.Mutex, multiplex),
sc: make([]*singleconnect, multiplex),
maxp: runtime.GOMAXPROCS(0),
maxm: option.BlockingPipeline,
usePool: option.DisableAutoPipelining,
}
m.clhks.Store(emptyclhks)
for i := 0; i < len(m.wire); i++ {
m.wire[i].Store(init)
}
m.dpool = newPool(option.BlockingPoolSize, dead, wireFn)
m.spool = newPool(option.BlockingPoolSize, dead, wireNoBgFn)
return m
}
func (m *mux) SetOnCloseHook(fn func(error)) {
m.clhks.Store(fn)
}
func (m *mux) setCloseHookOnWire(i uint16, w wire) {
if w != m.dead && w != m.init {
w.SetOnCloseHook(func(err error) {
if err != ErrClosing {
if m.wire[i].CompareAndSwap(w, m.init) {
m.clhks.Load().(func(error))(err)
}
}
})
}
}
func (m *mux) Override(cc conn) {
if m2, ok := cc.(*mux); ok {
for i := 0; i < len(m.wire) && i < len(m2.wire); i++ {
w := m2.wire[i].Load().(wire)
m.setCloseHookOnWire(uint16(i), w) // bind the new m to the old w
m.wire[i].CompareAndSwap(m.init, w)
}
}
}
func (m *mux) _pipe(i uint16) (w wire, err error) {
if w = m.wire[i].Load().(wire); w != m.init {
return w, nil
}
m.mu[i].Lock()
sc := m.sc[i]
if m.sc[i] == nil {
m.sc[i] = &singleconnect{}
m.sc[i].g.Add(1)
}
m.mu[i].Unlock()
if sc != nil {
sc.g.Wait()
return sc.w, sc.e
}
if w = m.wire[i].Load().(wire); w == m.init {
if w = m.wireFn(); w != m.dead {
m.setCloseHookOnWire(i, w)
m.wire[i].Store(w)
} else {
if err = w.Error(); err != ErrClosing {
m.clhks.Load().(func(error))(err)
}
}
}
m.mu[i].Lock()
sc = m.sc[i]
m.sc[i] = nil
m.mu[i].Unlock()
sc.w = w
sc.e = err
sc.g.Done()
return w, err
}
func (m *mux) pipe(i uint16) wire {
w, _ := m._pipe(i)
return w // this should never be nil
}
func (m *mux) Dial() error {
_, err := m._pipe(0)
return err
}
func (m *mux) Info() map[string]ValkeyMessage {
return m.pipe(0).Info()
}
func (m *mux) Version() int {
return m.pipe(0).Version()
}
func (m *mux) Error() error {
return m.pipe(0).Error()
}
func (m *mux) DoStream(ctx context.Context, cmd Completed) ValkeyResultStream {
wire := m.spool.Acquire()
return wire.DoStream(ctx, m.spool, cmd)
}
func (m *mux) DoMultiStream(ctx context.Context, multi ...Completed) MultiValkeyResultStream {
wire := m.spool.Acquire()
return wire.DoMultiStream(ctx, m.spool, multi...)
}
func (m *mux) Do(ctx context.Context, cmd Completed) (resp ValkeyResult) {
if (m.usePool && !cmd.NoReply()) || cmd.IsBlock() {
resp = m.blocking(ctx, cmd)
} else {
resp = m.pipeline(ctx, cmd)
}
return resp
}
func (m *mux) DoMulti(ctx context.Context, multi ...Completed) (resp *valkeyresults) {
if m.usePool || (len(multi) >= m.maxm && m.maxm > 0) {
goto block // use a dedicated connection if the pipeline is too large
}
for _, cmd := range multi {
if cmd.IsBlock() {
cmds.ToBlock(&multi[0]) // mark the first cmd as block if one of them is block to shortcut later check.
goto block
}
}
return m.pipelineMulti(ctx, multi)
block:
for _, cmd := range multi {
if cmd.NoReply() {
return m.pipelineMulti(ctx, multi)
}
}
return m.blockingMulti(ctx, multi)
}
func (m *mux) blocking(ctx context.Context, cmd Completed) (resp ValkeyResult) {
wire := m.spool.Acquire()
resp = wire.Do(ctx, cmd)
if resp.NonValkeyError() != nil { // abort the wire if blocking command return early (ex. context.DeadlineExceeded)
wire.Close()
}
m.spool.Store(wire)
return resp
}
func (m *mux) blockingMulti(ctx context.Context, cmd []Completed) (resp *valkeyresults) {
wire := m.spool.Acquire()
resp = wire.DoMulti(ctx, cmd...)
for _, res := range resp.s {
if res.NonValkeyError() != nil { // abort the wire if blocking command return early (ex. context.DeadlineExceeded)
wire.Close()
break
}
}
m.spool.Store(wire)
return resp
}
func (m *mux) pipeline(ctx context.Context, cmd Completed) (resp ValkeyResult) {
slot := slotfn(len(m.wire), cmd.Slot(), cmd.NoReply())
wire := m.pipe(slot)
if resp = wire.Do(ctx, cmd); isBroken(resp.NonValkeyError(), wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
}
return resp
}
func (m *mux) pipelineMulti(ctx context.Context, cmd []Completed) (resp *valkeyresults) {
slot := slotfn(len(m.wire), cmd[0].Slot(), cmd[0].NoReply())
wire := m.pipe(slot)
resp = wire.DoMulti(ctx, cmd...)
for _, r := range resp.s {
if isBroken(r.NonValkeyError(), wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
return resp
}
}
return resp
}
func (m *mux) DoCache(ctx context.Context, cmd Cacheable, ttl time.Duration) ValkeyResult {
slot := cmd.Slot() & uint16(len(m.wire)-1)
wire := m.pipe(slot)
resp := wire.DoCache(ctx, cmd, ttl)
if isBroken(resp.NonValkeyError(), wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
}
return resp
}
func (m *mux) DoMultiCache(ctx context.Context, multi ...CacheableTTL) (results *valkeyresults) {
var slots *muxslots
var mask = uint16(len(m.wire) - 1)
if mask == 0 {
return m.doMultiCache(ctx, 0, multi)
}
slots = muxslotsp.Get(len(m.wire), len(m.wire))
for _, cmd := range multi {
slots.s[cmd.Cmd.Slot()&mask]++
}
if slots.LessThen(2) {
return m.doMultiCache(ctx, multi[0].Cmd.Slot()&mask, multi)
}
batches := batchcachemaps.Get(len(m.wire), len(m.wire))
for slot, count := range slots.s {
if count > 0 {
batches.m[uint16(slot)] = batchcachep.Get(0, count)
}
}
muxslotsp.Put(slots)
for i, cmd := range multi {
batch := batches.m[cmd.Cmd.Slot()&mask]
batch.commands = append(batch.commands, cmd)
batch.cIndexes = append(batch.cIndexes, i)
}
results = resultsp.Get(len(multi), len(multi))
util.ParallelKeys(m.maxp, batches.m, func(slot uint16) {
batch := batches.m[slot]
resp := m.doMultiCache(ctx, slot, batch.commands)
for i, r := range resp.s {
results.s[batch.cIndexes[i]] = r
}
resultsp.Put(resp)
})
for _, batch := range batches.m {
batchcachep.Put(batch)
}
batchcachemaps.Put(batches)
return results
}
func (m *mux) doMultiCache(ctx context.Context, slot uint16, multi []CacheableTTL) (resps *valkeyresults) {
wire := m.pipe(slot)
resps = wire.DoMultiCache(ctx, multi...)
for _, r := range resps.s {
if isBroken(r.NonValkeyError(), wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
return resps
}
}
return resps
}
func (m *mux) Receive(ctx context.Context, subscribe Completed, fn func(message PubSubMessage)) error {
slot := slotfn(len(m.wire), subscribe.Slot(), subscribe.NoReply())
wire := m.pipe(slot)
err := wire.Receive(ctx, subscribe, fn)
if isBroken(err, wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
}
return err
}
func (m *mux) Acquire() wire {
return m.dpool.Acquire()
}
func (m *mux) Store(w wire) {
w.SetPubSubHooks(PubSubHooks{})
w.CleanSubscriptions()
m.dpool.Store(w)
}
func (m *mux) Close() {
for i := 0; i < len(m.wire); i++ {
if prev := m.wire[i].Swap(m.dead).(wire); prev != m.init && prev != m.dead {
prev.Close()
}
}
m.dpool.Close()
m.spool.Close()
}
func (m *mux) Addr() string {
return m.dst
}
func isBroken(err error, w wire) bool {
return err != nil && err != ErrClosing && w.Error() != nil
}
func slotfn(n int, ks uint16, noreply bool) uint16 {
if n == 1 || ks == cmds.NoSlot || noreply {
return 0
}
return uint16(util.FastRand(n))
}