-
-
Notifications
You must be signed in to change notification settings - Fork 614
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
Refactor log-validator to break up the big main function. #7170
Conversation
The main function in log-validator is overly big, so this refactors it in preparation for adding support for file globs. Largely it's functionally identical, except for one change: The 1-per-second ratelimiter is moved to be per-file, so it's 1-per-second-per-file. I think this more closely aligns with what we'd want, as we could potentially miss that a file had bad lines in it if it was overwhelmed by another log file at the same time.
|
||
// ValidateFile validates a single file and returns | ||
func ValidateFile(filename string) error { | ||
file, err := os.ReadFile(filename) |
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.
Consider reading the file line-by-line instead of loading it all into memory:
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
badFile := false
lineNum := 0
for scanner.Scan() {
lineNum++
line := scanner.Text()
...
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, I almost forgot, the scanner has one part that's a little wonky:
// Check for any errors that occurred during the scan.
if err := scanner.Err()
if err != nil {
return fmt.Errorf("error reading file: %w", err)
}
You'll want to do this after you've finished scanning a file, not inside of the loop.
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 actually have an idea to just get rid of this function completely: it’s not used at all during the regular operation of log-validator so is extra code duplication, but I didn’t want to make too many changes while moving things around. This function is currently unmodified from the source. So I’d like to just leave it as-is for now and eliminate it in the next patch in this series.
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.
This looks good to me, and I like doing splitups separately from substantive changes.
The main function in log-validator is overly big, so this refactors it in preparation for adding support for file globs, which is started in the (currently draft) #7134
This PR should be functionally a no-op, except for one change:
The 1-per-second ratelimiter is moved to be per-file, so it's 1-per-second-per-file. I think this more closely aligns with what we'd want, as we could potentially miss that a file had bad lines in it if it was overwhelmed by another log file at the same time.