-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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: bufio.Scanner: token too long #19662
Conversation
This commit adds a field to Batcher to configure the maximum allowed line length. If this value is non-zero, the bufio.Scanner buffer will be configured scan lines up to this length. If a line exceeds this length an ErrLineToLong error will be returned. Simplified TestBatcher_read tests and added a test case for this new scenario and to ensure lines exceeding the default bufio.MaxScanTokenSize are allowed.
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.
I'm afraid I didn't have time to finish looking through the code before eod, but here's one small comment in the meantime... (looks good otherwise as far as I saw so far!)
write/batcher.go
Outdated
} | ||
|
||
// limit the initial size of the buffer to a maximum of bufio.MaxScanTokenSize bytes | ||
scanner.Buffer(make([]byte, min(bufio.MaxScanTokenSize, maxLineLength)), maxLineLength) |
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.
Or just scanner.Buffer(nil, maxLineLength)
and let the buffer be grown as needed when lines are read. That way when all lines are short (presumably a common case) we'll use a lot less memory.
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.
Good feedback – consider it done!
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.
Nice find!
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.
LGTM 👍
Closes #19586
This PR fixes two issues.
LineReader
type.write.Batcher
to support line lengths greater than the default 64KiB for a newbufio.Scanner
max-line-length
, to configure the maximum line length. The default is 16MB, which should cover the majority of use cases.