fix(websocket): start the event loop and throttle drop warnings

The app built the websocket server but never called Start, so nothing read the 256-slot broadcast channel. After the first 256 events every event was dropped with a warning, and dashboard clients blocked forever on the unbuffered register channel, leaking a goroutine per connection and never receiving events. Run now starts the loop with the other workers. Dropped events are counted in gohoarder_websocket_events_dropped_total and warned about at most once a minute.
This commit is contained in:
2026-11-23 12:56:32 +00:00
parent c98720c02e
commit 6f604367da
4 changed files with 78 additions and 7 deletions
+14 -6
View File
@@ -593,6 +593,19 @@ func (a *App) setupServer() error {
return nil
}
// startWorkers starts the background loops that live as long as ctx.
func (a *App) startWorkers(ctx context.Context) {
// Without the event loop the broadcast channel fills after 256 events and websocket clients never register.
a.wsServer.Start(ctx)
// Start download data aggregation worker (runs every hour)
go a.startAggregationWorker(ctx)
// Start periodic stats broadcaster (every 30s) so connected WS clients
// see live counter updates without polling.
go a.startStatsBroadcaster(ctx)
}
func (a *App) Run() error {
// Cancellable worker context: aggregation + stats broadcaster goroutines
// watch ctx.Done() and exit, so Shutdown can stop them before closing
@@ -601,12 +614,7 @@ func (a *App) Run() error {
a.cancel = cancel
defer cancel()
// Start download data aggregation worker (runs every hour)
go a.startAggregationWorker(ctx)
// Start periodic stats broadcaster (every 30s) so connected WS clients
// see live counter updates without polling.
go a.startStatsBroadcaster(ctx)
a.startWorkers(ctx)
// Start Fiber server in goroutine. Branch on TLS to pick Listen vs ListenTLS.
errChan := make(chan error, 1)
+30
View File
@@ -0,0 +1,30 @@
package app
import (
"context"
"testing"
"time"
)
// TestWorkersDrainTheBroadcastChannel pins that the app starts the websocket
// event loop: without it the 256-slot broadcast channel fills and every later
// event is dropped.
func TestWorkersDrainTheBroadcastChannel(t *testing.T) {
a := newTestApp(t, nil)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
a.startWorkers(ctx)
const events = 1000
for i := 0; i < events; i++ {
a.wsServer.BroadcastEvent("cache_hit", map[string]interface{}{"i": i})
if i%200 == 0 {
time.Sleep(5 * time.Millisecond)
}
}
if dropped := a.wsServer.Dropped(); dropped != 0 {
t.Fatalf("dropped %d of %d events; the broadcast loop is not draining the channel", dropped, events)
}
}