Skip to content

fix(api): WS health check should read the live wss client count [BUG-110] - #225

Open
Morenikeoa wants to merge 1 commit into
dcccrypto:mainfrom
Morenikeoa:fix/ws-health-liveness-check
Open

fix(api): WS health check should read the live wss client count [BUG-110]#225
Morenikeoa wants to merge 1 commit into
dcccrypto:mainfrom
Morenikeoa:fix/ws-health-liveness-check

Conversation

@Morenikeoa

Copy link
Copy Markdown

Bug

The /health endpoint's WS-saturation check computes utilization as wsMetrics.totalConnections / maxGlobalConnections. totalConnections is our own bookkeeping counter, incremented only after a connection clears the connection handler's async IP-blocklist/auth-ban-check/per-IP-rate-limit chain (several awaits, including a SharedStore round-trip). A socket that's accepted at the ws library level sits open — and counts against the real connection budget — for that entire window before totalConnections ever reflects it.

Under a burst of concurrent handshakes (exactly the scenario this check exists to catch), totalConnections under-reports true saturation, so the health check can report healthy while the WS subsystem is actually near its connection cap.

Fix

Hold the live WebSocketServer instance created in setupWebSocket() and expose wss.clients.size — the ws library's own real-time client count — as a new liveConnections field from getWebSocketMetrics(). health.ts now prefers liveConnections over totalConnections for the saturation check, falling back to totalConnections if liveConnections is absent (keeps /ws/stats and any other consumer unaffected).

Test plan

  • Added tests/routes/ws-live-metrics.test.ts: spins up a real HTTP + WebSocketServer, mocks the shared store so a connection stalls inside isAuthBanned() (mid-handshake, before clients.add()), and asserts liveConnections reflects the real open socket while totalConnections is still 0. Also covers the steady-state case where both counters agree.
  • Added WS liveness check (BUG-110) block to tests/routes/health.test.ts verifying checks.ws flips to false when liveConnections is saturated even though totalConnections looks healthy, and that the check still works via the totalConnections fallback.
  • Verified both are genuine regressions: reverted src/routes/health.ts + src/routes/ws.ts via git stash, confirmed the new tests fail against the unfixed code, restored the fix.
  • Full suite passes (298/299 — the 1 pre-existing failure is unrelated to this change, in adl.test.ts).
  • tsc --noEmit clean.

…110)

The WS-saturation check in /health divides by wsMetrics.totalConnections,
our own bookkeeping counter, which only increments once a connection
clears the async IP-blocklist/auth-ban/per-IP rate-limit chain in the
"connection" handler. A burst of concurrent handshakes can sit open at
the `ws` library level for that entire await chain, so totalConnections
under-reports true connection-slot pressure exactly when it matters most.

Hold the live WebSocketServer instance and expose wss.clients.size (the
library's own real-time count) as liveConnections; health.ts now uses it
in preference to totalConnections.
@vercel

vercel Bot commented Jun 26, 2026

Copy link
Copy Markdown

@Princessdada is attempting to deploy a commit to the Khubair Nasir's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Morenikeoa, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 5 minutes and 37 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7a959c67-9891-4ae5-a235-543f5268a1d6

📥 Commits

Reviewing files that changed from the base of the PR and between b2751f4 and ab72f6b.

📒 Files selected for processing (4)
  • src/routes/health.ts
  • src/routes/ws.ts
  • tests/routes/health.test.ts
  • tests/routes/ws-live-metrics.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant