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
impl TimeoutScheduler
Sourcepub fn new(min_sleep_duration: Option<Duration>) -> Self
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.
Sourcepub fn set_timeout<Fut>(&self, delay: Duration, f: Fut) -> CancellationToken
pub fn set_timeout<Fut>(&self, delay: Duration, f: Fut) -> CancellationToken
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
Sourcepub fn cancel_timeout(&self, cancellation_token: CancellationToken)
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.