feat: add heartbeat to detect and clean up dead WS connections - #163
Open
Paranoa-dev wants to merge 1 commit into
Open
feat: add heartbeat to detect and clean up dead WS connections#163Paranoa-dev wants to merge 1 commit into
Paranoa-dev wants to merge 1 commit into
Conversation
- Add periodic ping/pong heartbeat to IntentsGateway using the standard ws library pattern - Track alive status per client with a WeakMap - Every 30s, mark all clients as 'not alive', send ping; clients that respond with pong are marked alive again; those that don't are terminated and removed from the subscriber set - Implement OnModuleDestroy to clean up the heartbeat interval - Add unit tests for heartbeat behavior (alive marking, pong response, dead client termination, interval cleanup) - Fix e2e config for pre-existing test environment issues Closes stellar-vortex-protocol#81
|
@Paranoa-dev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add heartbeat ping/pong to detect dead WebSocket connections
Summary
Adds a periodic heartbeat mechanism to
IntentsGatewaythat detects and cleans up dead WebSocket connections (e.g., network drops) that would otherwise remain in the subscribers set indefinitely, silently wastingbroadcast()cycles.Problem
IntentsGatewayonly removes a subscriber on an expliciterrororcloseevent. A connection that goes dead without a clean close (e.g., network drop, mobile sleep, load balancer timeout) stays in thesubscribersset indefinitely, causing:broadcast()cycles sending to a socket that will never receiveSolution
Implemented the standard ws library heartbeat pattern:
How it works
alive = trueand aponglistener is registeredHEARTBEAT_INTERVAL_MS):alive === false→ if so,client.terminate()and remove from setalive = falseand sendpingalive = trueagainImplementation details
WeakMap<WebSocket, boolean>to track alive status (avoids monkey-patching the WebSocket instance)OnModuleDestroyinterface ensures cleanup on application shutdowngetAliveCount()exposed for observability/monitoringCode changes
src/intents/intents.gateway.ts:WeakMap<WebSocket, boolean>for alive trackingsetInterval-based heartbeat loop in constructorpongevent handler inhandleConnectionheartbeat()private method with terminate logiconModuleDestroy()for cleanupgetAliveCount()for observabilitysrc/intents/intents.gateway.spec.ts(new):Test Results
Migration Notes
When scaling to multiple instances, the heartbeat pattern remains the same since each instance maintains its own WebSocket connections. No cross-instance coordination is needed.
Checklist
npm run lintpassesnpm run typecheckpasses (pre-existing errors only)npm testpassesnpm run test:e2epassesCloses #81