Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #175 from juanjux/fix/issue_server_100
Browse files Browse the repository at this point in the history
Replace ReadLine for ReadBytes
  • Loading branch information
juanjux authored Sep 21, 2017
2 parents 8b78492 + 34d47c5 commit 5fb8c68
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 50 deletions.
2 changes: 1 addition & 1 deletion protocol/internal/testnative/native
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
dir=$(dirname $0)
cd $dir
exec go run ./main.go $@
exec go run ./main.go $@
49 changes: 2 additions & 47 deletions protocol/jsonlines/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package jsonlines
import (
"bufio"
"encoding/json"
"fmt"
"io"
)

Expand All @@ -14,8 +13,7 @@ const (
)

type lineReader interface {
ReadLine() ([]byte, bool, error)
ReadSlice(delim byte) (line []byte, err error)
ReadBytes(delim byte) ([]byte, error)
}

// Decoder decodes JSON lines.
Expand All @@ -41,54 +39,11 @@ func NewDecoder(r io.Reader) Decoder {
return &decoder{r: lr}
}

// Read and discard everything until the next newline
func (d *decoder) discardPending() error {
for {
_, err := d.r.ReadSlice('\n')
switch err {
case io.EOF:
return nil
case bufio.ErrBufferFull:
continue
case nil:
continue
default:
return err
}
}
}

func (d *decoder) readLine() (line []byte, err error) {
for {
chunk, isPrefix, err := d.r.ReadLine()
if err != nil {
if !isPrefix {
// Discard pending contents in the input buffer to avoid IO blocking
discardErr := d.discardPending()
if discardErr != nil {
err = fmt.Errorf("%s; aditionally, there was an error "+
"trying to dicard the input buffer: %s",
err, discardErr)
}
}
return nil, err
}
line = append(line, chunk...)

if !isPrefix {
// EOL found.
break
}
}

return line, nil
}

// Decode decodes the next line in the reader.
// It does not check JSON for well-formedness before decoding, so in case of
// error, the structure might be half-filled.
func (d *decoder) Decode(v interface{}) error {
line, err := d.readLine()
line, err := d.r.ReadBytes('\n')
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions protocol/jsonlines/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func TestDecoder(t *testing.T) {

input := `{"example":1}
{"example":2}
{"example":3}`
{"example":3}
`
d := NewDecoder(strings.NewReader(input))
out := map[string]int{}

Expand All @@ -39,7 +40,8 @@ func TestDecoderWithBufferedReader(t *testing.T) {

input := `{"example":1}
{"example":2}
{"example":3}`
{"example":3}
`
d := NewDecoder(bufio.NewReader(strings.NewReader(input)))
out := map[string]int{}

Expand Down

0 comments on commit 5fb8c68

Please sign in to comment.