redis/internal/proto/reader.go

333 lines
6.4 KiB
Go
Raw Permalink Normal View History

2016-07-02 15:52:10 +03:00
package proto
import (
2018-08-17 13:56:37 +03:00
"bufio"
2016-07-02 15:52:10 +03:00
"fmt"
"io"
2020-03-11 17:29:16 +03:00
"github.com/go-redis/redis/v8/internal/util"
2016-07-02 15:52:10 +03:00
)
// redis resp protocol data type.
2016-11-20 10:50:49 +03:00
const (
ErrorReply = '-'
StatusReply = '+'
IntReply = ':'
StringReply = '$'
ArrayReply = '*'
)
2016-07-02 15:52:10 +03:00
2018-02-22 15:14:30 +03:00
//------------------------------------------------------------------------------
const Nil = RedisError("redis: nil") // nolint:errname
2018-02-22 15:14:30 +03:00
type RedisError string
func (e RedisError) Error() string { return string(e) }
func (RedisError) RedisError() {}
2018-02-22 15:14:30 +03:00
//------------------------------------------------------------------------------
2018-08-17 13:56:37 +03:00
type MultiBulkParse func(*Reader, int64) (interface{}, error)
2016-07-02 15:52:10 +03:00
type Reader struct {
2018-08-17 13:56:37 +03:00
rd *bufio.Reader
_buf []byte
2016-07-02 15:52:10 +03:00
}
2018-08-17 13:56:37 +03:00
func NewReader(rd io.Reader) *Reader {
return &Reader{
rd: bufio.NewReader(rd),
_buf: make([]byte, 64),
2016-07-02 15:52:10 +03:00
}
}
2019-08-09 15:11:52 +03:00
func (r *Reader) Buffered() int {
return r.rd.Buffered()
}
2019-08-14 11:09:02 +03:00
func (r *Reader) Peek(n int) ([]byte, error) {
return r.rd.Peek(n)
}
2018-08-17 13:56:37 +03:00
func (r *Reader) Reset(rd io.Reader) {
r.rd.Reset(rd)
}
2018-08-17 13:56:37 +03:00
func (r *Reader) ReadLine() ([]byte, error) {
2020-10-09 16:06:47 +03:00
line, err := r.readLine()
if err != nil {
2016-07-02 15:52:10 +03:00
return nil, err
}
if isNilReply(line) {
2018-02-22 15:14:30 +03:00
return nil, Nil
2016-07-02 15:52:10 +03:00
}
return line, nil
}
2019-08-14 11:09:02 +03:00
// readLine that returns an error if:
// - there is a pending read error;
// - or line does not end with \r\n.
func (r *Reader) readLine() ([]byte, error) {
2020-10-10 12:26:21 +03:00
b, err := r.rd.ReadSlice('\n')
if err != nil {
if err != bufio.ErrBufferFull {
return nil, err
2020-10-09 16:06:47 +03:00
}
2020-10-10 12:26:21 +03:00
full := make([]byte, len(b))
copy(full, b)
b, err = r.rd.ReadBytes('\n')
if err != nil {
return nil, err
2020-10-09 16:06:47 +03:00
}
2020-10-10 12:26:21 +03:00
2021-03-23 11:55:14 +03:00
full = append(full, b...) //nolint:makezero
2020-10-10 12:26:21 +03:00
b = full
}
if len(b) <= 2 || b[len(b)-1] != '\n' || b[len(b)-2] != '\r' {
return nil, fmt.Errorf("redis: invalid reply: %q", b)
2019-08-14 11:09:02 +03:00
}
2020-10-10 12:26:21 +03:00
return b[:len(b)-2], nil
2019-08-14 11:09:02 +03:00
}
2018-08-17 13:56:37 +03:00
func (r *Reader) ReadReply(m MultiBulkParse) (interface{}, error) {
2018-01-24 21:38:47 +03:00
line, err := r.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, err
}
switch line[0] {
case ErrorReply:
2016-12-13 18:28:39 +03:00
return nil, ParseErrorReply(line)
2016-07-02 15:52:10 +03:00
case StatusReply:
2018-08-16 13:25:19 +03:00
return string(line[1:]), nil
2016-07-02 15:52:10 +03:00
case IntReply:
2018-02-22 15:14:30 +03:00
return util.ParseInt(line[1:], 10, 64)
2016-07-02 15:52:10 +03:00
case StringReply:
2018-08-16 13:25:19 +03:00
return r.readStringReply(line)
2016-07-02 15:52:10 +03:00
case ArrayReply:
n, err := parseArrayLen(line)
if err != nil {
return nil, err
}
if m == nil {
err := fmt.Errorf("redis: got %.100q, but multi bulk parser is nil", line)
return nil, err
}
2018-01-24 21:38:47 +03:00
return m(r, n)
2016-07-02 15:52:10 +03:00
}
return nil, fmt.Errorf("redis: can't parse %.100q", line)
}
2018-08-17 13:56:37 +03:00
func (r *Reader) ReadIntReply() (int64, error) {
2018-01-24 21:38:47 +03:00
line, err := r.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
return 0, err
}
switch line[0] {
case ErrorReply:
2016-12-13 18:28:39 +03:00
return 0, ParseErrorReply(line)
2016-07-02 15:52:10 +03:00
case IntReply:
2018-02-22 15:14:30 +03:00
return util.ParseInt(line[1:], 10, 64)
2016-07-02 15:52:10 +03:00
default:
return 0, fmt.Errorf("redis: can't parse int reply: %.100q", line)
}
}
2018-08-17 13:56:37 +03:00
func (r *Reader) ReadString() (string, error) {
2018-01-24 21:38:47 +03:00
line, err := r.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return "", err
2016-07-02 15:52:10 +03:00
}
switch line[0] {
case ErrorReply:
2018-08-16 13:25:19 +03:00
return "", ParseErrorReply(line)
2016-07-02 15:52:10 +03:00
case StringReply:
2018-08-16 13:25:19 +03:00
return r.readStringReply(line)
2016-07-02 15:52:10 +03:00
case StatusReply:
2018-08-16 13:25:19 +03:00
return string(line[1:]), nil
case IntReply:
return string(line[1:]), nil
2016-07-02 15:52:10 +03:00
default:
2018-08-16 13:25:19 +03:00
return "", fmt.Errorf("redis: can't parse reply=%.100q reading string", line)
2016-07-02 15:52:10 +03:00
}
}
2018-08-17 13:56:37 +03:00
func (r *Reader) readStringReply(line []byte) (string, error) {
2018-08-16 13:25:19 +03:00
if isNilReply(line) {
return "", Nil
2017-01-13 14:39:59 +03:00
}
2019-08-14 10:24:13 +03:00
replyLen, err := util.Atoi(line[1:])
2016-07-02 15:52:10 +03:00
if err != nil {
return "", err
}
2018-08-16 13:25:19 +03:00
b := make([]byte, replyLen+2)
2018-08-17 13:56:37 +03:00
_, err = io.ReadFull(r.rd, b)
2016-07-02 15:52:10 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return "", err
2016-07-02 15:52:10 +03:00
}
2018-08-16 13:25:19 +03:00
return util.BytesToString(b[:replyLen]), nil
2016-07-02 15:52:10 +03:00
}
2018-08-17 13:56:37 +03:00
func (r *Reader) ReadArrayReply(m MultiBulkParse) (interface{}, error) {
2018-01-24 21:38:47 +03:00
line, err := r.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, err
}
switch line[0] {
case ErrorReply:
2016-12-13 18:28:39 +03:00
return nil, ParseErrorReply(line)
2016-07-02 15:52:10 +03:00
case ArrayReply:
n, err := parseArrayLen(line)
if err != nil {
return nil, err
}
2018-01-24 21:38:47 +03:00
return m(r, n)
2016-07-02 15:52:10 +03:00
default:
return nil, fmt.Errorf("redis: can't parse array reply: %.100q", line)
}
}
2020-09-23 11:00:34 +03:00
func (r *Reader) ReadArrayLen() (int, error) {
2018-01-24 21:38:47 +03:00
line, err := r.ReadLine()
2016-07-02 15:52:10 +03:00
if err != nil {
return 0, err
}
switch line[0] {
case ErrorReply:
2016-12-13 18:28:39 +03:00
return 0, ParseErrorReply(line)
2016-07-02 15:52:10 +03:00
case ArrayReply:
2020-09-23 11:00:34 +03:00
n, err := parseArrayLen(line)
if err != nil {
return 0, err
}
return int(n), nil
2016-07-02 15:52:10 +03:00
default:
return 0, fmt.Errorf("redis: can't parse array reply: %.100q", line)
}
}
2018-08-17 13:56:37 +03:00
func (r *Reader) ReadScanReply() ([]string, uint64, error) {
2018-01-24 21:38:47 +03:00
n, err := r.ReadArrayLen()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, 0, err
}
if n != 2 {
return nil, 0, fmt.Errorf("redis: got %d elements in scan reply, expected 2", n)
}
2018-01-25 10:15:44 +03:00
cursor, err := r.ReadUint()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, 0, err
}
2018-01-24 21:38:47 +03:00
n, err = r.ReadArrayLen()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, 0, err
}
keys := make([]string, n)
2020-09-23 11:00:34 +03:00
for i := 0; i < n; i++ {
2018-08-16 13:25:19 +03:00
key, err := r.ReadString()
2016-07-02 15:52:10 +03:00
if err != nil {
return nil, 0, err
}
keys[i] = key
}
return keys, cursor, err
}
2018-08-17 13:56:37 +03:00
func (r *Reader) ReadInt() (int64, error) {
2018-08-16 13:25:19 +03:00
b, err := r.readTmpBytesReply()
if err != nil {
return 0, err
2016-07-02 15:52:10 +03:00
}
2018-08-16 13:25:19 +03:00
return util.ParseInt(b, 10, 64)
}
2016-07-02 15:52:10 +03:00
2018-08-17 13:56:37 +03:00
func (r *Reader) ReadUint() (uint64, error) {
2018-08-16 13:25:19 +03:00
b, err := r.readTmpBytesReply()
2016-07-02 15:52:10 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return 0, err
2016-07-02 15:52:10 +03:00
}
2018-08-16 13:25:19 +03:00
return util.ParseUint(b, 10, 64)
}
2016-07-02 15:52:10 +03:00
2018-08-17 13:56:37 +03:00
func (r *Reader) ReadFloatReply() (float64, error) {
2018-08-16 13:25:19 +03:00
b, err := r.readTmpBytesReply()
2016-07-02 15:52:10 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return 0, err
2016-07-02 15:52:10 +03:00
}
2018-08-16 13:25:19 +03:00
return util.ParseFloat(b, 64)
2016-07-02 15:52:10 +03:00
}
2018-08-17 13:56:37 +03:00
func (r *Reader) readTmpBytesReply() ([]byte, error) {
2018-08-16 13:25:19 +03:00
line, err := r.ReadLine()
2017-01-13 14:39:59 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return nil, err
}
switch line[0] {
case ErrorReply:
return nil, ParseErrorReply(line)
case StringReply:
return r._readTmpBytesReply(line)
case StatusReply:
return line[1:], nil
default:
return nil, fmt.Errorf("redis: can't parse string reply: %.100q", line)
2017-01-13 14:39:59 +03:00
}
}
2018-08-17 13:56:37 +03:00
func (r *Reader) _readTmpBytesReply(line []byte) ([]byte, error) {
2018-08-16 13:25:19 +03:00
if isNilReply(line) {
return nil, Nil
}
2019-08-14 10:24:13 +03:00
replyLen, err := util.Atoi(line[1:])
2017-01-13 14:39:59 +03:00
if err != nil {
2018-08-16 13:25:19 +03:00
return nil, err
2017-01-13 14:39:59 +03:00
}
2018-08-16 13:25:19 +03:00
2018-08-17 13:56:37 +03:00
buf := r.buf(replyLen + 2)
_, err = io.ReadFull(r.rd, buf)
2018-08-16 13:25:19 +03:00
if err != nil {
return nil, err
}
2018-08-17 13:56:37 +03:00
return buf[:replyLen], nil
}
func (r *Reader) buf(n int) []byte {
2019-08-14 11:09:02 +03:00
if n <= cap(r._buf) {
return r._buf[:n]
2018-08-17 13:56:37 +03:00
}
2019-08-14 11:09:02 +03:00
d := n - cap(r._buf)
r._buf = append(r._buf, make([]byte, d)...)
return r._buf
2017-01-13 14:39:59 +03:00
}
2016-07-02 15:52:10 +03:00
func isNilReply(b []byte) bool {
return len(b) == 3 &&
(b[0] == StringReply || b[0] == ArrayReply) &&
b[1] == '-' && b[2] == '1'
}
2016-12-13 18:28:39 +03:00
func ParseErrorReply(line []byte) error {
2018-02-22 15:14:30 +03:00
return RedisError(string(line[1:]))
2016-07-02 15:52:10 +03:00
}
func parseArrayLen(line []byte) (int64, error) {
if isNilReply(line) {
2018-02-22 15:14:30 +03:00
return 0, Nil
2016-07-02 15:52:10 +03:00
}
2018-02-22 15:14:30 +03:00
return util.ParseInt(line[1:], 10, 64)
2016-07-02 15:52:10 +03:00
}