internal/proto: cleanup bufio reader

This commit is contained in:
Vladimir Mihailenco 2018-08-05 15:07:10 +03:00
parent af03c6a149
commit c651b50db5
2 changed files with 15 additions and 32 deletions

View File

@ -1,7 +1,6 @@
package proto
import (
"bufio"
"bytes"
"errors"
"io"
@ -145,10 +144,7 @@ func (b *BufioReader) ReadSlice(delim byte) (line []byte, err error) {
// Buffer full?
if b.Buffered() >= len(b.buf) {
b.r = b.w
line = b.buf
err = bufio.ErrBufferFull
break
b.grow(len(b.buf) + defaultBufSize)
}
b.fill() // buffer is not full
@ -157,23 +153,8 @@ func (b *BufioReader) ReadSlice(delim byte) (line []byte, err error) {
return
}
func (b *BufioReader) ReadLine() (line []byte, isPrefix bool, err error) {
func (b *BufioReader) ReadLine() (line []byte, err error) {
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 err != nil {
line = nil
@ -192,6 +173,18 @@ func (b *BufioReader) ReadLine() (line []byte, isPrefix bool, err error) {
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) {
b.grow(n)
for b.Buffered() < n {
@ -202,12 +195,6 @@ func (b *BufioReader) ReadN(n int) ([]byte, error) {
return buf, b.readErr()
}
// Buffer is full?
if b.Buffered() >= len(b.buf) {
b.r = b.w
return b.buf, bufio.ErrBufferFull
}
b.fill()
}

View File

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