Expand description
Single-level hashed timer wheel. O(1) schedule and cancel.
The wheel has N buckets. A scheduled timer at delay ticks goes into
bucket (now + delay) % N with a rounds counter of how many full
revolutions it must sit out first. On tick(), the hand walks one bucket
forward: timers with rounds == 0 fire (their values are returned); the
rest have rounds decremented. Cancel drops the id from the index and
flags the entry; the flagged entry is reclaimed on the next visit to its
bucket.
Tradeoff vs the hierarchical wheel: with a single level, a long delay
causes many no-op revolutions. For workloads with delays bounded by
N ticks, the single level is optimal.
Thread safety: TimerWheel is single-threaded. Every method takes
&mut self, so one caller owns the wheel and there is no interior
synchronisation to pay for. It is Send when V: Send and can be moved
to a ticker thread; to arm timers from several threads at once, enable the
concurrent feature or hand work to the ticker thread through a queue.
use subms_timer_wheel::TimerWheel;
let mut w: TimerWheel<&'static str> = TimerWheel::new(256);
let id = w.schedule(5, "hello");
for _ in 0..4 { assert!(w.tick().is_empty()); }
assert_eq!(w.tick(), vec!["hello"]);
let _ = id; // returned id can be used to cancel before firingFull writeup, design notes and measured benchmarks: https://www.submillisecond.com/cookbook/recipes/subms-timer-wheel
Re-exports§
pub use error::TimerError;pub use features::concurrent::ConcurrentTimerWheel;pub use features::cron::CronError;pub use features::cron::CronSchedule;pub use features::cron::CronScheduler;pub use features::deadline_scheduler::Clock;pub use features::deadline_scheduler::DeadlineScheduler;pub use features::deadline_scheduler::MonotonicClock;pub use features::deadline_scheduler::TestClock;pub use features::hierarchical::HierarchicalTimerWheel;pub use features::metrics::MeteredTimerWheel;pub use features::metrics::TimerMetrics;
Modules§
- error
- Typed error surface for the wheel family.
- features
- Opt-in feature catalog. Each submodule is gated by its own Cargo feature flag and adds a specific capability to the base timer wheel without bloating the core build.
- recipe
SubMsRecipeimpl.