Repository navigation
fix(rest): send large WebSocket notifications as bounded frames - #3422
Open
angela-helios wants to merge 7 commits into
Open
angela-helios wants to merge 7 commits into
angela-helios wants to merge 7 commits into
Conversation
…ires The bind handler authorized a client once at the upgrade and then streamed to it for as long as the connection lived. It now captures the expiry of the request's bearer token and closes the stream with 1008 credential-expired at that time. The websocket manager keeps the deadline with each client and drops a client past it on the next broadcast or bind. Without authentication there is no token and no deadline. Closes #2816
/ws/subscriptions/bind upgraded every request, so a peer could hold any number of connections that never send a bind message, and the memory they used stayed in the allocator arenas after they closed. The engine now holds one slot per connection waiting to bind (HFS_SUBSCRIPTION_WS_MAX_UNBOUND_CONNECTIONS, default 256). An upgrade past the limit is refused with 503 and Retry-After. The slot is given back when the bind message arrives, the connection closes, or the bind deadline passes. Every 64 closed connections the allocator is asked to return free memory to the OS. Closes #3069
…ap' into fix/2723-wip
…expiry' into fix/2723-wip # Conflicts: # crates/rest/src/handlers/ws.rs
…d client's reason Every close frame is sent within 5 seconds or given up on. A bound client that misses the send deadline or overflows its queue is closed with 1008 client-too-slow instead of a dropped connection. The bind deadline, send deadline and client queue size are configurable through HFS_SUBSCRIPTION_WS_*, and the README says what $events can and cannot recover. Closes #2723
…t filling its queue A bound client that accepted every frame inside the send deadline and kept its queue under the cap could hold queued bundles for hours. Each notification must now be delivered within HFS_SUBSCRIPTION_WS_DELIVERY_TIMEOUT_MS (2 minutes by default) of being queued, or the client is closed with 1008 client-too-slow. Closes #3080
tungstenite keeps a write buffer the size of the largest frame it has sent, so every bound connection held a copy of its largest notification. A notification over 64 KiB is now one message in frames of at most 64 KiB, each flushed before the next. axum's WebSocket cannot send frames, so the bind route runs tokio-tungstenite on the upgraded connection. Closes #3078
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
This branch has not been deployed
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.
Closes #3078
This branch is stacked on PR #3421 (#3080), and its diff includes the commits of that branch, and of the branches it is stacked on, until they merge.
Behaviour change to review
helios-restgets direct dependencies ontokio-tungstenite,hyperandhyper-util, at the versions axum already brings in. No package is added toCargo.lock.Summary
tungstenite copies each outgoing frame into a write buffer that it never shrinks, so every bound connection kept a buffer the size of its largest notification.
WebSocketsends whole messages only, so the bind route now runstokio_tungsteniteon the upgraded connection. The request is still validated by axum'sWebSocketUpgrade, and the limits on incoming messages are the same.work-with-subscriptionsskill (both mirrors) describe the fragmentation.A size limit on
full-resourcenotifications, also suggested in the issue, is not part of this change.Tests
crates/rest/tests/ws_large_notifications.rs: reads the frames off the wire and checks that none is over 64 KiB, that they make up the notification and that a small one is a single frame; a client receives large notifications, with frames ending inside a character, as one message.handlers/ws.rs: how a message is split, and that the frames do not copy it.Results:
cargo test -p helios-rest --libwith thesubscriptionsfeature: 1151 passed.--test ws_large_notifications: 2 passed.--test ws_client_deadlines: 7 passed.--test ws_bind_credential_expiry,--test ws_unbound_connection_capand--test ws_bind_message_size: 3 passed each.These ran on current
mainwith the branches this one is stacked on.