forked from wangaoone/redeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresp.go
122 lines (101 loc) · 2.78 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
// Package resp implements low-level primitives for dealing
// with RESP (REdis Serialization Protocol). It provides client and
// server side readers and writers.
package resp
import (
"fmt"
"io"
)
// ResponseType represents the reply type
type ResponseType uint8
// String returns the response type description
func (t ResponseType) String() string {
switch t {
case TypeArray:
return "Array"
case TypeBulk:
return "Bulk"
case TypeInline:
return "Inline"
case TypeError:
return "Error"
case TypeInt:
return "Int"
case TypeNil:
return "Nil"
}
return "Unknown"
}
// response type iota
const (
TypeUnknown ResponseType = iota
TypeArray
TypeBulk
TypeInline
TypeError
TypeInt
TypeNil
)
// --------------------------------------------------------------------
// Scannable interfaces may implement custom Scan behaviour
type Scannable interface {
// ScanResponse scans theresponse from the reader
ScanResponse(t ResponseType, r ResponseReader) error
}
// NullString is a scannable that can deal with nil values
type NullString struct {
Value string
Valid bool
}
// ScanResponse implements Scannable
func (s *NullString) ScanResponse(t ResponseType, r ResponseReader) error {
if t == TypeNil {
return r.ReadNil()
}
if err := r.Scan(&s.Value); err != nil {
return err
}
s.Valid = true
return nil
}
type AllReadCloser interface {
io.Reader
io.Closer
Len() int64
ReadAll() ([]byte, error)
}
type Holdable interface {
Hold()
Unhold()
}
// --------------------------------------------------------------------
type protoError string
func (p protoError) Error() string { return string(p) }
func protoErrorf(m string, args ...interface{}) error {
return protoError(fmt.Sprintf(m, args...))
}
// IsProtocolError returns true if the error is a protocol error
func IsProtocolError(err error) bool {
_, ok := err.(protoError)
return ok
}
const (
errInvalidMultiBulkLength = protoError("Protocol error: invalid multibulk length")
errInvalidBulkLength = protoError("Protocol error: invalid bulk length")
errBlankBulkLength = protoError("Protocol error: expected '$', got ' '")
errInlineRequestTooLong = protoError("Protocol error: too big inline request")
errNotANumber = protoError("Protocol error: expected a number")
errNotANilMessage = protoError("Protocol error: expected a nil")
errBadResponseType = protoError("Protocol error: bad response type")
ErrBulkTooLong = protoError("Protocol error: bulk too long, stream it")
)
var (
binCRLF = []byte("\r\n")
binOK = []byte("+OK\r\n")
binZERO = []byte(":0\r\n")
binONE = []byte(":1\r\n")
binNIL = []byte("$-1\r\n")
)
// MaxBufferSize is the max request/response buffer size
const MaxBufferSize = 4 * 1024
func mkStdBuffer() []byte { return make([]byte, MaxBufferSize) }