Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lorinescu
Copy link

@lorinescu lorinescu commented Apr 25, 2023

Fix for #502

@lorinescu lorinescu force-pushed the fix-lineprotocolfilter-truncation branch from 32604a1 to bd47464 Compare April 25, 2023 09:19
Copy link
Contributor

@davidby-influx davidby-influx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR to the influx-cli project! Sorry it has taken so long to review it.

I have a few suggestions for the code which will improve readability, maintainability, and possibly efficiency. If you would like to make the modifications, I will happily re-review. Thanks, again!

continue
bytesRead, err := state.lineReader.Read(state.line)
if err != nil && bytesRead == 0 {
return 0, err
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

@@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4096 should be the defaultBufSize constant, not a literal

Copy link
Contributor

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.

@@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4096 should be the defaultBufSize constant, not a literal

Copy link
Contributor

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 not necessary.


//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:])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code ignores any errors from lineReader.Read. Errors need to be handled and returned up the stack.

return 0, nil
}

copy(b, state.line[0:bytesRead])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code has an extra copy for every read, and sometimes two extra copies on a line wrap.

Are those copies necessary? Would it be possible to do all the reads into the passed in buffer, b, calling the second read (when there is no \n found) b[bytesRead:] or something like that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants