perf(gateway): run the worker event hand-off inline instead of two pool hops (#235)

The staging channel and the worker event channel now allow synchronous
continuations, so an event moves from the pipe read loop through the event
writer into the distributor pump on one thread. At ~240 ev/s each removed pool
work item ended in a PortableThreadPool spin-wait.

Subscriber channels keep asynchronous continuations, so no gRPC, dashboard or
alarm consumer code runs on the read loop. A full event channel still parks the
writer on an await and returns to the read loop; both overflow faults, ordering
and replay behavior are unchanged. The queue-depth gauge now increments before
the staging write, since the consumer can decrement inside it.

Documented under 'Event path threading' in docs/GatewayProcessDesign.md.
This commit is contained in:
Joseph Doherty
2026-11-21 08:09:30 -05:00
parent d68d1c0a06
commit 5516561084
4 changed files with 130 additions and 5 deletions
+37
View File
@@ -566,6 +566,43 @@ The worker event queue-depth gauge (`mxgateway.events.worker_queue.depth`) is
incremented at staging and decremented at consumer read, so it reports total
undelivered events across both channels rather than only the inbound channel.
### Event path threading
The staging channel and the inbound event channel are both created with
`AllowSynchronousContinuations = true` (mxaccessgw#235). The read loop's staging
`TryWrite` resumes the event write loop inline, whose `TryWrite` into the
inbound channel resumes the session's distributor pump inline. In the steady
state an event therefore travels from the pipe read to the per-subscriber
channels on one thread, instead of costing two thread-pool work items. At
~240 events/s each of those work items ended in a thread-pool spin-wait, which
was most of the gateway's context-switch rate.
This is safe because everything that can run inline is bounded and
non-blocking:
- The event write loop either moves the event into the inbound channel or, when
that channel is full, parks in its timed `WriteAsync`. The park is an await, so
control returns to the read loop at once and staging absorbs further events
exactly as before. The two overflow faults are unchanged.
- The pump maps the event, appends it to the replay ring under a short lock, and
fans out with `TryWrite`. Subscriber channels keep
`AllowSynchronousContinuations = false`, so no gRPC write, dashboard publish,
alarm reconcile or other consumer code ever runs on the read loop; each
consumer still resumes on the pool. A subscriber overflow runs the overflow
handler (a metric, at most a session state flip, and a log line) inline.
- Channel completion also runs inline. `MarkClosed`, `SetFaulted` and
`DisposeAsync` complete the channels after releasing `_syncRoot`, and no caller
holds a session or distributor lock, so the pump's final sweep and
`ObserveWorkerTerminalState` run on the completing thread without lock
inversion. Previously that work ran on the pool at an unspecified later point.
- The reverse direction is bounded too: when the pump frees space in a full
inbound channel, the parked event writer resumes on the pump's thread, drains
what staging holds into the inbound channel, and awaits again.
The constraint this imposes is that the sole `WorkerClient.ReadEventsAsync`
consumer must not block synchronously inside its loop body. The distributor
pump is the only production consumer and the channel is single-reader.
### Write loop
The write loop serializes all writes to the pipe. No other code should write to