-
Notifications
You must be signed in to change notification settings - Fork 9
/
redis.go
445 lines (388 loc) · 11.2 KB
/
redis.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Package redis provides access to Redis nodes.
// See <https://redis.io/topics/introduction> for the concept.
package redis
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"path/filepath"
"strconv"
"sync"
"unsafe"
)
// Server Limits
const (
// SizeMax is the upper boundary for byte sizes.
// A string value can be at most 512 MiB in length.
SizeMax = 512 << 20
// ElementMax is the upper boundary for element counts.
// Every hash, list, set, and sorted set, can hold 2³² − 1 elements.
ElementMax = 1<<32 - 1
)
// String is a key and/or value abstraction.
type String interface {
~string | ~[]byte
}
// conservativeMMS uses the IPv6 minimum MTU of 1280 bytes, minus a 40 byte IP
// header, minus a 32 byte TCP header (with timestamps).
const conservativeMSS = 1208
// ErrClosed signals end-of-life due a call to Close.
var ErrClosed = errors.New("redis: connection establishment closed")
// errProtocol signals invalid RESP reception.
var errProtocol = errors.New("redis: protocol violation")
// errNull represents a null reply. This case shoud be contained internally.
// The API represents null with nil and ok booleans conform Go convention.
var errNull = errors.New("redis: null")
// ServerError is a response from Redis.
type ServerError string
// Error honors the error interface.
func (e ServerError) Error() string {
return fmt.Sprintf("redis: error message %q", string(e))
}
// Prefix returns the first word, which represents the error kind.
func (e ServerError) Prefix() string {
s := string(e)
for i, r := range s {
if r == ' ' {
return s[:i]
}
}
return s
}
func isUnixAddr(s string) bool {
return len(s) != 0 && s[0] == '/'
}
func normalizeAddr(s string) string {
if isUnixAddr(s) {
return filepath.Clean(s)
}
host, port, err := net.SplitHostPort(s)
if err != nil {
host = s
}
if host == "" {
host = "localhost"
}
if port == "" {
port = "6379"
}
return net.JoinHostPort(host, port)
}
// ParseInt reads bytes as a decimal string without any validation.
// Empty bytes return zero. The value for any other invalid input is
// undefined, and may be subject to change in the future.
func ParseInt(bytes []byte) int64 {
switch len(bytes) {
case 0:
return 0
case 1: // happens often
return int64(bytes[0]) - '0'
}
u := uint64(bytes[1] - '0')
head := bytes[0]
if head != '-' {
u += 10 * uint64(head-'0')
}
for i := 2; i < len(bytes); i++ {
u = 10*u + uint64(bytes[i]-'0')
}
v := int64(u)
if head == '-' {
v = -v
}
return v
}
func readOK(r *bufio.Reader) error {
line, err := readLine(r)
if err != nil {
return err
}
if len(line) == 5 {
u := binary.LittleEndian.Uint32(line)
if u == '+'|'O'<<8|'K'<<16|'\r'<<24 {
return nil
}
if u == '$'|'-'<<8|'1'<<16|'\r'<<24 {
return errNull
}
}
if len(line) > 3 && line[0] == '-' {
return ServerError(line[1 : len(line)-2])
}
return fmt.Errorf("%w; received %.40q for OK", errProtocol, line)
}
func readInteger(r *bufio.Reader) (int64, error) {
line, err := readLine(r)
switch {
case err != nil:
return 0, err
case len(line) > 3 && line[0] == ':':
return ParseInt(line[1 : len(line)-2]), nil
case len(line) > 3 && line[0] == '-':
return 0, ServerError(line[1 : len(line)-2])
default:
return 0, fmt.Errorf("%w; received %.40q for integer", errProtocol, line)
}
}
func readBulk[T String](r *bufio.Reader) (bulk T, err error) {
size, err := readBulkSize(r)
if err != nil {
return bulk, err
}
bytes := make([]byte, size)
_, err = io.ReadFull(r, bytes)
if err == nil {
_, err = r.Discard(2) // skip CRLF
}
return *(*T)(unsafe.Pointer(&bytes)), err
}
func readArray[T String](r *bufio.Reader) ([]T, error) {
l, err := readArrayLen(r)
if l == 0 {
return nil, err
}
array := make([]T, l)
for i := range array {
array[i], err = readBulk[T](r)
switch err {
case nil, errNull:
break // OK
default:
return nil, err
}
}
return array, nil
}
func readBulkSize(r *bufio.Reader) (int64, error) {
line, err := readLine(r)
switch {
case err != nil:
return 0, err
case len(line) > 3 && line[0] == '$':
size := ParseInt(line[1 : len(line)-2])
if size >= 0 && size <= SizeMax {
return size, nil
}
if size == -1 {
// "null bulk string"
return 0, errNull
}
case len(line) > 3 && line[0] == '-':
return 0, ServerError(line[1 : len(line)-2])
}
return 0, fmt.Errorf("%w; received %.40q for bulk string", errProtocol, line)
}
func readArrayLen(r *bufio.Reader) (int64, error) {
line, err := readLine(r)
switch {
case err != nil:
return 0, err
case len(line) > 3 && line[0] == '*':
l := ParseInt(line[1 : len(line)-2])
if l >= 0 && l <= ElementMax {
return l, nil
}
if l == -1 {
// "null array"
return 0, errNull
}
case len(line) > 3 && line[0] == '-':
return 0, ServerError(line[1 : len(line)-2])
}
return 0, fmt.Errorf("%w; received %.40q for array", errProtocol, line)
}
func readLine(r *bufio.Reader) (line []byte, err error) {
line, err = r.ReadSlice('\n')
if err == bufio.ErrBufferFull {
err = fmt.Errorf("%w; line %.40q… exceeds %d bytes", errProtocol, line, r.Size())
}
return
}
type request struct {
buf []byte
receive chan *bufio.Reader
}
func (r *request) free() {
requestPool.Put(r)
}
var requestPool = sync.Pool{
New: func() interface{} {
return &request{
buf: make([]byte, 256),
receive: make(chan *bufio.Reader),
}
},
}
func requestFix(prefix string) *request {
r := requestPool.Get().(*request)
r.buf = append(r.buf[:0], prefix...)
return r
}
func requestSize(prefix string, size int) *request {
r := requestPool.Get().(*request)
r.buf = append(r.buf[:0], '*')
r.buf = strconv.AppendUint(r.buf, uint64(uint(size)), 10)
r.buf = append(r.buf, prefix...)
return r
}
func requestWithString[T String](prefix string, s T) *request {
r := requestFix(prefix)
r.buf = appendStringToDollar(r.buf, s)
return r
}
func requestWith2Strings[T1, T2 String](prefix string, s1 T1, s2 T2) *request {
r := requestFix(prefix)
r.buf = appendStringAndDollarToDollar(r.buf, s1)
r.buf = appendStringToDollar(r.buf, s2)
return r
}
func requestWith3Strings[T1, T2, T3 String](prefix string, s1 T1, s2 T2, s3 T3) *request {
r := requestFix(prefix)
r.buf = appendStringAndDollarToDollar(r.buf, s1)
r.buf = appendStringAndDollarToDollar(r.buf, s2)
r.buf = appendStringToDollar(r.buf, s3)
return r
}
func requestWithDecimal(prefix string, n int64) *request {
r := requestFix(prefix)
r.addDecimalToDollar(n)
return r
}
func requestWithStringAndDecimal[T String](prefix string, s T, n int64) *request {
r := requestFix(prefix)
r.buf = appendStringAndDollarToDollar(r.buf, s)
r.addDecimalToDollar(n)
return r
}
func requestWithStringAndDecimalAndString[T1, T2 String](prefix string, s1 T1, n int64, s2 T2) *request {
r := requestFix(prefix)
r.buf = appendStringAndDollarToDollar(r.buf, s1)
r.addSizeCRLFDecimal(n)
r.buf = append(r.buf, '\r', '\n', '$')
r.buf = appendStringToDollar(r.buf, s2)
return r
}
func requestWithStringAnd2Decimals[T String](prefix string, s T, n1, n2 int64) *request {
r := requestFix(prefix)
r.buf = appendStringAndDollarToDollar(r.buf, s)
r.addSizeCRLFDecimal(n1)
r.buf = append(r.buf, '\r', '\n', '$')
r.addDecimalToDollar(n2)
return r
}
func requestWith3StringsAndDecimal[T1, T2, T3 String](prefix string, s1 T1, s2 T2, s3 T3, n int64) *request {
r := requestFix(prefix)
r.buf = appendStringAndDollarToDollar(r.buf, s1)
r.buf = appendStringAndDollarToDollar(r.buf, s2)
r.buf = appendStringAndDollarToDollar(r.buf, s3)
r.addDecimalToDollar(n)
return r
}
func requestWith4StringsAndDecimal[T1, T2, T3, T4 String](prefix string, s1 T1, s2 T2, s3 T3, s4 T4, n int64) *request {
r := requestFix(prefix)
r.buf = appendStringAndDollarToDollar(r.buf, s1)
r.buf = appendStringAndDollarToDollar(r.buf, s2)
r.buf = appendStringAndDollarToDollar(r.buf, s3)
r.buf = appendStringAndDollarToDollar(r.buf, s4)
r.addDecimalToDollar(n)
return r
}
// Prefix must exclude both the size header and the command CRLF.
func requestWithList[T String](prefix string, list []T) *request {
r := requestSize(prefix, len(list)+1)
r.buf = appendCRLFAndList(r.buf, list)
return r
}
// Prefix must exclude the size header and it must include the '$' prefix for s.
func requestWithStringAndList[T1, T2 String](prefix string, s T1, list []T2) *request {
r := requestSize(prefix, len(list)+2)
r.buf = appendSizeCRLFString(r.buf, s)
r.buf = appendCRLFAndList(r.buf, list)
return r
}
// AppendCRLFAndList follows dst up with a CRLF and each list T.
func appendCRLFAndList[T String](dst []byte, list []T) []byte {
for _, s := range list {
dst = append(dst, '\r', '\n', '$')
dst = appendSizeCRLFString(dst, s)
}
return append(dst, '\r', '\n')
}
// ErrMapSlices rejects execution due malformed invocation.
var errMapSlices = errors.New("redis: number of keys doesn't match the number of values")
// Prefix must omit both the size header and the command CRLF.
func requestWithMap[Key, Value String](prefix string, keys []Key, values []Value) (*request, error) {
r := requestSize(prefix, len(keys)*2+1)
var err error
r.buf, err = appendCRLFAndMap(r.buf, keys, values)
if err != nil {
return nil, err
}
return r, nil
}
// Prefix must omit both the size header and the command CRLF.
func requestWithStringAndMap[T1, Key, Value String](prefix string, s T1, keys []Key, values []Value) (*request, error) {
r := requestSize(prefix, len(keys)*2+2)
r.buf = appendSizeCRLFString(r.buf, s)
var err error
r.buf, err = appendCRLFAndMap(r.buf, keys, values)
if err != nil {
return nil, err
}
return r, nil
}
// AppendCRLFAndMap follows dst up with a CRLF and each Key–Value pair.
func appendCRLFAndMap[Key, Value String](dst []byte, keys []Key, values []Value) ([]byte, error) {
if len(keys) != len(values) {
return nil, errMapSlices
}
for i := range keys {
dst = append(dst, '\r', '\n', '$')
dst = appendSizeCRLFString(dst, keys[i])
dst = append(dst, '\r', '\n', '$')
dst = appendSizeCRLFString(dst, values[i])
}
return append(dst, '\r', '\n'), nil
}
// AppendStringToDollar follows a '$' in dst up with one payload.
func appendStringToDollar[T String](dst []byte, s T) []byte {
dst = appendSizeCRLFString(dst, s)
return append(dst, '\r', '\n')
}
// AppendStringAndDollarToDollar follows a '$' in dst up with one payload and a '$'.
func appendStringAndDollarToDollar[T String](dst []byte, s T) []byte {
dst = appendSizeCRLFString(dst, s)
return append(dst, '\r', '\n', '$')
}
// AppendSizeCRLFString follows a '$' in dst up with the length of v, a CRLF, and the
// bytes of v.
func appendSizeCRLFString[T String](dst []byte, s T) []byte {
dst = strconv.AppendUint(dst, uint64(len(s)), 10)
dst = append(dst, '\r', '\n')
return append(dst, s...)
}
func (r *request) addDecimalToDollar(v int64) {
r.addSizeCRLFDecimal(v)
r.buf = append(r.buf, '\r', '\n')
}
func (r *request) addSizeCRLFDecimal(v int64) {
sizeOffset := len(r.buf)
sizeSingleDigit := v > -1e8 && v < 1e9
if sizeSingleDigit {
r.buf = append(r.buf, 0, '\r', '\n')
} else {
r.buf = append(r.buf, 0, 0, '\r', '\n')
}
valueOffset := len(r.buf)
r.buf = strconv.AppendInt(r.buf, v, 10)
size := len(r.buf) - valueOffset
if sizeSingleDigit {
r.buf[sizeOffset] = byte(size + '0')
} else { // two digits
r.buf[sizeOffset] = byte(size/10 + '0')
r.buf[sizeOffset+1] = byte(size%10 + '0')
}
}