forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
319 lines (277 loc) · 7.2 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
package rueidis
import (
"context"
"net"
"sync"
"sync/atomic"
"time"
"github.com/redis/rueidis/internal/cmds"
"github.com/redis/rueidis/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) RedisResult
DoCache(ctx context.Context, cmd Cacheable, ttl time.Duration) RedisResult
DoMulti(ctx context.Context, multi ...Completed) []RedisResult
DoMultiCache(ctx context.Context, multi ...CacheableTTL) []RedisResult
Receive(ctx context.Context, subscribe Completed, fn func(message PubSubMessage)) error
Info() map[string]RedisMessage
Error() error
Close()
Dial() error
Override(conn)
Acquire() wire
Store(w wire)
Addr() string
}
var _ conn = (*mux)(nil)
type mux struct {
init wire
dead wire
pool *pool
wireFn wireFn
dst string
wire []atomic.Value
sc []*singleconnect
mu []sync.Mutex
}
func makeMux(dst string, option *ClientOption, dialFn dialFn) *mux {
dead := deadFn()
return newMux(dst, option, (*pipe)(nil), dead, func() (w wire) {
w, err := newPipe(func() (net.Conn, error) {
return dialFn(dst, option)
}, option)
if err != nil {
dead.error.Store(&errs{error: err})
w = dead
}
return w
})
}
func newMux(dst string, option *ClientOption, init, dead wire, wireFn 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),
}
for i := 0; i < len(m.wire); i++ {
m.wire[i].Store(init)
}
m.pool = newPool(option.BlockingPoolSize, dead, wireFn)
return m
}
func (m *mux) Override(cc conn) {
if m2, ok := cc.(*mux); ok {
for i := 0; i < len(m.wire) && i < len(m2.wire); i++ {
m.wire[i].CompareAndSwap(m.init, m2.wire[i].Load())
}
}
}
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 {
i := i
w := w
w.SetOnCloseHook(func(err error) {
if err != ErrClosing {
m.wire[i].CompareAndSwap(w, m.init)
}
})
m.wire[i].Store(w)
} else {
err = w.Error()
}
}
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]RedisMessage {
return m.pipe(0).Info()
}
func (m *mux) Error() error {
return m.pipe(0).Error()
}
func (m *mux) Do(ctx context.Context, cmd Completed) (resp RedisResult) {
if 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 []RedisResult) {
for _, cmd := range multi {
if cmd.IsBlock() {
goto block
}
}
return m.pipelineMulti(ctx, multi)
block:
cmds.ToBlock(&multi[0]) // mark the first cmd as block if one of them is block to shortcut later check.
return m.blockingMulti(ctx, multi)
}
func (m *mux) blocking(ctx context.Context, cmd Completed) (resp RedisResult) {
wire := m.pool.Acquire()
resp = wire.Do(ctx, cmd)
if resp.NonRedisError() != nil { // abort the wire if blocking command return early (ex. context.DeadlineExceeded)
wire.Close()
}
m.pool.Store(wire)
return resp
}
func (m *mux) blockingMulti(ctx context.Context, cmd []Completed) (resp []RedisResult) {
wire := m.pool.Acquire()
resp = wire.DoMulti(ctx, cmd...)
for _, res := range resp {
if res.NonRedisError() != nil { // abort the wire if blocking command return early (ex. context.DeadlineExceeded)
wire.Close()
break
}
}
m.pool.Store(wire)
return resp
}
func (m *mux) pipeline(ctx context.Context, cmd Completed) (resp RedisResult) {
slot := cmd.Slot() & uint16(len(m.wire)-1)
wire := m.pipe(slot)
if resp = wire.Do(ctx, cmd); isBroken(resp.NonRedisError(), wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
}
return resp
}
func (m *mux) pipelineMulti(ctx context.Context, cmd []Completed) (resp []RedisResult) {
slot := cmd[0].Slot() & uint16(len(m.wire)-1)
wire := m.pipe(slot)
resp = wire.DoMulti(ctx, cmd...)
for _, r := range resp {
if isBroken(r.NonRedisError(), wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
return resp
}
}
return resp
}
func (m *mux) DoCache(ctx context.Context, cmd Cacheable, ttl time.Duration) RedisResult {
slot := cmd.Slot() & uint16(len(m.wire)-1)
wire := m.pipe(slot)
resp := wire.DoCache(ctx, cmd, ttl)
if isBroken(resp.NonRedisError(), wire) {
m.wire[slot].CompareAndSwap(wire, m.init)
}
return resp
}
func (m *mux) DoMultiCache(ctx context.Context, multi ...CacheableTTL) (results []RedisResult) {
var slots map[uint16]int
var mask = uint16(len(m.wire) - 1)
if mask == 0 {
return m.doMultiCache(ctx, 0, multi)
}
slots = make(map[uint16]int, len(m.wire))
for _, cmd := range multi {
slots[cmd.Cmd.Slot()&mask]++
}
if len(slots) == 1 {
return m.doMultiCache(ctx, multi[0].Cmd.Slot()&mask, multi)
}
commands := make(map[uint16][]CacheableTTL, len(slots))
cIndexes := make(map[uint16][]int, len(slots))
for slot, count := range slots {
cIndexes[slot] = make([]int, 0, count)
commands[slot] = make([]CacheableTTL, 0, count)
}
for i, cmd := range multi {
slot := cmd.Cmd.Slot() & mask
commands[slot] = append(commands[slot], cmd)
cIndexes[slot] = append(cIndexes[slot], i)
}
results = make([]RedisResult, len(multi))
util.ParallelKeys(commands, func(slot uint16) {
for i, r := range m.doMultiCache(ctx, slot, commands[slot]) {
results[cIndexes[slot][i]] = r
}
})
return results
}
func (m *mux) doMultiCache(ctx context.Context, slot uint16, multi []CacheableTTL) (resps []RedisResult) {
wire := m.pipe(slot)
resps = wire.DoMultiCache(ctx, multi...)
for _, r := range resps {
if isBroken(r.NonRedisError(), 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 := subscribe.Slot() & uint16(len(m.wire)-1)
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.pool.Acquire()
}
func (m *mux) Store(w wire) {
w.SetPubSubHooks(PubSubHooks{})
w.CleanSubscriptions()
m.pool.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.pool.Close()
}
func (m *mux) Addr() string {
return m.dst
}
func isBroken(err error, w wire) bool {
return err != nil && err != ErrClosing && w.Error() != nil
}