Skip to content

fix(subscriptions): apply the WebSocket client and token caps per caller - #3420

Open
angela-helios wants to merge 8 commits into
mainfrom
fix/3077-ws-caps-per-caller
Open

angela-helios wants to merge 8 commits into
mainfrom
fix/3077-ws-caps-per-caller

Conversation

@angela-helios

Copy link
Copy Markdown
Contributor

Closes #3077

Stacked on PR #3417 (#2723, which contains #3347 and #3322): the diff of this branch includes those commits until they merge.

Behaviour change to review

  • The limit of 16 bound WebSocket clients and 16 unused binding tokens is now per caller (issuer and subject of the access token) for each subscription. A subscription has a separate total of 256 of each. A subscription could have 16 of each before, so it can now hold more connections; notification bytes are still shared between clients.
  • The total of 256 is a choice made in this change; the issue does not give a number.
  • With authentication disabled all callers are one caller and share its 16, as before. Any peer that can reach the server can still use them up in that case. The README now says so.
  • The text of the 429 from $get-ws-binding-token names both limits.

Summary

Both limits were shared by every caller that can request a token for a Subscription, so one caller could take every place and keep the Subscription's owner from obtaining a token or binding a client.

  • A binding token records who requested it. The client bound with it counts against that caller.
  • WsBindingTokenManager::generate_token_for and WebSocketManager::register_client_for take the caller's key and apply the per-caller limit, then the total. register_client_for also takes the credential deadline and replaces register_client_until from fix(subscriptions): close a bound websocket stream when its token expires #3322, so one call applies both checks. generate_token and register_client remain for callers without an identity, who share one allowance.
  • validate_and_consume returns a WsBinding (tenant, subscription, requester) instead of a pair.
  • The caller's key is the existing UserKey, so two principals never share one.

Not covered: 16 or more principals acting together can still fill the total of 256 (reserving places for the writer needs #3021). Readers that bind still receive the notifications (#3018, #3023). There is no operator path to drop a subscription's clients (#2816).

Tests

  • Unit tests in ws_token.rs and ws_manager.rs: the per-caller limit, a caller at its limit not refusing another, the total, and a client with both a caller and a deadline held to each.
  • crates/rest/tests/ws_binding_caps_per_caller.rs over HTTP and a real WebSocket: a caller with 16 unused tokens gets 429 while another gets a token; a caller with 16 bound clients is closed with 1008 too-many-clients while another binds; callers without an identity share one allowance.
  • Existing websocket_integration, ws_bind_credential_expiry, ws_client_deadlines, ws_unbound_connection_cap, ws_bind_message_size and subscription_operation_route_type_live.

Results:

  • cargo test -p helios-subscriptions --lib: 259 passed. --test websocket_integration: 9 passed.
  • cargo test -p helios-rest --lib with the subscriptions feature: 1149 passed.
  • --test ws_binding_caps_per_caller: 3 passed. --test ws_client_deadlines: 5 passed. --test subscription_events: 5 passed. --test ws_bind_credential_expiry, --test ws_unbound_connection_cap and --test ws_bind_message_size: 3 passed each.
  • I did not run clippy locally; CI runs it.

These ran on current main with the branches this one is stacked on.

…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
…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
The caps of 16 bound clients and 16 unused binding tokens were shared by
every caller of a subscription, so one caller could keep its owner from
binding. A binding token now records who requested it, and both caps apply
per issuer and subject, with a total of 256 per subscription. Callers
without an identity share one allowance.

Closes #3077
@claude

claude Bot commented Dec 2, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.


This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The per-subscription WebSocket client and binding-token caps let any caller that can request a token lock the Subscription's owner out

1 participant