feat: backpressure-aware async broadcast with per-client drain queues - #217
Open
ayomidemariam wants to merge 2 commits into
Open
feat: backpressure-aware async broadcast with per-client drain queues#217ayomidemariam wants to merge 2 commits into
ayomidemariam wants to merge 2 commits into
Conversation
- Import createRateLimiter from ./rate-limiter.js - Add safeSend helper for outbound messages - Implement per-message rate limiting before validation - Clean up rate limiter state on disconnect - Remove ipConnectionCount and server from return value - All 18 test suites pass (163 tests)
…ain queues - Add backpressure options to RoomManager constructor (opt-in) - Batch broadcasts using setImmediate to prevent event loop starvation - Detect slow consumers via ws.bufferedAmount high water mark - Coalesce location_update messages for slow consumers - Auto-terminate slow consumers after configurable timeout - Add getRoomStats() for observability (queue depths, slow consumers) - Clean up slow consumer state on disconnect (no memory leaks) - All existing tests pass unchanged (backward compatible) Closes #734 Closes #733
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.
Summary
This PR implements backpressure-aware async broadcasting with per-client drain queues to prevent event loop starvation in large rooms.
Changes
backpressureconfiguration withenabled,highWaterMark,slowConsumerTimeout, andbatchSizeoptionssetImmediateto yield control back to the event loopws.bufferedAmountexceedshighWaterMark(default 1MB) are flagged as slowlocation_updatemessage, dropping intermediate updatesslowConsumerTimeout(default 30s) are terminated viaws.close(4000, "Slow consumer")getRoomStats(roomId)method returns member count, per-client send queue depths, and slow consumer listBackward Compatibility
The backpressure behavior is opt-in. When no options are provided (or
enabled: false),broadcast()behaves identically to the current synchronous implementation. All 30 existing room-manager tests pass unchanged.Testing
Closes #194