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

@@ -15,6 +15,12 @@ const mailjet = new Mailjet({
var systemList = [];
// Cached /systems response body; rebuilt when stale or when load_systems swaps the list.
const SYSTEMS_BODY_TTL_MS = 5000;
let systemsBody = null;
let systemsBodyList = null;
let systemsBodyTime = 0;
exports.contact_system = async function (req, res) {
var system = await System.findOne({
shortName: req.params.shortName.toLowerCase()
@@ -181,19 +187,24 @@ exports.get_systems = async function (req, res) {
await load_systems(req.systemClients);
}*/
const systemClients = req.systemClients || {};
for (var i = 0; i < systemList.length; i++) {
var system = systemList[i];
var shortName = system.shortName;
if (systemClients.hasOwnProperty(shortName)) {
system.clientCount = systemClients[shortName];
// The list is a few hundred systems and the homepage requests it constantly,
// so serialize it at most every few seconds instead of on every request.
const now = Date.now();
if (!systemsBody || systemsBodyList !== systemList || now - systemsBodyTime > SYSTEMS_BODY_TTL_MS) {
const systemClients = req.systemClients || {};
for (var i = 0; i < systemList.length; i++) {
var system = systemList[i];
system.clientCount = systemClients[system.shortName] || 0;
}
systemsBody = JSON.stringify({
success: true,
systems: systemList
});
systemsBodyList = systemList;
systemsBodyTime = now;
}
res.contentType('json');
res.send(JSON.stringify({
success: true,
systems: systemList
}));
res.set('Content-Type', 'application/json; charset=utf-8');
res.send(systemsBody);
}
exports.authorize_system = async function (req, res) {