0205. Owner memory and reads: byte-log catch-up rings and ACK caches, two read lanes, batched lookups; drain over shared memory
Status: Accepted (2026-11-28). Wave 3, R1. Refines 0201 (read pool, drain sequence). Security review SR-04 (access re-check), SR-16 (inflation cap).
Context
Three of the five bugs from the 100k run (WP-H, 2026-11-27):
- Bug 3, owner CPU creep at constant load. owner0 went 37% -> 91% in 6 minutes, and thread heaps grew about 30 MB/min.
- The catch-up rings kept every retained BROADCAST frame as its own Uint8Array (its own ArrayBuffer).
- Ring bytes grew linearly to the 256 MB budget, about 1.3M small buffers per owner, and V8’s GC worker threads’ CPU grew with them.
- Bug 4, reconnect never completes at 100k (stuck at 95.7%).
- Every room load, dedupe read, tail and index load shared one read pool of 2 per owner.
- Room loads and dedupe reads were one query per room and per client. Tens of thousands queued behind index loads, and handshakes timed out at 30 s.
- Bug 5, drain under load crashes the process.
- A gateway did not answer “stopped” within 5 s.
worker.terminate() then interrupted a uWS upgrade handler, and uWS aborted the process (“Returning from a request handler without responding”, std::terminate).
Decision
- FrameLog (
owner/framelog.ts): a FIFO of frames with ascending keys in one growable byte buffer plus typed-array index columns. It compacts, grows x2 up to its cap, then drops the oldest frames.
- A room’s catch-up ring is one FrameLog (key = seq, <=
ROOM_RING_BYTES). A frame larger than the cap empties it, so the ring is always a contiguous suffix up to the head.
- A client’s committed ACKs are one FrameLog (key = clientSeq, value = seq). Clean ACKs cost no bytes; ACKs with rejections keep their bytes (<= 128 entries, 256 KB).
- The budget charges allocated bytes. A log is at most 5 objects however many frames it holds; a unit test holds 200,000 frames with at most 4 extra Uint8Arrays.
- Two read lanes per owner, one connection each (2 x (1 commit + 2 read) + 1 LISTEN as before, plus 1 for the access re-check = 8 per instance):
- fast: what a subscribe waits for. Room loads, dedupe reads (
pendingFromClientSeq), catch-up tails.
- slow: structural index loads (structure section, journal replay).
- Batched lookups.
- Rooms opened in the same event-loop turn load in one
pages + head query (<= 512 ids).
- Resubscribing clients’ dedupe reads are one
unnest ... lateral query (max client_seq plus up to 1,024 rows each).
- Regression test: with index loads blocked, 112 subscribes to 56 new rooms finished in 14 ms with 2 room-load queries and 1 dedupe query.
- Structure sections inflate with
maxOutputLength = 14 B x MAX_NODES_PER_PAGE + 1 MiB (SR-16). A larger one fails the index load (busy) instead of inflating without limit.
- Drain protocol over shared memory. One Int32Array holds [phase, ack per gateway]. The main thread stores the phase and notifies.
- Upgrades read the phase directly, so a gateway refuses new sockets at once even while its loop is busy.
- Each gateway runs the phase from its own loop and acknowledges it in its slot.
- Phases: stop accepting -> (owners flush, rings drain) -> close all with
going_away -> shutdown. Each waits up to 30 s.
- Gateways exit by themselves from
setImmediate. A terminate is only the last resort, 10 s after the shutdown phase, when no handler can still be running.
- Upgrade handlers also always respond: an exception answers 500.
- Access re-check (SR-04).
- The main thread collects each gateway’s distinct (credential, user, project, file, role) tuples and checks them in batched queries against sessions, api_tokens, users, project_members, projects and files (linora_realtime grants, migration 0003).
- Each failure becomes the linora_auth event that would have closed or downgraded the sockets.
- It runs on every LISTEN reconnect, when events may have been lost, and every
LINORA_REALTIME_RECHECK_S (300 s).
- The denylist matches tokens with
iat <= at + 2 s (SR-09, API vs realtime clock skew).
Alternatives
- A process-wide slab for all rooms’ frames: fewer allocations still, but eviction across rooms needs a free list and compaction across owners of the slab. Per-room logs are simple and already O(1) objects per room.
- A bigger shared read pool: more connections per instance, and index loads still starve subscribes under a storm.
- Closing every socket on LISTEN reconnect (SR-04 alternative): a reconnect storm on every Postgres blip at 100k.
- Terminating gateways after a short timeout: the crash.
Consequences
- Owner heap object count no longer grows with retained frames. GC cost tracks live rooms and clients, not ops.
- Subscribes complete while index loads are slow. Index loads per owner are serialized on the slow lane; a cold restart’s first ops wait for them, and the room queue answers busy beyond 256 batches.
- A drain under load always ends with going_away to every socket and exit code 0 (
test/drain.test.ts: the gateway blocked 7 s inside an upgrade handler during close()).
- Every open socket is re-validated at least every 5 minutes.