forked from valkey-io/valkey-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resp.go
388 lines (358 loc) · 8.83 KB
/
resp.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
package valkey
import (
"bufio"
"errors"
"fmt"
"io"
"math"
"strconv"
"strings"
"sync"
)
var errChunked = errors.New("unbounded valkey message")
var errOldNull = errors.New("RESP2 null")
const (
typeBlobString = byte('$')
typeSimpleString = byte('+')
typeSimpleErr = byte('-')
typeInteger = byte(':')
typeNull = byte('_')
typeEnd = byte('.')
typeFloat = byte(',')
typeBool = byte('#')
typeBlobErr = byte('!')
typeVerbatimString = byte('=')
typeBigNumber = byte('(')
typeArray = byte('*')
typeMap = byte('%')
typeSet = byte('~')
typeAttribute = byte('|')
typePush = byte('>')
typeChunk = byte(';')
)
var typeNames = make(map[byte]string, 16)
type reader func(i *bufio.Reader) (ValkeyMessage, error)
var readers = [256]reader{}
func init() {
readers[typeBlobString] = readBlobString
readers[typeSimpleString] = readSimpleString
readers[typeSimpleErr] = readSimpleString
readers[typeInteger] = readInteger
readers[typeNull] = readNull
readers[typeFloat] = readSimpleString
readers[typeBool] = readBoolean
readers[typeBlobErr] = readBlobString
readers[typeVerbatimString] = readBlobString
readers[typeBigNumber] = readSimpleString
readers[typeArray] = readArray
readers[typeMap] = readMap
readers[typeSet] = readArray
readers[typeAttribute] = readMap
readers[typePush] = readArray
readers[typeEnd] = readNull
typeNames[typeBlobString] = "blob string"
typeNames[typeSimpleString] = "simple string"
typeNames[typeSimpleErr] = "simple error"
typeNames[typeInteger] = "int64"
typeNames[typeNull] = "null"
typeNames[typeFloat] = "float64"
typeNames[typeBool] = "boolean"
typeNames[typeBlobErr] = "blob error"
typeNames[typeVerbatimString] = "verbatim string"
typeNames[typeBigNumber] = "big number"
typeNames[typeArray] = "array"
typeNames[typeMap] = "map"
typeNames[typeSet] = "set"
typeNames[typeAttribute] = "attribute"
typeNames[typePush] = "push"
typeNames[typeEnd] = "null"
}
func readSimpleString(i *bufio.Reader) (m ValkeyMessage, err error) {
m.string, err = readS(i)
return
}
func readBlobString(i *bufio.Reader) (m ValkeyMessage, err error) {
m.string, err = readB(i)
if err == errChunked {
sb := strings.Builder{}
for {
if _, err = i.Discard(1); err != nil { // discard the ';'
return ValkeyMessage{}, err
}
length, err := readI(i)
if err != nil {
return ValkeyMessage{}, err
}
if length == 0 {
return ValkeyMessage{string: sb.String()}, nil
}
sb.Grow(int(length))
if _, err = io.CopyN(&sb, i, length); err != nil {
return ValkeyMessage{}, err
}
if _, err = i.Discard(2); err != nil {
return ValkeyMessage{}, err
}
}
}
return
}
func readInteger(i *bufio.Reader) (m ValkeyMessage, err error) {
m.integer, err = readI(i)
return
}
func readBoolean(i *bufio.Reader) (m ValkeyMessage, err error) {
b, err := i.ReadByte()
if err != nil {
return ValkeyMessage{}, err
}
if b == 't' {
m.integer = 1
}
_, err = i.Discard(2)
return
}
func readNull(i *bufio.Reader) (m ValkeyMessage, err error) {
_, err = i.Discard(2)
return
}
func readArray(i *bufio.Reader) (m ValkeyMessage, err error) {
length, err := readI(i)
if err == nil {
if length == -1 {
return m, errOldNull
}
m.values, err = readA(i, length)
} else if err == errChunked {
m.values, err = readE(i)
}
return m, err
}
func readMap(i *bufio.Reader) (m ValkeyMessage, err error) {
length, err := readI(i)
if err == nil {
m.values, err = readA(i, length*2)
} else if err == errChunked {
m.values, err = readE(i)
}
return m, err
}
const ok = "OK"
const okrn = "OK\r\n"
func readS(i *bufio.Reader) (string, error) {
if peek, _ := i.Peek(2); string(peek) == ok {
if peek, _ = i.Peek(4); string(peek) == okrn {
_, _ = i.Discard(4)
return ok, nil
}
}
bs, err := i.ReadBytes('\n')
if err != nil {
return "", err
}
if trim := len(bs) - 2; trim < 0 {
return "", errors.New(unexpectedNoCRLF)
} else {
bs = bs[:trim]
}
return BinaryString(bs), nil
}
func readI(i *bufio.Reader) (v int64, err error) {
bs, err := i.ReadSlice('\n')
if err != nil {
return 0, err
}
if len(bs) < 3 {
return 0, errors.New(unexpectedNoCRLF)
}
if bs[0] == '?' {
return 0, errChunked
}
var s = int64(1)
if bs[0] == '-' {
s = -1
bs = bs[1:]
}
for _, c := range bs[:len(bs)-2] {
if d := int64(c - '0'); d >= 0 && d <= 9 {
v = v*10 + d
} else {
return 0, errors.New(unexpectedNumByte + strconv.Itoa(int(c)))
}
}
return v * s, nil
}
func readB(i *bufio.Reader) (string, error) {
length, err := readI(i)
if err != nil {
return "", err
}
if length == -1 {
return "", errOldNull
}
bs := make([]byte, length)
if _, err = io.ReadFull(i, bs); err != nil {
return "", err
}
if _, err = i.Discard(2); err != nil {
return "", err
}
return BinaryString(bs), nil
}
func readE(i *bufio.Reader) ([]ValkeyMessage, error) {
v := make([]ValkeyMessage, 0)
for {
n, err := readNextMessage(i)
if err != nil {
return nil, err
}
if n.typ == '.' {
return v, err
}
v = append(v, n)
}
}
func readA(i *bufio.Reader, length int64) (v []ValkeyMessage, err error) {
v = make([]ValkeyMessage, length)
for n := int64(0); n < length; n++ {
if v[n], err = readNextMessage(i); err != nil {
return nil, err
}
}
return v, nil
}
func writeB(o *bufio.Writer, id byte, str string) (err error) {
_ = writeN(o, id, len(str))
_, _ = o.WriteString(str)
_, err = o.WriteString("\r\n")
return err
}
func writeS(o *bufio.Writer, id byte, str string) (err error) {
_ = o.WriteByte(id)
_, _ = o.WriteString(str)
_, err = o.WriteString("\r\n")
return err
}
func writeN(o *bufio.Writer, id byte, n int) (err error) {
_ = o.WriteByte(id)
if n < 10 {
_ = o.WriteByte(byte('0' + n))
} else {
for d := int(math.Pow10(int(math.Log10(float64(n))))); d > 0; d /= 10 {
_ = o.WriteByte(byte('0' + n/d))
n = n % d
}
}
_, err = o.WriteString("\r\n")
return err
}
func readNextMessage(i *bufio.Reader) (m ValkeyMessage, err error) {
var attrs *ValkeyMessage
var typ byte
for {
if typ, err = i.ReadByte(); err != nil {
return ValkeyMessage{}, err
}
fn := readers[typ]
if fn == nil {
return ValkeyMessage{}, errors.New(unknownMessageType + strconv.Itoa(int(typ)))
}
if m, err = fn(i); err != nil {
if err == errOldNull {
return ValkeyMessage{typ: typeNull}, nil
}
return ValkeyMessage{}, err
}
m.typ = typ
if m.typ == typeAttribute { // handle the attributes
a := m // clone the original m first, and then take address of the clone
attrs = &a // to avoid go compiler allocating the m on heap which causing worse performance.
m = ValkeyMessage{}
continue
}
m.attrs = attrs
return m, nil
}
}
var lrs = sync.Pool{New: func() any { return &io.LimitedReader{} }}
func streamTo(i *bufio.Reader, w io.Writer) (n int64, err error, clean bool) {
next:
var typ byte
if typ, err = i.ReadByte(); err != nil {
return 0, err, false
}
switch typ {
case typeBlobString, typeVerbatimString, typeChunk:
if n, err = readI(i); err != nil {
if err == errChunked {
var nn int64
nn, err, clean = streamTo(i, w)
for n += nn; nn != 0 && clean && err == nil; n += nn {
nn, err, clean = streamTo(i, w)
}
}
return n, err, clean
}
if n == -1 {
return 0, Nil, true
}
full := n + 2
if n != 0 {
lr := lrs.Get().(*io.LimitedReader)
lr.R = i
lr.N = n
n, err = io.Copy(w, lr)
lr.R = nil
lrs.Put(lr)
} else if typ == typeChunk {
return n, err, true
}
if _, err2 := i.Discard(int(full - n)); err2 == nil {
clean = true
} else if err == nil {
err = err2
}
return n, err, clean
default:
_ = i.UnreadByte()
m, err := readNextMessage(i)
if err != nil {
return 0, err, false
}
switch m.typ {
case typeSimpleString, typeFloat, typeBigNumber:
n, err := w.Write([]byte(m.string))
return int64(n), err, true
case typeNull:
return 0, Nil, true
case typeSimpleErr, typeBlobErr:
mm := m
return 0, (*ValkeyError)(&mm), true
case typeInteger, typeBool:
n, err := w.Write([]byte(strconv.FormatInt(m.integer, 10)))
return int64(n), err, true
case typePush:
goto next
default:
return 0, fmt.Errorf("unsupported valkey %q response for streaming read", typeNames[typ]), true
}
}
}
func writeCmd(o *bufio.Writer, cmd []string) (err error) {
err = writeN(o, '*', len(cmd))
for _, m := range cmd {
err = writeB(o, '$', m)
// TODO: Can we set cmd[i] = "" here to allow GC to eagerly recycle memory?
// Related: https://github.com/redis/rueidis/issues/364
}
return err
}
func flushCmd(o *bufio.Writer, cmd []string) (err error) {
_ = writeCmd(o, cmd)
return o.Flush()
}
const (
unexpectedNoCRLF = "received unexpected simple string message ending without CRLF"
unexpectedNumByte = "received unexpected number byte: "
unknownMessageType = "received unknown message type: "
)