From c651b50db543a304f5bb3cac6d74f2c633b02dd9 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sun, 5 Aug 2018 15:07:10 +0300 Subject: [PATCH] internal/proto: cleanup bufio reader --- internal/proto/bufio_reader.go | 41 ++++++++++++---------------------- internal/proto/reader.go | 6 +---- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/internal/proto/bufio_reader.go b/internal/proto/bufio_reader.go index 6e392e6..7ca05d6 100644 --- a/internal/proto/bufio_reader.go +++ b/internal/proto/bufio_reader.go @@ -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() } diff --git a/internal/proto/reader.go b/internal/proto/reader.go index 70fef5e..26167eb 100644 --- a/internal/proto/reader.go +++ b/internal/proto/reader.go @@ -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") }