Index socket clients by system; make /systems cheap

A CPU profile during a traffic spike (4,850 sockets, 74 uploads/s) showed
the backend spending ~28% of its busy time in two loops over every
connected socket: notify_clients on each upload, and addSystemClients on
each /systems request.

- Add client_registry.js: sockets in a Map, with active clients indexed
  by system plus a firehose set. notify_clients only visits the
  uploaded call's system, with the same matching rules as before.
- /systems: counts come from the index, the serialized body is reused
  for 5s, and counts now reset to 0 when a system's listeners leave.
  Firehose clients no longer count toward a system.
- nginx: 10s microcache for /systems on api.*.
- Test: node --test test/client_registry.test.js checks recipients and
  counts against the old full-scan implementation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Luke Berndt
2026-11-20 13:22:31 -05:00
parent 5b177eca58
commit a00bc98cc4
5 changed files with 403 additions and 116 deletions

View File

@@ -142,6 +142,25 @@ server {
access_log off;
}
# System list for the homepage: listener counts can lag a few seconds.
location = /systems {
proxy_pass http://backend_api;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
include /etc/nginx/proxy.conf;
proxy_cache api_cache;
proxy_cache_key "$scheme$host$request_uri|$http_origin";
proxy_cache_methods GET HEAD;
proxy_cache_valid 200 10s;
proxy_cache_lock on;
proxy_cache_lock_timeout 5s;
proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
add_header X-Cache-Status $upstream_cache_status always;
access_log off;
}
location / {
proxy_pass http://backend_api;
proxy_http_version 1.1;