Skip to content

feat(timer-wheel): an approximate per-thread timer wheel - #1990

Closed
daniel-noland wants to merge 1 commit into
mainfrom
pr/daniel-noland/timer-wheel-crate
Closed

daniel-noland wants to merge 1 commit into
mainfrom
pr/daniel-noland/timer-wheel-crate

Conversation

@daniel-noland

Copy link
Copy Markdown
Collaborator

First of a stack that takes flow and NAT-mapping expiry off tokio, with no async on the workers. This PR adds only the generic crate. The next two PRs put flows and mapping reapers on it.

Why

On env-5's bare-metal gateway, per-flow tokio timers are a failure mode, not a slowdown:

  • With the 5 s one-way timeout at 1M flows, about 600k timer re-arms per second land on the shared 128-thread mgmt runtime. That convoys its locks, BGP drops, and the gateway leaves the network: steady state falls to 0.06 Mpps.
  • An expiry storm of 1M flows causes a ~7 s outage.
  • The gateway cannot sustain even 0.25 M new flows/s.

A synchronous per-worker wheel removes all three. Details are in the env-5 notes (SUMMARY.md, E1-E3) and can be shared.

Design (timer-wheel/)

  • One level: WheelConfig::standard() is 10 ms × 8192 slots. Longer deadlines wrap and are revisited.
  • Storage: entries live in a slab with an intrusive doubly linked list per bucket. Insert, cancel and move are O(1), with no allocation per entry. A TimerId carries a generation, so a stale cancel is a no-op.
  • Lazy deadlines: the payload reports its current deadline when it comes due. A refresh therefore costs the wheel nothing; a later deadline just re-links the entry.
  • Bounded sweeps: sweep(now, budget, &mut out) looks at most budget due entries and hands back Fired::{Due, Cancelled} in a batch. An avalanche of expiries cannot stall a worker's rx polling.
  • Cross-thread: TimerRef::cancel and Remote::schedule go through mutex-guarded queues from the concurrency facade, so they also run under shuttle and loom. Cross-thread traffic is rare: invalidations, and config apply.
  • Tests: a bolero model test against a reference model, and a cross-thread concurrency::test.

The stack

  1. this PR: the crate;
  2. flows expire on the worker's wheel;
  3. masquerade mapping reapers on the wheel.

Further up, flow-state layout PRs make use of it. A docs PR is still to come.

🤖 Generated with Claude Code

Per-flow tokio timer tasks collapse the gateway at a million flows:
every refreshed flow re-arms a task on the shared management runtime,
which convoys on its locks until BGP drops. A single-level wheel owned
by the worker thread avoids the runtime entirely.

Timers do not store their deadline; the payload is asked when its
bucket comes due and is moved if the deadline went later, so a refresh
costs the refresher only its own store. Entries live in a slab with an
intrusive list per bucket: insert and cancel are O(1). Each sweep does
a bounded amount of work and hands fired and cancelled payloads back
as a batch. Other threads cancel through a TimerRef and schedule
through a Remote; both queue for the owner's next sweep.

A timer fires no earlier than its deadline and, unless the wheel is
behind, within a tick of it. A model-based property test checks this
against a map of deadlines, with cancellation, refresh, wrap-around
and budgets; a concurrency test runs under loom and shuttle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 25, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.98597% with 35 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
timer-wheel/src/lib.rs 87.24% 13 Missing and 12 partials ⚠️
timer-wheel/src/config.rs 78.57% 6 Missing ⚠️
timer-wheel/src/queue.rs 90.90% 3 Missing ⚠️
timer-wheel/src/tests.rs 99.58% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@daniel-noland

Copy link
Copy Markdown
Collaborator Author

Closed in favor of #2004.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant