Skip to main content

Crate subms_timer_wheel

Crate subms_timer_wheel 

Source
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 delay / N. On tick(), the hand walks one bucket forward: timers with rounds == 0 fire (their callbacks are returned); the rest have rounds decremented. Cancel sets a flag; the entry is dropped lazily on the next visit.

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.

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 firing

Structsยง

TimerWheel