forked from wuxibin89/redis-go-cluster
-
Notifications
You must be signed in to change notification settings - Fork 14
/
conn.go
376 lines (313 loc) · 7.86 KB
/
conn.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
package redis
import (
"net"
"time"
"bufio"
"fmt"
"strconv"
"io"
"errors"
"sync/atomic"
)
var (
okReply interface{} = "OK"
pongReply interface{} = "PONG"
)
type redisError string
func (err redisError) Error() string { return string(err) }
type redisConn struct {
c net.Conn
t time.Time
br *bufio.Reader
bw *bufio.Writer
readTimeout time.Duration
writeTimeout time.Duration
// Pending replies to be read in redis pipeling.
// pending int
pending int32
// Scratch space for formatting argument length.
lenScratch [32]byte
// Scratch space for formatting integer and float.
numScratch [40]byte
}
func (conn *redisConn) auth(password string) (err error) {
var args []interface{}
for _, arg := range strings.Split(password, ":") {
args = append(args, arg)
}
if err = conn.send("AUTH", args...); err != nil {
conn.shutdown()
return
}
if err = conn.flush(); err != nil {
conn.shutdown()
return
}
_, err = conn.receive()
if err != nil {
conn.shutdown()
return
}
return nil
}
func (conn *redisConn) shutdown() {
conn.c.Close()
}
func (conn *redisConn) send(cmd string, args ...interface{}) error {
// this is a bug because send() and receive() maybe run in two different threads.
// so this must be the atomic.
// conn.pending += 1
atomic.AddInt32(&conn.pending, 1)
if conn.writeTimeout > 0 {
conn.c.SetWriteDeadline(time.Now().Add(conn.writeTimeout))
}
if err := conn.writeCommand(cmd, args); err != nil {
return err
}
return nil
}
func (conn *redisConn) flush() error {
if conn.writeTimeout > 0 {
conn.c.SetWriteDeadline(time.Now().Add(conn.writeTimeout))
}
if err := conn.bw.Flush(); err != nil {
return err
}
return nil
}
func (conn *redisConn) receive() (interface{}, error) {
if conn.readTimeout > 0 {
conn.c.SetWriteDeadline(time.Now().Add(conn.readTimeout))
}
if atomic.LoadInt32(&conn.pending) <= 0 {
return nil, errors.New("no more pending reply")
}
// conn.pending -= 1
atomic.AddInt32(&conn.pending, -1)
return conn.readReply()
}
func (conn *redisConn) writeLen(prefix byte, n int) error {
conn.lenScratch[len(conn.lenScratch)-1] = '\n'
conn.lenScratch[len(conn.lenScratch)-2] = '\r'
i := len(conn.lenScratch) - 3
for {
conn.lenScratch[i] = byte('0' + n%10)
i -= 1
n = n / 10
if n == 0 {
break
}
}
conn.lenScratch[i] = prefix
_, err := conn.bw.Write(conn.lenScratch[i:])
return err
}
func (conn *redisConn) writeString(s string) error {
conn.writeLen('$', len(s))
conn.bw.WriteString(s)
_, err := conn.bw.WriteString("\r\n")
return err
}
func (conn *redisConn) writeBytes(p []byte) error {
conn.writeLen('$', len(p))
conn.bw.Write(p)
_, err := conn.bw.WriteString("\r\n")
return err
}
func (conn *redisConn) writeInt64(n int64) error {
return conn.writeBytes(strconv.AppendInt(conn.numScratch[:0], n, 10))
}
func (conn *redisConn) writeUInt64(n uint64) error {
return conn.writeBytes(strconv.AppendUint(conn.numScratch[:0], n, 10))
}
func (conn *redisConn) writeFloat64(n float64) error {
return conn.writeBytes(strconv.AppendFloat(conn.numScratch[:0], n, 'g', -1, 64))
}
// Args must be int64, float64, string, []byte, other types are not supported for safe reason.
func (conn *redisConn) writeCommand(cmd string, args []interface{}) error {
conn.writeLen('*', len(args)+1)
err := conn.writeString(cmd)
for _, arg := range args {
if err != nil {
break
}
switch arg := arg.(type) {
case int8:
err = conn.writeInt64(int64(arg))
case int32:
err = conn.writeInt64(int64(arg))
case int:
err = conn.writeInt64(int64(arg))
case int64:
err = conn.writeInt64(arg)
case uint8:
err = conn.writeUInt64(uint64(arg))
case uint32:
err = conn.writeUInt64(uint64(arg))
case uint:
err = conn.writeUInt64(uint64(arg))
case uint64:
err = conn.writeUInt64(arg)
case float64:
err = conn.writeFloat64(arg)
case string:
err = conn.writeString(arg)
case []byte:
err = conn.writeBytes(arg)
default:
err = fmt.Errorf("unknown type %T", arg)
}
}
return err
}
// readLine read a single line terminated with CRLF.
func (conn *redisConn) readLine() ([]byte, error) {
var line []byte
for {
p, err := conn.br.ReadBytes('\n')
if err != nil {
return nil, err
}
n := len(p) - 2
if n < 0 {
return nil, fmt.Errorf("invalid response: readLine data illegal: %v", p)
}
// bulk string may contain '\n', such as CLUSTER NODES
if p[n] != '\r' {
if line != nil {
line = append(line, p[:]...)
} else {
line = p
}
continue
}
if line != nil {
return append(line, p[:n]...), nil
} else {
return p[:n], nil
}
}
}
func (conn *redisConn) readReply() (interface{}, error) {
line, err := conn.readLine()
if err != nil {
return nil, err
}
if len(line) == 0 {
return nil, errors.New("invalid response: return size is 0")
}
switch line[0] {
case '+':
switch {
case len(line) == 3 && line[1] == 'O' && line[2] == 'K':
// Avoid allocation for frequent "+OK" response.
return okReply, nil
case len(line) == 5 && line[1] == 'P' && line[2] == 'O' && line[3] == 'N' && line[4] == 'G':
// Avoid allocation in PING command benchmarks :)
return pongReply, nil
default:
return string(line[1:]), nil
}
case '-':
return redisError(string(line[1:])), nil
case ':':
return parseInt(line[1:])
case '$':
n, err := parseLen(line[1:])
if n == -1 {
// -1 is legal
// return []byte{}, nil
return nil, nil
} else if n < -1 || err != nil {
return nil, fmt.Errorf("parse length failed: %v, line[0]: [%v], length: [%v]", err, rune(line[0]), n)
}
/*
* Bugfix: see https://github.com/alibaba/RedisFullCheck/issues/73.
* This may include bug when '\r\n' occurs in the data.
* line, err = conn.readLine()
*
* if err != nil {
* return nil, fmt.Errorf("read length failed: %v, line[0]: %v", err, line[0])
* }
* if len(line) != n {
* return nil, fmt.Errorf("invalid response: line length[%v] != n[%v]", len(line), n)
* }
*
* return line, nil
*/
buf := make([]byte, n + 2)
x, err := io.ReadFull(conn.br, buf)
if err != nil {
return nil, err
}
if x < n || buf[n] != '\r' || buf[n + 1] != '\n' {
return nil, fmt.Errorf("invalid response: length[%v] != n[%v] or suffix != \r\n, line: %v",
len(buf), n, buf)
}
return buf[:n], nil
case '*':
n, err := parseLen(line[1:])
if n == -1 {
// -1 is legal.
// For instance when the BLPOP command times out, it returns a Null Array that has a
// count of -1 as in the following example
return []byte{}, nil
} else if n < 0 || err != nil {
return nil, fmt.Errorf("parse length failed: %v, line[0]: %v", err, line[0])
}
r := make([]interface{}, n)
for i := range r {
r[i], err = conn.readReply()
if err != nil {
return nil, fmt.Errorf("read reply failed: %v, line[0]: %v", err, line[0])
}
}
return r, nil
}
return nil, fmt.Errorf("invalid response: line[0]: %v", line[0])
}
// parseLen parses bulk string and array length.
func parseLen(p []byte) (int, error) {
if len(p) == 0 {
return -1, errors.New("invalid response: parseLen == 0")
}
// null element.
if p[0] == '-' && len(p) == 2 && p[1] == '1' {
return -1, nil
}
var n int
for _, b := range p {
n *= 10
if b < '0' || b > '9' {
return -1, fmt.Errorf("invalid response: parseLen: parse character[%c] failed, data: %v", b, p)
}
n += int(b - '0')
}
return n, nil
}
// parseInt parses an integer reply.
func parseInt(p []byte) (int64, error) {
if len(p) == 0 {
return 0, fmt.Errorf("invalid response: parse int failed[length == 0]")
}
var negate bool
if p[0] == '-' {
negate = true
p = p[1:]
if len(p) == 0 {
return 0, fmt.Errorf("invalid response: parse int failed[p: %v]", p)
}
}
var n int64
for _, b := range p {
n *= 10
if b < '0' || b > '9' {
return 0, errors.New("invalid response: parseInt: character[%c] failed, data: %v")
}
n += int64(b - '0')
}
if negate {
n = -n
}
return n, nil
}