Skip to content

feat(api): cap concurrent WebSocket connections per user - #73

Merged
vianbas merged 5 commits into
mainfrom
feat/72-ws-connection-cap
Nov 19, 2026
Merged

vianbas merged 5 commits into
mainfrom
feat/72-ws-connection-cap

Conversation

@vianbas

@vianbas vianbas commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #65, which introduces /ws. The diff shows only this change. After #65 merges, retarget this PR to main; GitHub does it automatically if the #65 branch is deleted.

Summary

This bounds how many concurrent /ws event streams a single user can hold. Until now, one valid token could open an unbounded number of streams. Each stream holds a goroutine pair, a 64-event buffer and a hub subscription.

Closes #72

Architecture summary

  • Contract first: contracts/asyncapi.yaml (0.2.0 → 0.3.0) documents that a handshake beyond WS_MAX_CONNECTIONS_PER_USER is rejected with HTTP 429 TOO_MANY_CONNECTIONS before the upgrade.
  • realtime/httpapi.connLimiter: a mutex-guarded map[userID]int.
    • The /ws handler reserves a slot after token verification and before websocket.Accept.
    • defer release frees the slot however the stream ends: client close, slow-consumer drop, token expiry, shutdown, or a failed upgrade such as a 403 for a disallowed origin.
    • The key is the verified token subject (user_id). A client cannot choose it.
  • Config: WS_MAX_CONNECTIONS_PER_USER, default 5, validated to be at least 1. It is added to .env.example and docker-compose.yml.
  • Docs: the threat model (DoS row and the realtime section) and container-design.md.

Security considerations

  • Rejection cost: a rejected handshake gets 429 before the upgrade, so it allocates no WebSocket resources.
  • Logging: the server logs realtime: connection limit reached with user_id only. No tokens and no emails are logged.
  • In-process limit: the limiter is in-process, matching the single-instance deployment in ADR 0003.
  • Not covered here: global and per-IP caps and POST /login rate limiting stay in the threat model's Planned column.

Testing evidence

$ make verify                       # EXIT 0
$ go test -race -count=3 ./internal/realtime/... ./internal/config/... ./cmd/...   # ok
$ npx @asyncapi/cli validate contracts/asyncapi.yaml   # valid
$ git diff --check                  # clean
  • Limiter unit tests:
    • Enforces the per-user maximum, keeps each user's budget separate, and frees slots on release.
    • Under concurrency (100 goroutines, max 5), exactly 5 slots are granted.
  • Handler tests run through the real net/http and WebSocket stack:
    • The (N+1)th connection gets 429 with code TOO_MANY_CONNECTIONS.
    • Another user is unaffected.
    • Closing a stream frees its slot, and the next dial succeeds.
    • A 403 from a disallowed origin does not leak a slot.
  • Mutation check: with the limit check disabled, both the handler test and the limiter test fail (the third dial got 101). Restoring the check makes them pass.
  • Manual end-to-end against the real API with WS_MAX_CONNECTIONS_PER_USER=2:
  • Config tests: default 5, an override, and rejection of 0 and of non-integer values.

Known limitations

Follow-up issues

  • Global or per-IP WebSocket caps, and rate limiting on POST /login (threat model, Planned).

Checklist

Handshakes beyond WS_MAX_CONNECTIONS_PER_USER get HTTP 429 before the
upgrade.

Refs #72

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xzt3HRqVzoHnEyEUbTT3h9
- connLimiter counts active /ws streams per verified token subject.
- The slot is reserved after token verification and before the upgrade;
  over the limit the handshake gets 429 TOO_MANY_CONNECTIONS, so a
  rejected client costs no WebSocket resources.
- The slot is released when the stream ends for any reason, including a
  failed upgrade.
- WS_MAX_CONNECTIONS_PER_USER config (default 5, must be >= 1).

Refs #72

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xzt3HRqVzoHnEyEUbTT3h9
Refs #72

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xzt3HRqVzoHnEyEUbTT3h9
Base automatically changed from claude/keen-hawking-qpa724 to main November 19, 2026 00:26
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xzt3HRqVzoHnEyEUbTT3h9
Resolve the threat-model conflict with RBAC (#69): keep the per-user
WebSocket cap in the denial-of-service row and the server-side role check
in the elevation-of-privilege row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013Yguxp4QdXefV36UWrij2T
@vianbas
vianbas merged commit e810e3c into main Nov 19, 2026
8 checks passed
@vianbas
vianbas deleted the feat/72-ws-connection-cap branch November 19, 2026 00:34
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.

feat(api): cap concurrent WebSocket connections per user

2 participants