Repository navigation
fix(realtime): cap subscriptions, channels and connections per user - #14945
Conversation
A connection could register any number of subscriptions, channels and queries, and a user could hold any number of open connections, so one client could grow worker memory and stall the event loop for every project on that worker. - At most 100 channels and 100 queries per subscription, on both the message and URL paths - At most 100 subscriptions per connection; re-subscribing an existing id does not count, and the connection stays open when the limit is hit - At most 100 ids per unsubscribe batch - At most 64 open connections per project user on one worker, also enforced when in-band authentication switches the user; guests stay under the per-IP abuse limit Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
This PR introduces
Reviewed commit: 468758b |
🟢 Tier S · Ready to merge
Caps realtime subscriptions, channels, queries, and open connections, with per-connection limits configurable through environment variables. Adds per-project guest connection quotas keyed by client IP and a project policy to deny guest subscriptions while allowing guests to authenticate on an existing connection. Adds response models, API documentation metadata, compose configuration, and unit and end-to-end coverage for these behaviors. Latest changes: The newest commits make subscription, channel, and query limits configurable, add guest connection quotas and the deny-realtime-guests project policy, and update related API models and tests.
📂 Walkthrough · 20
✅ Fixed since the last review · 1
Reviewed the commits since |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
- Subscribing again with an existing id adds its channels, so the per-subscription channel limit now applies to the merged set - The subscription limits are checked on fresh state after the tail lookups, which can yield, so concurrent messages cannot both pass - URL subscriptions and queries are counted in constructSubscriptions before any query is parsed Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Adds per-user realtime connection and subscription limits enforced at connect and subscribe time, with matching validators and e2e/unit coverage.
Key findings
- 🟡 Auth-vs-close race permanently leaks a connection slot — Realtime.php:452
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
The findings that requested changes are resolved. See the summary comment for what is still open.
onOpen and the authentication handler yield on lookups before counting the connection. If the socket closed meanwhile, onClose had already released it and the late count leaked a slot. onOpen now skips sockets that are no longer established, and authentication rejects a connection whose entry onClose has dropped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The websocket client connects on its first receive, so the close helper saw a disconnected client and returned no frames. Also applies Rector's generator data providers and string casts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Limits per subscription multiplied: 100 subscriptions x 100 channels x 100 queries let one connection hold tens of MB. - At most 200 channels and 100 queries across all subscriptions of a connection, on both the message and URL paths - Queries nested in and/or count, and a query takes at most 100 values, so one large query cannot stand in for many - Subscribing again with an id replaces its queries on all of its channels, so each subscription holds one set of queries Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Guests were not counted, so the per-user cap could be skipped by not signing in. Guest connections are now counted per IP, or per IPv6 /64, per project on one worker, with a higher cap than a user since many people can share one address. Signing in moves the count from the address to the user. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The subscription, channel and query totals per connection now come from the plan's realtimeSubscriptions, realtimeChannels and realtimeQueries, falling back to the defaults. Self-hosted has no plan, so nothing changes there. The limits are resolved once when the connection opens and kept on it, including across in-band authentication and permission changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
New project policy deny-realtime-guests. When enabled, a guest that opens a connection with channels is refused, and a guest subscribe message is refused while the connection stays open, so cookie-less web clients can still sign in over the connection and then subscribe. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A value count alone did not bound how long a value could be, so one query could carry a ~50 KB string. Query strings are now limited to APP_LIMIT_ARRAY_ELEMENT_SIZE characters, the same as the REST API, checked before parsing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…limits # Conflicts: # tests/unit/Messaging/MessagingTest.php
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…line Co-Authored-By: Claude Opus 5.5 <[email protected]>
The guest cap test closes 128 connections at once, and those closes can hold up new connections for a few seconds, past the default 2s read timeout. Co-Authored-By: Claude Opus 5.5 <[email protected]>
_APP_LIMIT_REALTIME_SUBSCRIPTIONS, _APP_LIMIT_REALTIME_CHANNELS and _APP_LIMIT_REALTIME_QUERIES set the defaults, falling back to 100, the same way _APP_LIMIT_DATABASE_BATCH works. A plan still overrides them on Cloud. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ptions Authentication rebuilds a connection from its subscriptions. A connection with none, such as a cookie-less client that connects, signs in and only then subscribes, lost its project, roles and user, so its next subscribe ran as a guest. Record them the same way the permission-change path already does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The realtime limits now come from the environment, so the tests read the same constants instead of assuming 100. Cloud CI can then lower them to match a plan and run the CE suite as is. The URL path also checks its channel list against the connection's channel total rather than a fixed 100. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@hansi-codes review |
…tion-limits # Conflicts: # src/Appwrite/Messaging/Adapter/Realtime.php
commit: |
The pkg.vc build predates Videos, so the console no longer built, and lint:sdk-pins refuses pkg.vc on main. Pins the pkg.pr.new build of #14945 instead, rewords the guests card for connections that list channels, and adds a mocked e2e spec for the Realtime policies tab. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What does this PR do?
Adds limits to realtime connections. Nothing capped how many subscriptions, channels or queries a connection could register, or how many connections one user could keep open, so a single client could make a shared realtime worker do unbounded work.
Limits
APP_LIMIT_ARRAY_PARAMS_SIZE)1003error, connection stays open; URL:1008closeAPP_LIMIT_ARRAY_PARAMS_SIZE)APP_LIMIT_REALTIME_SUBSCRIPTIONS)1003errorAPP_LIMIT_REALTIME_CONNECTIONS)1008closeWorst-case memory
The most one worker can be made to hold when every level is maxed out: 100 projects × 100 users × 32 connections, each connection with the most subscriptions and channels allowed. Measured on PHP 8.5 with the real adapter and normal-size queries; subscriptions are stored once per role, so the 20-role column is a user in several teams.
main)A general shape for comparison: 30 projects × 100 users × 5 connections, users with 10 roles, each connection with 30 subscriptions of 5 distinct channels each and 30 queries in total. This is well within the limits, so it costs the same before and after this PR.
Before, a single 64 KB message could add ~21 MB, so one connection could exhaust a worker in minutes. After, one connection has a ceiling, but the per-user and fleet-level totals are still large.
Design notes
projectId:userId, with no per-project cap, so one user can't lock out a project's other users. Guests aren't counted; the existing per-IP abuse limit covers them.onClosereleases it by connection id. In-band authentication moves the count to the new user, and a rejected switch leaves the connection as it was.Test Plan
SubscribePayloadTest,UnsubscribePayloadTest,MessagingTest::testAddConnection*RealtimeCustomClientTest::testConnectionLimitPerUser,testConnectionChannelLimit,RealtimeCustomClientQueryTestWithMessage::testSubscribeLimitsChecklist
🤖 Generated with Claude Code