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
+21
View File
@@ -152,4 +152,25 @@ describe("PayStation.activeSessions", () => {
expect(list.map((s) => s.identity)).toEqual(["NEW", "OLD"]);
expect(list.every((s) => s.open)).toBe(true);
});
// The list prices each open transient from ONE whole-ledger scan (no per-row ledger
// reads — those froze park-2's server, 2026-10-24). The amounts must match the
// single-session quote() exactly, including after a payment (overstay repricing) and
// for a subscription (never quoted).
it("prices every row identically to quote() without per-session ledger reads", async () => {
seedTariff(db, { pricePerIncrementMinor: 10000, incrementMin: 60, gracePeriodExitMin: 0 });
await enter("UNPAID", minutesAgo(150));
await enter("PAID", minutesAgo(300));
await pay.pay("PAID", "cash"); // grace 0 → paid-but-open ⇒ overstay, re-priced from now
await enter("SUB", minutesAgo(10), { permit: true, permitId: "sub-1" });
const list = pay.activeSessions();
const by = new Map(list.map((s) => [s.identity, s]));
expect(by.get("UNPAID")!.amountMinor).toBe(pay.quote("UNPAID").amountMinor);
expect(by.get("UNPAID")!.currency).toBe(pay.quote("UNPAID").currency);
expect(by.get("PAID")!.overstay).toBe(true);
expect(by.get("PAID")!.amountMinor).toBe(pay.quote("PAID").amountMinor);
expect(by.get("SUB")!.subscription).toBe(true);
expect(by.get("SUB")!.amountMinor).toBeNull();
});
});