-
Notifications
You must be signed in to change notification settings - Fork 18.7k
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
Use bufio.Reader instead of bufio.Scanner for logger.Copier #13564
Conversation
you are awesome :) |
reader := bufio.NewReader(src) | ||
|
||
for { | ||
line, err := reader.ReadString('\n') |
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.
How about ReadBytes
?
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.
Oh yeah, good point, I forgot that one existed. Will update.
1ace45e
to
4c515fe
Compare
return | ||
} | ||
logrus.Errorf("Error scanning log stream: %s", err) | ||
return |
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 think you can remove the double return by doing something like:
if err != io.EOF {
logrus.Errorf("Error scanning log stream: %s", err)
}
return
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 have this sense that comparing nil
with io.EOF
at runtime causes a panic but I can't remember why... I'll give it a shot.
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.
Oh, yeah, nevermind, I see what you're saying.
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.
but you compared that err != nil
in the line before.
LGTM |
I think this can go into 1.7, but feel free to remove it from the milestone. |
ping @unclejack |
This closes #13333 |
reader := bufio.NewReader(src) | ||
|
||
for { | ||
lineAndNewline, err := reader.ReadBytes('\n') |
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 kinda liked your lightweight solution with slicing. I think might be:
line, err := reader.ReadBytes('\n')
line = line[:len(line)-1]
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.
So the problem I had, and the reason I changed the commit, is that ReadBytes
can return an error and a partial output. I haven't tested this, but I'm assuming that if it returns a partial input, it will end with a valuable byte and not a newline -- so it's probably prudent to only trim the trailing character if it's a newline.
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.
Okay, anyway then you should reuse one variable :)
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.
Works for me. Changed.
When using a scanner, log lines over 64K will crash the Copier with bufio.ErrTooLong. Subsequently, the ioutils.bufReader will grow without bound as the logs are no longer being flushed to disk. Signed-off-by: Burke Libbey <burke.libbey@shopify.com>
LGTM |
Use bufio.Reader instead of bufio.Scanner for logger.Copier
cherry-picked |
When using a scanner, log lines over 64K will crash the Copier with bufio.ErrTooLong. Subsequently, the ioutils.bufReader will grow without bound as the logs are no longer being flushed to disk.
For us, this manifested as some background workers occasionally stopping log writes, and slow memory growth between deploys.