fix(server): bound active-sessions cost, index hot paths, no phantom relay timeouts
Build & push images / images (push) Successful in 3m45s

park-2 (2026-10-24): GET /api/sessions/active ran ~2 s synchronously — PayStation.activeSessions
called quote() per open unpaid transient (664 rows), each 3-4 unindexed ledger_events scans by
identity — 2234x/day. The frozen event loop made the Dingtian driver's 2 s timer fire before the
already-received UDP reply (timers run before poll I/O), so healthy pulses logged "timeout"; the
subscription flow then unwound before firing the camera or writing the sessions row, leaving
signed entries with no snapshot. Cost grew ~120 ms/day with the operator's open-ticket count.

- pay-station: price every list row from the one ledger scan (rows grouped per identity ->
  #quoteFromRows); tariff resolver built once per request; quote(identity) wraps the same core.
  validations: sessionValidationsFromRows / liveValidationsFromRows. Test pins list == quote().
- db 0032_hot_path_indexes: ledger_events(identity,index), device_events(category,kind,
  occurred_at), device_events(kind,occurred_at), snapshots(identity), sessions(state),
  sessions(subscription_id,state). Additive.
- access-dingtian binaryUdp: defer the timeout rejection with setImmediate so a datagram that
  already arrived wins over an expired timer.
- subscription-flow: snapshot fired and session cache written/closed BEFORE the pulse.

wiki/concepts/park-2-event-loop-stall.md has the full analysis.
This commit is contained in:
2026-10-24 11:17:30 +02:00
parent 6eb65361a8
commit 73ada89a13
7 changed files with 142 additions and 26 deletions
@@ -77,7 +77,13 @@ function binaryUdp(
sock.close();
err ? reject(err) : resolve(val!);
};
const timer = setTimeout(() => done(new Error("timeout"), null), timeoutMs);
// On expiry, defer the rejection by one loop turn. Node runs expired timers BEFORE
// it drains pending socket I/O, so if the event loop was blocked (a long synchronous
// handler) while the device's reply sat in the kernel buffer, the timer would win
// and report "timeout" for a relay that answered. The setImmediate lands in the
// check phase, after the poll phase has delivered that datagram to `message`.
// (park-2, 2026-10-24: ~2 s handler stalls produced exactly these phantom timeouts.)
const timer = setTimeout(() => setImmediate(() => done(new Error("timeout"), null)), timeoutMs);
sock.on("error", (e) => done(e, null));
sock.on("message", (m) => done(null, m));
// Bind to a specific local address (the device-facing NIC) on multi-homed