forked from redis/rueidis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
255 lines (225 loc) · 5.75 KB
/
client.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
package rueidis
import (
"context"
"sync/atomic"
"time"
"github.com/rueian/rueidis/internal/cmds"
)
type singleClient struct {
conn conn
stop uint32
cmd cmds.Builder
retry bool
}
func newSingleClient(opt *ClientOption, prev conn, connFn connFn) (*singleClient, error) {
if len(opt.InitAddress) == 0 {
return nil, ErrNoAddr
}
conn := connFn(opt.InitAddress[0], opt)
conn.Override(prev)
if err := conn.Dial(); err != nil {
return nil, err
}
return newSingleClientWithConn(conn, cmds.NewBuilder(cmds.NoSlot), !opt.DisableRetry), nil
}
func newSingleClientWithConn(conn conn, builder cmds.Builder, retry bool) *singleClient {
return &singleClient{cmd: builder, conn: conn, retry: retry}
}
func (c *singleClient) B() cmds.Builder {
return c.cmd
}
func (c *singleClient) Do(ctx context.Context, cmd Completed) (resp RedisResult) {
retry:
resp = c.conn.Do(ctx, cmd)
if c.retry && cmd.IsReadOnly() && c.isRetryable(resp.NonRedisError(), ctx) {
goto retry
}
if resp.NonRedisError() == nil { // not recycle cmds if error, since cmds may be used later in pipe. consider recycle them by pipe
cmds.PutCompleted(cmd)
}
return resp
}
func (c *singleClient) DoMulti(ctx context.Context, multi ...Completed) (resps []RedisResult) {
if len(multi) == 0 {
return nil
}
retry:
resps = c.conn.DoMulti(ctx, multi...)
if c.retry && allReadOnly(multi) {
for _, resp := range resps {
if c.isRetryable(resp.NonRedisError(), ctx) {
goto retry
}
}
}
for i, cmd := range multi {
if resps[i].NonRedisError() == nil {
cmds.PutCompleted(cmd)
}
}
return resps
}
func (c *singleClient) DoMultiCache(ctx context.Context, multi ...CacheableTTL) (resps []RedisResult) {
if len(multi) == 0 {
return nil
}
retry:
resps = c.conn.DoMultiCache(ctx, multi...)
if c.retry {
for _, resp := range resps {
if c.isRetryable(resp.NonRedisError(), ctx) {
goto retry
}
}
}
for i, cmd := range multi {
if err := resps[i].NonRedisError(); err == nil || err == ErrDoCacheAborted {
cmds.PutCacheable(cmd.Cmd)
}
}
return resps
}
func (c *singleClient) DoCache(ctx context.Context, cmd Cacheable, ttl time.Duration) (resp RedisResult) {
retry:
resp = c.conn.DoCache(ctx, cmd, ttl)
if c.retry && c.isRetryable(resp.NonRedisError(), ctx) {
goto retry
}
if err := resp.NonRedisError(); err == nil || err == ErrDoCacheAborted {
cmds.PutCacheable(cmd)
}
return resp
}
func (c *singleClient) Receive(ctx context.Context, subscribe Completed, fn func(msg PubSubMessage)) (err error) {
retry:
err = c.conn.Receive(ctx, subscribe, fn)
if c.retry {
if _, ok := err.(*RedisError); !ok && c.isRetryable(err, ctx) {
goto retry
}
}
if err == nil {
cmds.PutCompleted(subscribe)
}
return err
}
func (c *singleClient) Dedicated(fn func(DedicatedClient) error) (err error) {
wire := c.conn.Acquire()
dsc := &dedicatedSingleClient{cmd: c.cmd, conn: c.conn, wire: wire, retry: c.retry}
err = fn(dsc)
dsc.release()
return err
}
func (c *singleClient) Dedicate() (DedicatedClient, func()) {
wire := c.conn.Acquire()
dsc := &dedicatedSingleClient{cmd: c.cmd, conn: c.conn, wire: wire, retry: c.retry}
return dsc, dsc.release
}
func (c *singleClient) Nodes() map[string]Client {
return map[string]Client{c.conn.Addr(): c}
}
func (c *singleClient) Close() {
atomic.StoreUint32(&c.stop, 1)
c.conn.Close()
}
type dedicatedSingleClient struct {
conn conn
wire wire
mark uint32
cmd cmds.Builder
retry bool
}
func (c *dedicatedSingleClient) B() cmds.Builder {
return c.cmd
}
func (c *dedicatedSingleClient) Do(ctx context.Context, cmd Completed) (resp RedisResult) {
retry:
resp = c.wire.Do(ctx, cmd)
if c.retry && cmd.IsReadOnly() && isRetryable(resp.NonRedisError(), c.wire, ctx) {
goto retry
}
if resp.NonRedisError() == nil {
cmds.PutCompleted(cmd)
}
return resp
}
func (c *dedicatedSingleClient) DoMulti(ctx context.Context, multi ...Completed) (resp []RedisResult) {
if len(multi) == 0 {
return nil
}
retryable := c.retry
if retryable {
retryable = allReadOnly(multi)
}
retry:
resp = c.wire.DoMulti(ctx, multi...)
if retryable && anyRetryable(resp, c.wire, ctx) {
goto retry
}
for i, cmd := range multi {
if resp[i].NonRedisError() == nil {
cmds.PutCompleted(cmd)
}
}
return resp
}
func (c *dedicatedSingleClient) Receive(ctx context.Context, subscribe Completed, fn func(msg PubSubMessage)) (err error) {
retry:
err = c.wire.Receive(ctx, subscribe, fn)
if c.retry {
if _, ok := err.(*RedisError); !ok && isRetryable(err, c.wire, ctx) {
goto retry
}
}
if err == nil {
cmds.PutCompleted(subscribe)
}
return err
}
func (c *dedicatedSingleClient) SetPubSubHooks(hooks PubSubHooks) <-chan error {
return c.wire.SetPubSubHooks(hooks)
}
func (c *dedicatedSingleClient) Close() {
c.wire.Close()
c.release()
}
func (c *dedicatedSingleClient) release() {
if atomic.CompareAndSwapUint32(&c.mark, 0, 1) {
c.conn.Store(c.wire)
}
}
func (c *singleClient) isRetryable(err error, ctx context.Context) bool {
return err != nil && atomic.LoadUint32(&c.stop) == 0 && ctx.Err() == nil
}
func isRetryable(err error, w wire, ctx context.Context) bool {
return err != nil && w.Error() == nil && ctx.Err() == nil
}
func anyRetryable(resp []RedisResult, w wire, ctx context.Context) bool {
for _, r := range resp {
if isRetryable(r.NonRedisError(), w, ctx) {
return true
}
}
return false
}
func allReadOnly(multi []Completed) bool {
for _, cmd := range multi {
if cmd.IsWrite() {
return false
}
}
return true
}
func chooseSlot(multi []Completed) uint16 {
for i := 0; i < len(multi); i++ {
if multi[i].Slot() != cmds.InitSlot {
for j := i + 1; j < len(multi); j++ {
if multi[j].Slot() != cmds.InitSlot && multi[j].Slot() != multi[i].Slot() {
return cmds.NoSlot
}
}
return multi[i].Slot()
}
}
return cmds.InitSlot
}