Skip to content

Timers - #7

Merged
supernomad merged 15 commits into
mainfrom
timers
Mar 20, 2026
Merged

supernomad merged 15 commits into
mainfrom
timers

Conversation

@supernomad

Copy link
Copy Markdown
Contributor

No description provided.

Protocol-agnostic hierarchical timer wheel to replace O(n) TCP timer
scanning. Designed for 10k+ connections with extensibility for DNS and
QUIC timer consumers.
Introduces TimerWheel with three-tier hierarchical layout (1ms/256ms/65.5s
resolution), TimerId/TimerHandle types, and the arm()/is_armed() operations
backed by a Slab and per-slot doubly-linked lists.
Implements cancel() on TimerWheel with full doubly-linked list unlinking
(head, middle, tail cases) and silent no-op on already-cancelled handles.
Five TDD tests cover all unlink positions and the idempotent cancel case.
Implements advance(now_ms) which drains inner wheel slots tick by tick,
cascades middle-tier entries to the inner wheel on inner-wrap, and
cascades outer-tier entries to the middle wheel on middle-wrap.
cascade() collects all entries before re-arming to avoid slab aliasing.
Adds four cascade tests: middle-to-inner placement after inner-wrap,
correct firing tick after cascade, outer-to-middle cascade at the 65536
boundary, and simultaneous firing of multiple cascaded entries.
Adds entry_count() -> usize (delegates to slab len). Adds seven edge
case tests: re-arm after cancel, arm at current tick, arm in past
(slot wrap), entry_count tracking, advance noop when time unchanged,
burst catch-up across multiple slots, and no-fire when no timers due.
Introduces timer_kinds.rs with the TcpTimerKind enum (6 variants,
repr(u8)), free functions for packing/unpacking TimerId with a
connection key and timer kind, and TcpTimerHandles for per-connection
handle storage. Adds TimerHandle::from_raw(crate-visible) constructor
to support tests.
Add individual fire_delayed_ack, fire_retransmit, fire_keep_alive,
fire_persist, fire_linger, fire_time_wait, and handle_timer methods
alongside existing poll_timers()/evict_stale(). These methods operate
on a single connection by slab key, enabling O(1) timer-wheel dispatch
instead of O(n) scanning.
@supernomad
supernomad merged commit a99fae8 into main Mar 20, 2026
3 of 6 checks passed
@supernomad
supernomad deleted the timers branch March 20, 2026 06:56
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