diff options
| author | bndw <ben@bdw.to> | 2026-02-14 22:04:19 -0800 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2026-02-14 22:04:19 -0800 |
| commit | a98e6b9f844b980d27097be68886ec9faa6d90e8 (patch) | |
| tree | 8db0cc7e8d0a00ff5ea339c3c5cebdf1b05a84ad /internal/handler/websocket/handler.go | |
| parent | 32ca0fba5108d0dc2c7415f36e55f031d5a0562e (diff) | |
fix: track WebSocket connections in metrics
Connection metrics were defined but never called. Add IncrementConnections
and DecrementConnections to MetricsRecorder interface and call them when
WebSocket connections are established/closed.
- Add connection methods to MetricsRecorder interface
- Increment on successful WebSocket upgrade
- Decrement in defer when connection closes
- Dashboard will now show active connection count
Diffstat (limited to 'internal/handler/websocket/handler.go')
| -rw-r--r-- | internal/handler/websocket/handler.go | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/internal/handler/websocket/handler.go b/internal/handler/websocket/handler.go index a7b73ec..cb089b1 100644 --- a/internal/handler/websocket/handler.go +++ b/internal/handler/websocket/handler.go | |||
| @@ -26,6 +26,8 @@ type AuthStore interface { | |||
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | type MetricsRecorder interface { | 28 | type MetricsRecorder interface { |
| 29 | IncrementConnections() | ||
| 30 | DecrementConnections() | ||
| 29 | IncrementSubscriptions() | 31 | IncrementSubscriptions() |
| 30 | DecrementSubscriptions() | 32 | DecrementSubscriptions() |
| 31 | SetActiveSubscriptions(count int) | 33 | SetActiveSubscriptions(count int) |
| @@ -114,6 +116,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |||
| 114 | } | 116 | } |
| 115 | defer conn.Close(websocket.StatusNormalClosure, "") | 117 | defer conn.Close(websocket.StatusNormalClosure, "") |
| 116 | 118 | ||
| 119 | // Track connection metrics | ||
| 120 | if h.metrics != nil { | ||
| 121 | h.metrics.IncrementConnections() | ||
| 122 | } | ||
| 123 | |||
| 117 | ctx := r.Context() | 124 | ctx := r.Context() |
| 118 | clientSubs := make(map[string]*subscription.Subscription) | 125 | clientSubs := make(map[string]*subscription.Subscription) |
| 119 | state := &connState{ | 126 | state := &connState{ |
| @@ -121,6 +128,12 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |||
| 121 | } | 128 | } |
| 122 | 129 | ||
| 123 | defer func() { | 130 | defer func() { |
| 131 | // Decrement connection count | ||
| 132 | if h.metrics != nil { | ||
| 133 | h.metrics.DecrementConnections() | ||
| 134 | } | ||
| 135 | |||
| 136 | // Clean up subscriptions | ||
| 124 | count := len(clientSubs) | 137 | count := len(clientSubs) |
| 125 | for subID := range clientSubs { | 138 | for subID := range clientSubs { |
| 126 | h.subs.Remove(subID) | 139 | h.subs.Remove(subID) |
