1
0
mirror of https://github.com/redis/go-redis.git synced 2025-12-25 00:01:00 +03:00

Improves performance

This commit is contained in:
Eelco Cramer
2020-10-09 15:06:47 +02:00
parent 97bbed8a92
commit 39571ccc56
2 changed files with 30 additions and 16 deletions

View File

@@ -55,13 +55,10 @@ func (r *Reader) Reset(rd io.Reader) {
}
func (r *Reader) ReadLine() ([]byte, error) {
line, err := r.rd.ReadBytes('\n')
if err != nil && err != io.EOF {
line, err := r.readLine()
if err != nil {
return nil, err
}
if len(line) == 0 {
return nil, fmt.Errorf("redis: reply is empty")
}
if isNilReply(line) {
return nil, Nil
}
@@ -72,15 +69,29 @@ func (r *Reader) ReadLine() ([]byte, error) {
// - there is a pending read error;
// - or line does not end with \r\n.
func (r *Reader) readLine() ([]byte, error) {
b, err := r.rd.ReadSlice('\n')
if err != nil {
return nil, err
var s []byte
multi := false
for {
b, err := r.rd.ReadSlice('\n')
if err != nil {
// in case the end of the buffer is not reached
if err == bufio.ErrBufferFull {
s = append(s, b...)
multi = true
continue
} else {
return nil, err
}
}
if len(b) <= 2 || b[len(b)-1] != '\n' || b[len(b)-2] != '\r' {
return nil, fmt.Errorf("redis: invalid reply: %q", b)
}
if multi {
b = append(s, b...)
}
b = b[:len(b)-2]
return b, nil
}
if len(b) <= 2 || b[len(b)-1] != '\n' || b[len(b)-2] != '\r' {
return nil, fmt.Errorf("redis: invalid reply: %q", b)
}
b = b[:len(b)-2]
return b, nil
}
func (r *Reader) ReadReply(m MultiBulkParse) (interface{}, error) {