1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Low-latency timer primitives: a hierarchical **timer wheel** (O(1) insert +
//! cancel) for arbitrary deadlines, and a fixed-duration **[`TimeoutQueue`]** for
//! the common case where every timer shares one timeout.
//!
//! # Two structures — pick by workload
//!
//! - [`TimerWheel`] ([`Wheel`] / [`BoundedWheel`]) — **arbitrary deadlines**:
//! timers that fire at different, unrelated future times.
//! - [`TimeoutQueue`] ([`BoundedTimeoutQueue`]) — **a single fixed timeout**:
//! every timer expires the *same* duration after it is scheduled.
//!
//! The tell: *"do all my timers time out after the same amount of time?"* —
//! yes → [`TimeoutQueue`], no → the wheel.
//!
//! | | [`TimerWheel`] | [`TimeoutQueue`] |
//! |---|---|---|
//! | Timeout per timer | arbitrary | one fixed duration |
//! | Poll | O(active population) | **O(fired)** |
//! | `next_deadline` | lower bound (cached) | **O(1) exact** |
//! | Clock | any monotone source | monotone, non-decreasing |
//!
//! Reach for [`TimeoutQueue`] on the hot path when it fits — idle timeouts (all
//! 30 s), request deadlines (all 5 s), a fixed retry/debounce delay, uniform
//! TTL expiry. Fixed duration ⇒ monotone deadlines ⇒ a sorted list, so a
//! nothing-due poll stays flat (~12 cycles) no matter how many timers are
//! outstanding. A handful of distinct durations → one list each; many or
//! arbitrary deadlines → the wheel.
//!
//! # The clock
//!
//! Every constructor takes `now` — it fixes the **reference epoch**. An
//! `Instant` has no absolute zero (you can only measure durations *between*
//! instants), so deadlines are stored relative to this epoch. Pass one monotone
//! clock source (e.g. `Instant::now()`) to the constructor *and* to every
//! `schedule` / `poll`. A `now` that goes backwards is a bug — [`TimeoutQueue`]
//! debug-asserts on it.
//!
//! # Timer wheel design
//!
//! The wheel is hierarchical: a timer is placed into a slot at the level whose
//! granularity matches how far away its deadline is — coarse levels for distant
//! deadlines, fine levels for near ones.
//!
//! - **Rebalance, don't re-walk:** as a timer's deadline approaches it is moved
//! ("rebalanced") down to finer levels, so `poll` collects ready timers from
//! small, exact fine-level slots instead of walking a fat coarse slot on every
//! call. Rebalancing can be folded into the poll
//! ([`TimerWheel::poll_and_rebalance`]), bounded per poll
//! ([`WheelBuilder::max_rebalances_per_poll`]), or run proactively off the hot
//! path ([`TimerWheel::rebalance`]) to keep the poll tail flat under a large
//! number of timers. Timers still fire at their exact tick.
//! - **Intrusive active-slot lists:** Only non-empty slots are visited
//! during poll and next-deadline queries. No bitmap, no full scan.
//! - **Embedded refcounting:** Lightweight `Cell<u8>` refcount per entry
//! enables fire-and-forget timers alongside cancellable timers without
//! external reference-counting machinery.
//! - **Generic storage:** Parameterized over slab backend — bounded
//! (fixed-capacity) or unbounded (growable).
//!
//! # Quick Start
//!
//! ```
//! use std::time::{Duration, Instant};
//! use nexus_timer::Wheel;
//!
//! let now = Instant::now();
//! let mut wheel: Wheel<u64> = Wheel::unbounded(4096, now);
//!
//! // Schedule a timer 100ms from now
//! let handle = wheel.schedule(now + Duration::from_millis(100), 42u64);
//!
//! // Cancel before it fires — get the value back
//! let value = wheel.cancel(handle);
//! assert_eq!(value, Some(42));
//! ```
pub use WheelEntry;
pub use TimerHandle;
pub use ;
pub use ;
// Re-export Full for bounded wheel users
pub use Full;