TimeoutScheduler

Struct TimeoutScheduler 

Source
pub struct TimeoutScheduler { /* private fields */ }
Expand description

A timeout scheduler, used for running futures at some delay.

This scheduler will use a single tokio task no matter how many timeouts are scheduled.

The scheduler may be wrapped in an Arc or stored in a global variable using lazy_static in order to share it across multiple tasks.

Implementations§

Source§

impl TimeoutScheduler

Source

pub fn new(min_sleep_duration: Option<Duration>) -> Self

Creates a new timeout scheduler.

The min_sleep_duration parameter is the minimum sleep duration that the scheduler will sleep for. Any timeout which requires sleeping for a delay smaller than this will be executed immediately.

Note that a duration value greater than 0 for min_sleep_duration means that the scheduled timeouts may execute earlier than their delay. They will be early by at most the duration of min_sleep_duration. This may slightly increase the performance because it avoids unnecessary short sleeps.

A value of None for min_sleep_duration means that there is no minimum sleep duration, which means that the scheduler will wait for every timeout however small it is. This guarantees that the timeouts will never run before their delay has exceeded.

Source

pub fn set_timeout<Fut>(&self, delay: Duration, f: Fut) -> CancellationToken
where Fut: Future<Output = ()> + Send + 'static,

Executes the given future after the given delay has exceeded.

The delay at which the future is actually executed might be bigger than the given delay, and if min_sleep_duration was set, the delay might even be smaller than the given delay.

This function returns a CancellationToken, which allows cancelling this timeout using a call to TimeoutScheduler::cancel_timeout

Source

pub fn cancel_timeout(&self, cancellation_token: CancellationToken)

Cancels the timeout associated with the given cancellation token. If the timeout was already executed or already cancelled, this does nothing.

Trait Implementations§

Source§

impl Debug for TimeoutScheduler

Source§

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

Formats the value using the given formatter. 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.