-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: handle LineReader buffer wrapparound in LineProtocolFilter (#502) #503
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,10 @@ type LineProtocolFilterReader struct { | |
lineReader *LineReader | ||
// LineNumber represents line number of csv.Reader, 1 is the first | ||
LineNumber int | ||
// line buffer | ||
line []byte | ||
// wrapparound buffer | ||
wrapBuf []byte | ||
} | ||
|
||
// LineProtocolFilter creates a reader wrapper that parses points, skipping if invalid | ||
|
@@ -21,24 +25,35 @@ func LineProtocolFilter(reader io.Reader) *LineProtocolFilterReader { | |
lineReader.LineNumber = 1 // start counting from 1 | ||
return &LineProtocolFilterReader{ | ||
lineReader: lineReader, | ||
line: make([]byte, 4096), | ||
wrapBuf: make([]byte, 4096), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: I am suggesting elsewhere that this buffer is not necessary. |
||
} | ||
} | ||
|
||
func (state *LineProtocolFilterReader) Read(b []byte) (int, error) { | ||
for { | ||
bytesRead, err := state.lineReader.Read(b) | ||
if err != nil { | ||
return bytesRead, err | ||
} | ||
state.LineNumber = state.lineReader.LastLineNumber | ||
buf := b[0:bytesRead] | ||
pts, err := models.ParsePoints(buf) // any time precision because we won't actually use this point | ||
if err != nil { | ||
log.Printf("invalid point on line %d: %v\n", state.LineNumber, err) | ||
continue | ||
} else if len(pts) == 0 { // no points on this line | ||
continue | ||
bytesRead, err := state.lineReader.Read(state.line) | ||
if err != nil && bytesRead == 0 { | ||
return 0, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This conditional changes the behavior; in the case where bytes are read, but an error is returned (perhaps EOF from a final line without a newline), this will continue, whereas the existing code will return the bytesRead and the error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a fix for a problem you found, or an inadvertent change in behavior? |
||
} | ||
|
||
//read again when we read a partial line at the end of the line reader buffer | ||
if bytesRead > 0 && state.line[bytesRead-1] != '\n' { | ||
wrapBytesRead, _ := state.lineReader.Read(state.wrapBuf[0:]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code ignores any errors from |
||
if wrapBytesRead > 0 { | ||
copy(state.line[bytesRead:], state.wrapBuf[0:wrapBytesRead]) | ||
bytesRead = bytesRead + wrapBytesRead | ||
} | ||
return bytesRead, nil | ||
} | ||
|
||
state.LineNumber = state.lineReader.LastLineNumber | ||
pts, err := models.ParsePoints(state.line[0:bytesRead]) // any time precision because we won't actually use this point | ||
if err != nil { | ||
log.Printf("invalid point on line %d: %v\n", state.LineNumber, err) | ||
return 0, nil | ||
} else if len(pts) == 0 { | ||
return 0, nil | ||
} | ||
|
||
copy(b, state.line[0:bytesRead]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code has an extra Are those copies necessary? Would it be possible to do all the reads into the passed in buffer, |
||
return bytesRead, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4096
should be thedefaultBufSize
constant, not a literalThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I am suggesting elsewhere that this buffer is unnecessary.