mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
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:
+14
-6
@@ -593,6 +593,19 @@ func (a *App) setupServer() error {
|
|||||||
return nil
|
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 {
|
func (a *App) Run() error {
|
||||||
// Cancellable worker context: aggregation + stats broadcaster goroutines
|
// Cancellable worker context: aggregation + stats broadcaster goroutines
|
||||||
// watch ctx.Done() and exit, so Shutdown can stop them before closing
|
// watch ctx.Done() and exit, so Shutdown can stop them before closing
|
||||||
@@ -601,12 +614,7 @@ func (a *App) Run() error {
|
|||||||
a.cancel = cancel
|
a.cancel = cancel
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// Start download data aggregation worker (runs every hour)
|
a.startWorkers(ctx)
|
||||||
go a.startAggregationWorker(ctx)
|
|
||||||
|
|
||||||
// Start periodic stats broadcaster (every 30s) so connected WS clients
|
|
||||||
// see live counter updates without polling.
|
|
||||||
go a.startStatsBroadcaster(ctx)
|
|
||||||
|
|
||||||
// Start Fiber server in goroutine. Branch on TLS to pick Listen vs ListenTLS.
|
// Start Fiber server in goroutine. Branch on TLS to pick Listen vs ListenTLS.
|
||||||
errChan := make(chan error, 1)
|
errChan := make(chan error, 1)
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -62,6 +62,14 @@ var (
|
|||||||
[]string{"reason"}, // ttl, lru, manual
|
[]string{"reason"}, // ttl, lru, manual
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// WebSocketEventsDropped counts realtime events dropped because the broadcast channel was full.
|
||||||
|
WebSocketEventsDropped = promauto.NewCounter(
|
||||||
|
prometheus.CounterOpts{
|
||||||
|
Name: "gohoarder_websocket_events_dropped_total",
|
||||||
|
Help: "Realtime dashboard events dropped because the broadcast channel was full",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
// Storage metrics
|
// Storage metrics
|
||||||
StorageOperations = promauto.NewCounterVec(
|
StorageOperations = promauto.NewCounterVec(
|
||||||
prometheus.CounterOpts{
|
prometheus.CounterOpts{
|
||||||
|
|||||||
+26
-1
@@ -7,10 +7,13 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/lukaszraczylo/gohoarder/pkg/metrics"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EventType represents the type of event being broadcast
|
// EventType represents the type of event being broadcast
|
||||||
@@ -59,6 +62,8 @@ type Server struct {
|
|||||||
unregister chan *Client
|
unregister chan *Client
|
||||||
upgrader websocket.Upgrader
|
upgrader websocket.Upgrader
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
|
dropped atomic.Uint64
|
||||||
|
lastWarn atomic.Int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config holds WebSocket server configuration
|
// Config holds WebSocket server configuration
|
||||||
@@ -222,10 +227,30 @@ func (s *Server) Broadcast(eventType EventType, data map[string]interface{}) {
|
|||||||
select {
|
select {
|
||||||
case s.broadcast <- event:
|
case s.broadcast <- event:
|
||||||
default:
|
default:
|
||||||
log.Warn().Msg("Broadcast channel full - dropping event")
|
s.recordDrop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dropWarnInterval bounds the drop warning to one line per interval, so a burst does not flood the log.
|
||||||
|
const dropWarnInterval = time.Minute
|
||||||
|
|
||||||
|
// recordDrop counts a dropped event and warns at most once per dropWarnInterval.
|
||||||
|
func (s *Server) recordDrop() {
|
||||||
|
total := s.dropped.Add(1)
|
||||||
|
metrics.WebSocketEventsDropped.Inc()
|
||||||
|
now := time.Now().UnixNano()
|
||||||
|
last := s.lastWarn.Load()
|
||||||
|
if now-last < int64(dropWarnInterval) || !s.lastWarn.CompareAndSwap(last, now) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Warn().Uint64("dropped_total", total).Msg("Broadcast channel full - dropping events")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dropped returns how many events were dropped because the broadcast channel was full.
|
||||||
|
func (s *Server) Dropped() uint64 {
|
||||||
|
return s.dropped.Load()
|
||||||
|
}
|
||||||
|
|
||||||
// BroadcastEvent satisfies the events.Broadcaster contract used by
|
// BroadcastEvent satisfies the events.Broadcaster contract used by
|
||||||
// cache/scanner sub-systems. It accepts a string event type and an
|
// cache/scanner sub-systems. It accepts a string event type and an
|
||||||
// arbitrary payload (typically map[string]any) and forwards to the
|
// arbitrary payload (typically map[string]any) and forwards to the
|
||||||
|
|||||||
Reference in New Issue
Block a user