-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathprotocol.go
458 lines (401 loc) · 12.9 KB
/
protocol.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
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright 2009-2012 Joubin Houshyar
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// REVU notes for protocol.go
// - all exported funcs that can raise error must return Error.
// - If error is via panic, then it must be a SystemError
// - If SystemError and with cause, the cause must be std.lib or 3rd party
package redis
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strconv"
)
// ----------------------------------------------------------------------------
// Wire
// ----------------------------------------------------------------------------
// protocol's special bytes
const (
cr_byte byte = byte('\r')
lf_byte = byte('\n')
space_byte = byte(' ')
err_byte = byte('-')
ok_byte = byte('+')
count_byte = byte('*')
size_byte = byte('$')
num_byte = byte(':')
true_byte = byte('1')
)
type ctlbytes []byte
var crlf_bytes ctlbytes = ctlbytes{cr_byte, lf_byte}
// ----------------------------------------------------------------------------
// Services
// ----------------------------------------------------------------------------
// Creates the byte buffer that corresponds to the specified Command and
// provided command arguments.
//
// panics on error (with redis.Error)
func CreateRequestBytes(cmd *Command, args [][]byte) []byte {
defer func() {
if e := recover(); e != nil {
panic(newSystemErrorf("CreateRequestBytes(%s) - failed to create request buffer", cmd.Code))
}
}()
cmd_bytes := []byte(cmd.Code)
buffer := bytes.NewBufferString("")
buffer.WriteByte(count_byte)
buffer.Write([]byte(strconv.Itoa(len(args) + 1)))
buffer.Write(crlf_bytes)
buffer.WriteByte(size_byte)
buffer.Write([]byte(strconv.Itoa(len(cmd_bytes))))
buffer.Write(crlf_bytes)
buffer.Write(cmd_bytes)
buffer.Write(crlf_bytes)
for _, s := range args {
buffer.WriteByte(size_byte)
buffer.Write([]byte(strconv.Itoa(len(s))))
buffer.Write(crlf_bytes)
buffer.Write(s)
buffer.Write(crlf_bytes)
}
return buffer.Bytes()
}
// Creates a specific Future type for the given Redis command
// and returns it as a generic reference.
func CreateFuture(cmd *Command) (future interface{}) {
switch cmd.RespType {
case BOOLEAN:
future = newFutureBool()
case BULK:
future = newFutureBytes()
case MULTI_BULK:
future = newFutureBytesArray()
case NUMBER:
future = newFutureInt64()
case STATUS:
future = newFutureBool()
case STRING:
future = newFutureString()
case VIRTUAL:
// REVU - treating virtual futures as FutureBools (always true)
future = newFutureBool()
}
return
}
// Sets the type specific result value from the response for the future reference
// based on the command type.
func SetFutureResult(future interface{}, cmd *Command, r Response) {
if r.IsError() {
future.(FutureResult).onError(newRedisError(r.GetMessage()))
} else {
switch cmd.RespType {
case BOOLEAN:
future.(FutureBool).set(r.GetBooleanValue())
case BULK:
future.(FutureBytes).set(r.GetBulkData())
case MULTI_BULK:
future.(FutureBytesArray).set(r.GetMultiBulkData())
case NUMBER:
future.(FutureInt64).set(r.GetNumberValue())
case STATUS:
future.(FutureBool).set(true)
case STRING:
future.(FutureString).set(r.GetStringValue())
case VIRTUAL:
// REVU - OK to treat virtual commands as FutureBool
future.(FutureBool).set(true)
}
}
}
// ----------------------------------------------------------------------------
// request processing
// ----------------------------------------------------------------------------
// Either writes all the bytes or it fails and returns an error
//
// panics on error (with redis.Error)
func sendRequest(w io.Writer, data []byte) {
loginfo := "sendRequest"
if w == nil {
panic(newSystemErrorf("<BUG> %s() - nil Writer", loginfo))
}
n, e := w.Write(data)
if e != nil {
panic(newSystemErrorf("%s() - connection Write wrote %d bytes only.", loginfo, n))
}
// doc isn't too clear but the underlying netFD may return n<len(data) AND
// e == nil, but that's precisely what we're checking.
// presumably we can try sending the remaining bytes but that is precisely
// what netFD.Write is doing (and it couldn't) so ...
if n < len(data) {
panic(newSystemErrorf("%s() - connection Write wrote %d bytes only.", loginfo, n))
}
}
// ----------------------------------------------------------------------------
// Response
// ----------------------------------------------------------------------------
type Response interface {
IsError() bool
GetMessage() string
GetBooleanValue() bool
GetNumberValue() int64
GetStringValue() string
GetBulkData() []byte
GetMultiBulkData() [][]byte
}
type _response struct {
isError bool
msg string
boolval bool
numval int64
stringval string
bulkdata []byte
multibulkdata [][]byte
}
func (r *_response) IsError() bool { return r.isError }
func (r *_response) GetMessage() string { return r.msg }
func (r *_response) GetBooleanValue() bool { return r.boolval }
func (r *_response) GetNumberValue() int64 { return r.numval }
func (r *_response) GetStringValue() string { return r.stringval }
func (r *_response) GetBulkData() []byte { return r.bulkdata }
func (r *_response) GetMultiBulkData() [][]byte {
return r.multibulkdata
}
// ----------------------------------------------------------------------------
// response processing
// ----------------------------------------------------------------------------
// Gets the response to the command.
//
// The returned response (regardless of flavor) may have (application level)
// errors as sent from Redis server. (Note err will be nil in that case)
//
// Any errors (whether runtime or bugs) are returned as redis.Error.
func GetResponse(reader *bufio.Reader, cmd *Command) (resp Response, err Error) {
defer func() {
err = onRecover(recover(), "GetResponse")
}()
buf := readToCRLF(reader)
// Redis error
if buf[0] == err_byte {
resp = &_response{msg: string(buf[1:]), isError: true}
return
}
switch cmd.RespType {
case STATUS:
resp = &_response{msg: string(buf[1:])}
return
case STRING:
assertCtlByte(buf, ok_byte, "STRING")
resp = &_response{stringval: string(buf[1:])}
return
case BOOLEAN:
assertCtlByte(buf, num_byte, "BOOLEAN")
resp = &_response{boolval: buf[1] == true_byte}
return
case NUMBER:
assertCtlByte(buf, num_byte, "NUMBER")
n, e := strconv.ParseInt(string(buf[1:]), 10, 64)
assertNotError(e, "in GetResponse - parse error in NUMBER response")
resp = &_response{numval: n}
return
case VIRTUAL:
resp = &_response{boolval: true}
return
case BULK:
assertCtlByte(buf, size_byte, "BULK")
size, e := strconv.Atoi(string(buf[1:]))
assertNotError(e, "in GetResponse - parse error in BULK size")
resp = &_response{bulkdata: readBulkData(reader, size)}
return
case MULTI_BULK:
assertCtlByte(buf, count_byte, "MULTI_BULK")
cnt, e := strconv.Atoi(string(buf[1:]))
assertNotError(e, "in GetResponse - parse error in MULTIBULK cnt")
resp = &_response{multibulkdata: readMultiBulkData(reader, cnt)}
return
}
panic(fmt.Errorf("BUG - GetResponse - this should not have been reached"))
}
// panics on error (with redis.Error)
func assertCtlByte(buf []byte, b byte, info string) {
if buf[0] != b {
panic(newSystemErrorf("control byte for %s is not '%s' as expected - got '%s'", info, string(b), string(buf[0])))
}
}
// panics on error (with redis.Error)
func assertNotError(e error, info string) {
if e != nil {
panic(newSystemErrorWithCause(info, e))
}
}
// ----------------------------------------------------------------------------
// PubSub message
// ----------------------------------------------------------------------------
type PubSubMType int
const (
SUBSCRIBE_ACK PubSubMType = iota
UNSUBSCRIBE_ACK
MESSAGE
)
func (t PubSubMType) String() string {
switch t {
case SUBSCRIBE_ACK:
return "SUBSCRIBE_ACK"
case UNSUBSCRIBE_ACK:
return "UNSUBSCRIBE_ACK"
case MESSAGE:
return "MESSAGE"
}
panic(newSystemErrorf("BUG - unknown PubSubMType %d", t))
}
// Conforms to the payload as received from wire.
// If Type is MESSAGE, then Body will contain a message, and
// SubscriptionCnt will be -1.
// otherwise, it is expected that SubscriptionCnt will contain subscription-info,
// e.g. number of subscribed channels, and data will be nil.
type Message struct {
Type PubSubMType
Topic string
Body []byte
SubscriptionCnt int
}
func (m Message) String() string {
return fmt.Sprintf("Message [type:%s topic:%s body:<%s> subcnt:%d]",
m.Type,
m.Topic,
m.Body,
m.SubscriptionCnt,
)
}
func newMessage(topic string, Body []byte) *Message {
m := Message{}
m.Type = MESSAGE
m.Topic = topic
m.Body = Body
return &m
}
func newPubSubAck(Type PubSubMType, topic string, scnt int) *Message {
m := Message{}
m.Type = Type
m.Topic = topic
m.SubscriptionCnt = scnt
return &m
}
func newSubcribeAck(topic string, scnt int) *Message {
return newPubSubAck(SUBSCRIBE_ACK, topic, scnt)
}
func newUnsubcribeAck(topic string, scnt int) *Message {
return newPubSubAck(UNSUBSCRIBE_ACK, topic, scnt)
}
// ----------------------------------------------------------------------------
// PubSub message processing
// ----------------------------------------------------------------------------
// Fully reads and processes an expected Redis pubsub message byte sequence.
func GetPubSubResponse(r *bufio.Reader) (msg *Message, err Error) {
defer func() {
err = onRecover(recover(), "GetPubSubResponse")
}()
buf := readToCRLF(r)
assertCtlByte(buf, count_byte, "PubSub Sequence")
num, e := strconv.ParseInt(string(buf[1:len(buf)]), 10, 64)
assertNotError(e, "in getPubSubResponse - ParseInt")
if num != 3 {
panic(fmt.Errorf("<BUG> Expecting *3 for len in response - got %d - buf: %s", num, buf))
}
header := readMultiBulkData(r, 2)
msgtype := string(header[0])
subid := string(header[1])
buf = readToCRLF(r)
n, e := strconv.Atoi(string(buf[1:]))
assertNotError(e, "in getPubSubResponse - pubsub msg seq 3 line - number parse error")
// TODO - REVU decisiont to conflate P/SUB and P/UNSUB
switch msgtype {
case "subscribe":
assertCtlByte(buf, num_byte, "subscribe")
msg = newSubcribeAck(subid, n)
case "unsubscribe":
assertCtlByte(buf, num_byte, "unsubscribe")
msg = newUnsubcribeAck(subid, n)
case "message":
assertCtlByte(buf, size_byte, "MESSAGE")
msg = newMessage(subid, readBulkData(r, int(n)))
// TODO
case "psubscribe", "punsubscribe", "pmessage":
panic(fmt.Errorf("<BUG> - pattern-based message type %s not implemented", msgtype))
}
return
}
// ----------------------------------------------------------------------------
// protocol i/o
// ----------------------------------------------------------------------------
// reads all bytes upto CR-LF. (Will eat those last two bytes)
// return the line []byte up to CR-LF
// error returned is NOT ("-ERR ..."). If there is a Redis error
// that is in the line buffer returned
//
// panics on errors (with redis.Error)
func readToCRLF(r *bufio.Reader) []byte {
// var buf []byte
buf, e := r.ReadBytes(cr_byte)
if e != nil {
panic(newSystemErrorWithCause("readToCRLF - ReadBytes", e))
}
var b byte
b, e = r.ReadByte()
if e != nil {
panic(newSystemErrorWithCause("readToCRLF - ReadByte", e))
}
if b != lf_byte {
e = errors.New("<BUG> Expecting a Linefeed byte here!")
}
return buf[0 : len(buf)-1]
}
// Reads a multibulk response of given expected elements.
//
// panics on errors (with redis.Error)
func readBulkData(r *bufio.Reader, n int) (data []byte) {
if n >= 0 {
buffsize := n + 2
data = make([]byte, buffsize)
if _, e := io.ReadFull(r, data); e != nil {
panic(newSystemErrorWithCause("readBulkData - ReadFull", e))
} else {
if data[n] != cr_byte || data[n+1] != lf_byte {
panic(newSystemErrorf("terminal was not crlf_bytes as expected - data[n:n+1]:%s", data[n:n+1]))
}
data = data[:n]
}
}
return
}
// Reads a multibulk response of given expected elements.
// The initial *num\r\n is assumed to have been consumed.
//
// panics on errors (with redis.Error)
func readMultiBulkData(conn *bufio.Reader, num int) [][]byte {
data := make([][]byte, num)
for i := 0; i < num; i++ {
buf := readToCRLF(conn)
if buf[0] != size_byte {
panic(newSystemErrorf("readMultiBulkData - expected: size_byte got: %d", buf[0]))
}
size, e := strconv.Atoi(string(buf[1:]))
if e != nil {
panic(newSystemErrorWithCause("readMultiBulkData - Atoi parse error", e))
}
data[i] = readBulkData(conn, size)
}
return data
}