-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: Resolve the error in reading container logs #8965
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
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
| case <-time.After(5 * time.Second): | ||
| errorChan <- fmt.Errorf("message channel blocked") | ||
| 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.
The provided code snippet has an issue where errorChan is written to after it has already been closed, which can lead to a runtime panic if any goroutines attempt to write to it afterwards. The correct approach would be to check whether errorChan is still open before writing to it.
Additionally, there's some redundancy in the use of select, since both conditions inside the select block (<-messageChan) and <-time.After() handle the same message processing logic. This could potentially make the code less readable. A more efficient way would be to have a separate channel specifically for handling timeout errors or using a timer that closes itself directly when a message is read from the other channel within its interval:
func collectLogs(params dto.StreamLog, messageChan chan<- string, errorChan chan<- error) {
scanner := bufio.NewScanner(io.Reader)
go func() {
defer scanner.Close()
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
for message := scanner.Text(); ; message = strings.TrimSpace(scanner.Text()), timer.Reset(5*time.Second) {
select {
case messageChan <- message:
return // Exit loop on successful message transmission
case <-errorChan:
return // Exit loop if channel is closed (e.g., due to context cancellation)
case <-timer.C: // Timeout reached before log message received
errorChan <- fmt.Errorf("message channel blocked")
return
}
}
}()
}This change ensures better resource management by closing the timer upon receiving a valid log message or encountering an immediate error condition. It also removes redundant logic within the loop.
|
wanghe-fit2cloud
left a comment
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
|
/approve |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: wanghe-fit2cloud The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |



No description provided.