pub struct TimerWheel { /* private fields */ }Expand description
Timer wheel for managing scheduled timers.
The timer wheel stores active timers indexed by ID and checks for expired timers during each tick. Expired timers have their callbacks scheduled as tasks on the work queue.
§Thread Safety
The wheel uses a Mutex<HashMap> internally, making it safe to
schedule and cancel from multiple threads.
Implementations§
Source§impl TimerWheel
impl TimerWheel
Sourcepub fn with_max_timers(max_timers: usize) -> Self
pub fn with_max_timers(max_timers: usize) -> Self
Create a timer wheel with specified maximum timer count.
Sourcepub fn schedule_oneshot<F>(
self: &Arc<Self>,
delay: Duration,
priority: Priority,
work: F,
) -> TimerHandle
pub fn schedule_oneshot<F>( self: &Arc<Self>, delay: Duration, priority: Priority, work: F, ) -> TimerHandle
Schedule a one-shot timer.
Returns a handle that cancels the timer when dropped.
Sourcepub fn schedule_periodic<F>(
self: &Arc<Self>,
interval: Duration,
priority: Priority,
work: F,
) -> TimerHandle
pub fn schedule_periodic<F>( self: &Arc<Self>, interval: Duration, priority: Priority, work: F, ) -> TimerHandle
Schedule a periodic timer.
Returns a handle that cancels the timer when dropped.
Sourcepub fn cancel(&self, id: TimerId) -> bool
pub fn cancel(&self, id: TimerId) -> bool
Cancel a timer by ID.
Returns true if the timer was found and cancelled, false if
it was already cancelled or has fired.
Sourcepub fn is_pending(&self, id: TimerId) -> bool
pub fn is_pending(&self, id: TimerId) -> bool
Check if a timer is still pending (scheduled but not yet fired).
Returns true if the timer exists and hasn’t been cancelled or fired.
Sourcepub fn tick(&self, now: Instant) -> Vec<Task>
pub fn tick(&self, now: Instant) -> Vec<Task>
Process expired timers and return tasks to execute.
This should be called during each tick with the current time. Returns a vector of tasks for expired timers.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Get the number of pending timers.
Sourcepub fn dropped_count(&self) -> u64
pub fn dropped_count(&self) -> u64
Get the number of timers dropped due to capacity limits.