fix pipeling fragmentation

This commit is contained in:
Josh Baker 2017-10-04 13:07:56 -07:00
parent 86f9248a78
commit f8bc40d881
1 changed files with 15 additions and 19 deletions

View File

@ -39,10 +39,10 @@ type Message struct {
// PipelineReader ... // PipelineReader ...
type PipelineReader struct { type PipelineReader struct {
rd io.Reader rd io.Reader
wr io.Writer wr io.Writer
pbuf [0xFFFF]byte packet [0xFFFF]byte
rbuf []byte buf []byte
} }
const kindHTTP redcon.Kind = 9999 const kindHTTP redcon.Kind = 9999
@ -202,7 +202,7 @@ func readNextCommand(packet []byte, argsIn [][]byte, msg *Message, wr io.Writer)
func (rd *PipelineReader) ReadMessages() ([]*Message, error) { func (rd *PipelineReader) ReadMessages() ([]*Message, error) {
var msgs []*Message var msgs []*Message
moreData: moreData:
n, err := rd.rd.Read(rd.pbuf[:]) n, err := rd.rd.Read(rd.packet[:])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -210,17 +210,13 @@ moreData:
// need more data // need more data
goto moreData goto moreData
} }
var packet []byte data := rd.packet[:n]
if len(rd.rbuf) == 0 { if len(rd.buf) > 0 {
packet = rd.pbuf[:n] data = append(rd.buf, data...)
} else {
rd.rbuf = append(rd.rbuf, rd.pbuf[:n]...)
packet = rd.rbuf
} }
for len(data) > 0 {
for len(packet) > 0 {
msg := &Message{} msg := &Message{}
complete, args, kind, leftover, err := readNextCommand(packet, nil, msg, rd.wr) complete, args, kind, leftover, err := readNextCommand(data, nil, msg, rd.wr)
if err != nil { if err != nil {
break break
} }
@ -247,12 +243,12 @@ moreData:
return nil, errInvalidHTTP return nil, errInvalidHTTP
} }
msgs = append(msgs, msg) msgs = append(msgs, msg)
packet = leftover data = leftover
} }
if len(packet) > 0 { if len(data) > 0 {
rd.rbuf = append(rd.rbuf[:0], packet...) rd.buf = append(rd.buf[:0], data...)
} else if rd.rbuf != nil { } else if len(rd.buf) > 0 {
rd.rbuf = rd.rbuf[:0] rd.buf = rd.buf[:0]
} }
if err != nil && len(msgs) == 0 { if err != nil && len(msgs) == 0 {
return nil, err return nil, err