Skip to content

Commit e1b0a0b

Browse files
committed
Cleanup readLine
1 parent b66d05b commit e1b0a0b

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

internal/proto/reader.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,27 @@ func (r *Reader) ReadLine() ([]byte, error) {
6969
// - there is a pending read error;
7070
// - or line does not end with \r\n.
7171
func (r *Reader) readLine() ([]byte, error) {
72-
var s []byte
73-
multi := false
74-
for {
75-
b, err := r.rd.ReadSlice('\n')
76-
if err != nil {
77-
// in case the end of the buffer is not reached
78-
if err == bufio.ErrBufferFull {
79-
s = append(s, b...)
80-
multi = true
81-
continue
82-
} else {
83-
return nil, err
84-
}
85-
}
86-
if len(b) <= 2 || b[len(b)-1] != '\n' || b[len(b)-2] != '\r' {
87-
return nil, fmt.Errorf("redis: invalid reply: %q", b)
72+
b, err := r.rd.ReadSlice('\n')
73+
if err != nil {
74+
if err != bufio.ErrBufferFull {
75+
return nil, err
8876
}
89-
if multi {
90-
b = append(s, b...)
77+
78+
full := make([]byte, len(b))
79+
copy(full, b)
80+
81+
b, err = r.rd.ReadBytes('\n')
82+
if err != nil {
83+
return nil, err
9184
}
92-
b = b[:len(b)-2]
93-
return b, nil
85+
86+
full = append(full, b...)
87+
b = full
88+
}
89+
if len(b) <= 2 || b[len(b)-1] != '\n' || b[len(b)-2] != '\r' {
90+
return nil, fmt.Errorf("redis: invalid reply: %q", b)
9491
}
92+
return b[:len(b)-2], nil
9593
}
9694

9795
func (r *Reader) ReadReply(m MultiBulkParse) (interface{}, error) {

0 commit comments

Comments
 (0)