Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion agent/app/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ func collectLogs(params dto.StreamLog, messageChan chan<- string, errorChan chan
message := scanner.Text()
select {
case messageChan <- message:
case <-time.After(time.Second):
case <-time.After(5 * time.Second):
errorChan <- fmt.Errorf("message channel blocked")
return
}
Copy link
Member

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.

Expand Down
Loading