Skip to content

Commit

Permalink
issue-91: fix comments, simplify code a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryk-dk committed Oct 24, 2024
1 parent 7c07988 commit 7bf37de
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions pkg/plugin/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,33 @@ func parseStreamResponse(reader io.Reader) backend.DataResponse {
lineField := data.NewFieldFromFieldType(data.FieldTypeString, 0)
lineField.Name = gLineField

sc := bufio.NewReader(reader)
// aligns with -insert.maxLineSizeBytes
br := bufio.NewReaderSize(reader, 256*1024)
var parser fastjson.Parser
var finishedReading bool

finishedReading := false
n := -1
for !finishedReading {
for n := 0; !finishedReading; n++ {
n++
b, err := sc.ReadBytes('\n')
b, err := br.ReadBytes('\n')
if err != nil {
if !errors.Is(err, io.EOF) && !errors.Is(err, bufio.ErrBufferFull) {
return newResponseError(fmt.Errorf("cannot read line in response: %s", err), backend.StatusInternal)
}
if errors.Is(err, bufio.ErrBufferFull) {
// Skip the line if it's too long.
backend.Logger.Info("skipping line number #%d: line too long", n)
continue
}
finishedReading = true
if errors.Is(err, io.EOF) {
// b can be != nil when EOF is returned, so we need to process it
finishedReading = true
} else {
return newResponseError(fmt.Errorf("cannot read line in response: %s", err), backend.StatusInternal)
}
}

if len(b) == 0 {
continue
}

b = bytes.Trim(b, "\n")
value, err := fastjson.ParseBytes(b)
value, err := parser.ParseBytes(b)
if err != nil {
return newResponseError(fmt.Errorf("error decode response: %s", err), backend.StatusInternal)
}
Expand Down

0 comments on commit 7bf37de

Please sign in to comment.