This repository has been archived by the owner on Aug 21, 2022. It is now read-only.
/
ledisconn.go
353 lines (287 loc) · 7.62 KB
/
ledisconn.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
package rewledis
import (
"errors"
"time"
"github.com/gomodule/redigo/redis"
)
// Error variables related to the LedisConn type.
var (
ErrTimeoutNotSupported = errors.New("rewledis: connection does not support ConnWithTimeout")
ErrConnClosed = errors.New("rewledis: connection closed")
)
var _ redis.Conn = &LedisConn{}
// LedisConn is a rewriting wrapper around a connection to a LedisDB server.
// Many Redis commands can be issued on this connection. The commands are
// dynamically rewritten to their LedisDB equivalent. See the repository's
// README for more information on which commands are supported.
type LedisConn struct {
slots SlotDeque
rewriter *Rewriter
// err stores any error which occurred during processing in Send(),
// Receive(), Flush() and Do(). err may store errors occuring during
// rewriting or errors returned by the methods of the underlying connection.
err error
conn redis.Conn
}
// RawConn returns the underlying connection to the LedisDB server.
//
// This method must be used with care, as the stack of internally stored reply
// procedures (Slots) can no longer be maintained. RawConn should only be used
// on connections without any pending replies.
func (l *LedisConn) RawConn() redis.Conn {
return l.conn
}
func (l *LedisConn) fatal(err error) error {
if l.err == nil {
l.err = err
}
if l.conn != nil {
_ = l.Close()
l.conn = nil
}
return err
}
// Close closes the connection.
func (l *LedisConn) Close() error {
if l.conn == nil {
return ErrConnClosed
}
err := l.conn.Close()
l.conn = nil
l.slots.Clear()
return err
}
// Err returns a non-nil value when the connection is not usable.
func (l *LedisConn) Err() error {
if l.err != nil {
return l.err
}
if l.conn == nil {
return ErrConnClosed
}
return l.conn.Err()
}
// Send writes the command to the client's output buffer.
func (l *LedisConn) Send(commandName string, args ...interface{}) error {
if l.conn == nil {
return ErrConnClosed
}
slot, err := l.rewriteAndSend(commandName, args...)
if err != nil {
return l.fatal(err)
}
l.slots.PushBack(slot)
return nil
}
// Flush flushes the output buffer to the Redis server.
func (l *LedisConn) Flush() error {
if l.conn == nil {
return ErrConnClosed
}
// error is captured by underlying conn
return l.conn.Flush()
}
// Receive receives a single reply from the Redis server.
func (l *LedisConn) Receive() (interface{}, error) {
if l.conn == nil {
return nil, ErrConnClosed
}
slot := l.slots.PopFront()
var repliesArray [8]interface{}
replies, err := l.receiveRepliesAppend(slot.RepliesCount, repliesArray[:0])
if err != nil {
// error is captured by underlying conn
return nil, err
}
reply, err := slot.ProcessFunc(replies)
if err != nil {
return nil, l.fatal(err)
}
if err, ok := reply.(redis.Error); ok {
return reply, err
}
return reply, nil
}
// Receive receives a single reply from the Redis server. The timeout
// overrides the read timeout set when dialing the connection.
func (l *LedisConn) ReceiveWithTimeout(timeout time.Duration) (interface{}, error) {
if l.conn == nil {
return nil, ErrConnClosed
}
connWithTimeout, ok := l.conn.(redis.ConnWithTimeout)
if !ok {
return nil, ErrTimeoutNotSupported
}
slot := l.slots.PopFront()
var repliesArray [8]interface{}
replies, err := l.receiveRepliesWithTimeoutAppend(connWithTimeout, slot.RepliesCount, timeout, repliesArray[:0])
if err != nil {
// error is captured by underlying conn
return nil, err
}
reply, err := slot.ProcessFunc(replies)
if err != nil {
return nil, l.fatal(err)
}
if err, ok := reply.(redis.Error); ok {
return reply, err
}
return reply, nil
}
// Do sends a command to the server and returns the received reply.
func (l *LedisConn) Do(commandName string, args ...interface{}) (interface{}, error) {
if l.conn == nil {
return nil, ErrConnClosed
}
var err error
var slot Slot
if len(commandName) > 0 {
slot, err = l.rewriteAndSend(commandName, args...)
if err != nil {
// could be not-fataled if fatal is pushed into rewriteAndSend
return nil, l.fatal(err)
}
}
err = l.conn.Flush()
if err != nil {
// error is captured by underlying conn
return nil, err
}
err = l.consumeSlots()
if err != nil {
// error is captured by underlying conn
return nil, err
}
if len(commandName) > 0 {
var repliesArray [8]interface{}
replies, err := l.receiveRepliesAppend(slot.RepliesCount, repliesArray[:0])
if err != nil {
// error is captured by underlying conn
return nil, err
}
reply, err := slot.ProcessFunc(replies)
if err != nil {
return nil, l.fatal(err)
}
if err, ok := reply.(redis.Error); ok {
return reply, err
}
return reply, nil
}
return nil, nil
}
// Do sends a command to the server and returns the received reply. The
// timeout overrides the read timeout set when dialing the connection.
func (l *LedisConn) DoWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (interface{}, error) {
if l.conn == nil {
return nil, ErrConnClosed
}
connWithTimeout, ok := l.conn.(redis.ConnWithTimeout)
if !ok {
return nil, ErrTimeoutNotSupported
}
var err error
var slot Slot
if len(commandName) > 0 {
slot, err = l.rewriteAndSend(commandName, args...)
if err != nil {
// could be not-fataled if fatal is pushed into rewriteAndSend
return nil, l.fatal(err)
}
}
err = l.conn.Flush()
if err != nil {
// error is captured by underlying conn
return nil, err
}
err = l.consumeSlots()
if err != nil {
// error is captured by underlying conn
return nil, err
}
if len(commandName) > 0 {
var repliesArray [8]interface{}
replies, err := l.receiveRepliesWithTimeoutAppend(connWithTimeout, slot.RepliesCount, timeout, repliesArray[:0])
if err != nil {
// error is captured by underlying conn
return nil, err
}
reply, err := slot.ProcessFunc(replies)
if err != nil {
return nil, l.fatal(err)
}
if err, ok := reply.(redis.Error); ok {
return reply, err
}
return reply, nil
}
return nil, nil
}
func (l *LedisConn) consumeSlots() error {
for i := 0; i < l.slots.Len(); i++ {
for j := 0; j < l.slots.At(i).RepliesCount; j++ {
_, err := l.conn.Receive()
if err != nil {
if _, ok := err.(redis.Error); !ok {
return err
}
}
}
}
l.slots.Clear()
return nil
}
func (l *LedisConn) rewriteAndSend(commandName string, args ...interface{}) (Slot, error) {
sendLedisFunc, err := l.rewriter.Rewrite(commandName, args...)
if err != nil {
return Slot{}, err
}
slot, err := sendLedisFunc(l.conn)
if err != nil {
return Slot{}, err
}
return slot, nil
}
func (l *LedisConn) receiveRepliesAppend(count int, replies []interface{}) ([]interface{}, error) {
baseInd := len(replies)
replies = append(replies, make([]interface{}, count)...)
for i := 0; i < count; i++ {
reply, err := l.conn.Receive()
if err != nil {
if _, ok := err.(redis.Error); ok {
if reply == nil {
reply = err
}
} else {
return replies[:baseInd], err
}
}
replies[baseInd+i] = reply
}
return replies, nil
}
func (l *LedisConn) receiveRepliesWithTimeoutAppend(
connWithTimeout redis.ConnWithTimeout,
count int,
timeout time.Duration,
replies []interface{},
) ([]interface{}, error) {
baseInd := len(replies)
replies = append(replies, make([]interface{}, count)...)
deadline := time.Now().Add(timeout)
for i := 0; i < count; i++ {
timeout := time.Now().Sub(deadline)
reply, err := connWithTimeout.ReceiveWithTimeout(timeout)
if err != nil {
if _, ok := err.(redis.Error); ok {
if reply == nil {
reply = err
}
} else {
return replies[:baseInd], err
}
}
replies[baseInd+i] = reply
}
return replies, nil
}