Merge pull request #834 from go-redis/fix/bufio-reader

internal/proto: cleanup bufio reader
This commit is contained in:
Vladimir Mihailenco 2018-08-05 15:19:56 +03:00 committed by GitHub
commit bf3a84175e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 32 deletions

View File

@ -1,7 +1,6 @@
package proto package proto
import ( import (
"bufio"
"bytes" "bytes"
"errors" "errors"
"io" "io"
@ -145,10 +144,7 @@ func (b *BufioReader) ReadSlice(delim byte) (line []byte, err error) {
// Buffer full? // Buffer full?
if b.Buffered() >= len(b.buf) { if b.Buffered() >= len(b.buf) {
b.r = b.w b.grow(len(b.buf) + defaultBufSize)
line = b.buf
err = bufio.ErrBufferFull
break
} }
b.fill() // buffer is not full b.fill() // buffer is not full
@ -157,23 +153,8 @@ func (b *BufioReader) ReadSlice(delim byte) (line []byte, err error) {
return return
} }
func (b *BufioReader) ReadLine() (line []byte, isPrefix bool, err error) { func (b *BufioReader) ReadLine() (line []byte, err error) {
line, err = b.ReadSlice('\n') line, err = b.ReadSlice('\n')
if err == bufio.ErrBufferFull {
// Handle the case where "\r\n" straddles the buffer.
if len(line) > 0 && line[len(line)-1] == '\r' {
// Put the '\r' back on buf and drop it from line.
// Let the next call to ReadLine check for "\r\n".
if b.r == 0 {
// should be unreachable
panic("bufio: tried to rewind past start of buffer")
}
b.r--
line = line[:len(line)-1]
}
return line, true, nil
}
if len(line) == 0 { if len(line) == 0 {
if err != nil { if err != nil {
line = nil line = nil
@ -192,6 +173,18 @@ func (b *BufioReader) ReadLine() (line []byte, isPrefix bool, err error) {
return return
} }
func (b *BufioReader) ReadByte() (byte, error) {
for b.r == b.w {
if b.err != nil {
return 0, b.readErr()
}
b.fill() // buffer is empty
}
c := b.buf[b.r]
b.r++
return c, nil
}
func (b *BufioReader) ReadN(n int) ([]byte, error) { func (b *BufioReader) ReadN(n int) ([]byte, error) {
b.grow(n) b.grow(n)
for b.Buffered() < n { for b.Buffered() < n {
@ -202,12 +195,6 @@ func (b *BufioReader) ReadN(n int) ([]byte, error) {
return buf, b.readErr() return buf, b.readErr()
} }
// Buffer is full?
if b.Buffered() >= len(b.buf) {
b.r = b.w
return b.buf, bufio.ErrBufferFull
}
b.fill() b.fill()
} }

View File

@ -1,7 +1,6 @@
package proto package proto
import ( import (
"bufio"
"fmt" "fmt"
"io" "io"
"strconv" "strconv"
@ -54,13 +53,10 @@ func (r *Reader) ReadN(n int) ([]byte, error) {
} }
func (r *Reader) ReadLine() ([]byte, error) { func (r *Reader) ReadLine() ([]byte, error) {
line, isPrefix, err := r.src.ReadLine() line, err := r.src.ReadLine()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if isPrefix {
return nil, bufio.ErrBufferFull
}
if len(line) == 0 { if len(line) == 0 {
return nil, fmt.Errorf("redis: reply is empty") return nil, fmt.Errorf("redis: reply is empty")
} }