fix(server): cap held SSE/long-poll connections + cache referenced asset ids#153
Merged
Conversation
…set ids (#146) Bound concurrently-held SSE (/api/events) and long-poll (/api/comments?wait=N) connections per workspace via maxHoldConnections (default 32, configurable through AppOptions). Over-cap returns 503; slots release exactly once on stream/request abort or normal return. Instant ?wait=0 reads don't count. The platform has no equivalent knob — the DO overload protection and WAF rate limiting are request-rate/queue-depth, not concurrent-held-connection, so a slow SSE flood stays under both yet pins sockets open indefinitely. One workspace is one user, so 32 clears real concurrency (a few viewer tabs + several agents in a multi-agent session) with headroom; a real flood is orders of magnitude bigger. Also index the referenced-asset set used by /a/:id's optimistic-read wait and putAsset's eviction scan: it was re-parsing every post's surfaces+history JSON on each call (full-table scan on every /a/:id miss), now built lazily and maintained incrementally on post create/update, invalidated on remove. Correct because post history is append-only — a ref stays referenced until its whole post is deleted.
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 #146.
What
Two halves of the issue, both in
server/app.ts+ the two stores:1. Connection caps on held SSE + long-poll
/api/events(SSE) and/api/comments?wait=N(long-poll) now share a per-instanceholdConnectionscounter, gated bymaxHoldConnections(default 32, configurable viaAppOptions). Over-cap →503. Each slot releases exactly once via a guardedrelease()wired to stream abort, request abort, and normal return — no leak. Instant?wait=0reads don't count (they don't hold a socket).2. Index the referenced-asset set
referencedAssetIds()(used by/a/:id's optimistic-read wait andputAsset's eviction scan) was re-parsing every post'ssurfaces+historyJSON on each call — a full-table scan on every/a/:idmiss. Now built lazily and maintained incrementally:createPost/updatePostfold new refs in,removePost/removeSession/importBoardinvalidate. Correct because post history is append-only — a ref stays referenced until its whole post is deleted, at which point we recompute from scratch.Why not a Cloudflare platform feature
Researched this — there's no platform equivalent for concurrent-held-connection capping:
server/app.tsis runtime-agnostic by repo invariant.The in-process counter is what Cloudflare's own in-memory-state docs imply you do for custom per-object coordination. Layering the edge WAF rate limiter on top for a deploy is a good follow-up note, but it complements rather than replaces this.
Why 32
One workspace = one user (per AGENTS.md). Real concurrent holds: one SSE per open viewer tab (a user keeps a few) + one long-poll per active agent. A multi-agent session with 5 agents + a few tabs legitimately reaches ~15. 32 clears that with headroom; a real flood is orders of magnitude bigger, so rejecting at 32 vs 16 makes no difference to flood protection — only to legitimate use. Configurable for anyone who needs more.
Tests
SqlStoreandJsonFileStore): removed-post invalidation, update-keeps-history-referenced, cold-cache unreferenced.Validation
npm run typecheck(3 programs) ·npm run lint·npm run format:check·npm test— all green (270/270 on this branch; +9 from the new tests).