feat: add OutboxWaker — in-process wake to cut worker idle latency#20
Merged
Conversation
`runWorkerLoop` only waits `pollIntervalMs` when a tick claims nothing, so that
interval is the worst-case latency for a lone event landing in an idle outbox
(under load the loop drains immediately and is never gated by it). When that idle
latency matters — a user-facing signal behind an outbox event — turning the poll
down has a floor and a DB-load cost.
`OutboxWaker` is the better lever: pass it to `runWorkerLoop({ waker })` and call
`waker.notify()` after the enqueueing transaction commits, so the worker relays
now instead of on the next poll. Polling stays the backstop — a missed or absent
notify never stalls delivery, only widens latency back to one interval — and wakes
are latched, so an event committed in the sliver between a tick and its sleep is
never lost.
Dialect-agnostic and same-process only; a cross-process wake (Postgres
LISTEN/NOTIFY) is a planned follow-up. Fully backward compatible: omit the waker
and the loop is an unchanged pure poller.
- outbox-waker.ts: the primitive (notify + internal latched wait), 100% covered
- outbox-worker.ts: optional `waker` option; idle/error wait uses it when present
- tests: waker unit specs + worker-loop waker path; latch mutant hand-verified
- README + CHANGELOG: the low-latency-wake recipe and the same-process caveat
📊
|
| Metric | PR | Base | Diff | |
|---|---|---|---|---|
| Statements | ████████████████████ |
1677/1677 (100%) |
1588/1588 (100%) |
⚪ 0% |
| Branches | ████████████████████ |
205/205 (100%) |
188/188 (100%) |
⚪ 0% |
| Functions | ████████████████████ |
67/67 (100%) |
62/62 (100%) |
⚪ 0% |
| Lines | ████████████████████ |
1677/1677 (100%) |
1588/1588 (100%) |
⚪ 0% |
🧾 Changed files
| File | Statements | Branches | Diff | |
|---|---|---|---|---|
messaging/outbox-waker.ts |
████████████████████ |
74/74 (100%) |
13/13 (100%) |
🆕 new |
messaging/outbox-worker.ts |
████████████████████ |
74/74 (100%) |
20/20 (100%) |
⚪ 0% |
Updated for b6ce092 | Compared against base branch
⏱️ Performance Report
🐢 Slowest test suites
🐌 Slowest individual tests
Updated for |
🧠 Cognitive Complexity Report
🧩 Most complex functions
🧾 Changed files
Updated for |
This was referenced Jul 19, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds
OutboxWaker, an opt-in in-process wake forrunWorkerLoop. It lets a producer cut the worker's idle poll wait short so a freshly-committed event is relayed immediately instead of on the next poll — directly answering a reader question on the outbox article: "that 2s poll interval is a quiet latency knob… did you land on 2s for a reason, or is it a sane default?"Why
runWorkerLoopis self-clocking: after a full-batch tick it loops again immediately to drain the backlog, and it only waitspollIntervalMs(default 2s) when a tick claims nothing. So the interval is the worst-case latency for a lone event landing in an idle outbox — not a per-event tax, and under load throughput is never gated by it.When even that idle latency matters (a user-facing "we've got it" behind an outbox event), turning
pollIntervalMsdown works but has a floor and a DB-poll cost.OutboxWakeris the better lever.How it works
notify()never stalls delivery — it only widens latency back to one interval, so correctness never depends on the wake.notify()that arrives in the sliver between an idle tick and its sleep is remembered, so the next wait returns at once — no lost-wakeup race.notify()can't cross process boundaries; a worker in a separate process still polls. A cross-process wake (PostgresLISTEN/NOTIFY) is a planned follow-up, and this primitive is the dialect-agnostic half that bridge will drive.wakerandrunWorkerLoopis an unchanged pure poller.Tests / gates
test/waker.spec.ts(backstop timer, notify-wakes-parked-wait, latched-and-consumed-once, already-aborted, abort-mid-wait + listener cleanup, signal-less) + worker-loop waker-path specs.outbox-waker.tsandoutbox-worker.ts(105 hermetic tests pass); complexity ≤15; core typecheck clean.infra:up && test:full→ hermetic 105/105 and the real Postgres + MySQL integration specs 3/3, 0 skipped. Not store-adjacent, but verified nothing regressed.this.#pending = truefails the "latched wake" spec (waits the full 8s), so the mutant is killed.No version bump here; this is the feature landing on
main. Cutting a release is a follow-up.