Skip to main content

TimerWheel

Struct TimerWheel 

Source
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

Source

pub fn new() -> Self

Create a new timer wheel with default capacity.

Source

pub fn with_max_timers(max_timers: usize) -> Self

Create a timer wheel with specified maximum timer count.

Source

pub fn schedule_oneshot<F>( self: &Arc<Self>, delay: Duration, priority: Priority, work: F, ) -> TimerHandle
where F: FnOnce() + Send + 'static,

Schedule a one-shot timer.

Returns a handle that cancels the timer when dropped.

Source

pub fn schedule_periodic<F>( self: &Arc<Self>, interval: Duration, priority: Priority, work: F, ) -> TimerHandle
where F: Fn() + Send + Sync + 'static,

Schedule a periodic timer.

Returns a handle that cancels the timer when dropped.

Source

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.

Source

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.

Source

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.

Source

pub fn pending_count(&self) -> usize

Get the number of pending timers.

Source

pub fn dropped_count(&self) -> u64

Get the number of timers dropped due to capacity limits.

Trait Implementations§

Source§

impl Debug for TimerWheel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TimerWheel

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.