fix(backend): fix memory leaks and resource issues in logs system
CI / build (push) Successful in 1m18s
CI / build (push) Successful in 1m18s
- Fix Subscribe overwriting channel without closing old one (goroutine leak) - Add context.Context to Stream() for cancellation support - Add WebSocket read pump to detect dead clients (StreamLogs, StreamSteamCMDLogs, StreamRPTLogs) - Use defer f.Close() pattern in StreamRPTLogs file handling - Fix findLatestRPT nil dereference on failed os.Stat - Optimize findLatestRPT to collect stat results before sorting - Apply gofmt formatting to modlists.go, settings.go
This commit is contained in:
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"sync"
|
||||
@@ -26,6 +27,10 @@ func (ls *LogStreamer) Subscribe(serverID, clientID string) chan string {
|
||||
ls.subs[serverID] = make(map[string]chan string)
|
||||
}
|
||||
|
||||
if old, ok := ls.subs[serverID][clientID]; ok {
|
||||
close(old)
|
||||
}
|
||||
|
||||
ch := make(chan string, 256)
|
||||
ls.subs[serverID][clientID] = ch
|
||||
return ch
|
||||
@@ -46,14 +51,23 @@ func (ls *LogStreamer) Unsubscribe(serverID, clientID string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (ls *LogStreamer) Stream(serverID string, reader io.Reader, closeMsg string) {
|
||||
func (ls *LogStreamer) Stream(ctx context.Context, serverID string, reader io.Reader, closeMsg string) {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
}
|
||||
line := scanner.Text()
|
||||
ls.broadcast(serverID, line)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Printf("log stream error for server %s: %v", serverID, err)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
default:
|
||||
log.Printf("log stream error for server %s: %v", serverID, err)
|
||||
}
|
||||
}
|
||||
if closeMsg != "" {
|
||||
ls.broadcast(serverID, closeMsg)
|
||||
|
||||
Reference in New Issue
Block a user