mirror of
https://github.com/multica-ai/multica.git
synced 2026-06-17 03:38:32 +02:00
Without a heartbeat, dead or silently-dropped WebSocket connections are not detected until the next write fails. This causes goroutine and memory leaks for each stale client, and breaks real-time updates for users whose connections are dropped by a load balancer or proxy idle timeout (e.g. Nginx default 60s, AWS ALB default 60s) without a TCP RST. This commit applies the standard gorilla/websocket keepalive pattern: - writePump sends a ping frame every pingPeriod (54 s) using a ticker. The ticker replaces the simple range-over-channel loop with a select, which also adds a proper write deadline on every write operation. - readPump installs a pong handler that resets the read deadline on each pong, keeping healthy connections alive indefinitely. A connection that misses a pong is detected within pongWait (60 s) and closed, which causes readPump to exit and send the client to hub.unregister for clean removal. Timing constants: writeWait = 10 s (per-write deadline, prevents hung writers) pongWait = 60 s (max silence before declaring a connection dead) pingPeriod = 54 s (ping interval, 90 % of pongWait) Also adds user_id and workspace_id to the write-error log line so that connection problems can be attributed to a specific client in production. All existing hub tests continue to pass unchanged. Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>