Struct tokio::timer::DelayQueue
source · [−]pub struct DelayQueue<T> { /* private fields */ }
Expand description
A queue of delayed elements.
Once an element is inserted into the DelayQueue
, it is yielded once the
specified deadline has been reached.
Usage
Elements are inserted into DelayQueue
using the insert
or
insert_at
methods. A deadline is provided with the item and a Key
is
returned. The key is used to remove the entry or to change the deadline at
which it should be yielded back.
Once delays have been configured, the DelayQueue
is used via its
Stream
implementation. poll
is called. If an entry has reached its
deadline, it is returned. If not, Async::NotReady
indicating that the
current task will be notified once the deadline has been reached.
Stream
implementation
Items are retrieved from the queue via Stream::poll
. If no delays have
expired, no items are returned. In this case, NotReady
is returned and the
current task is registered to be notified once the next item’s delay has
expired.
If no items are in the queue, i.e. is_empty()
returns true
, then poll
returns Ready(None)
. This indicates that the stream has reached an end.
However, if a new item is inserted after, poll
will once again start
returning items or `NotReady.
Items are returned ordered by their expirations. Items that are configured to expire first will be returned first. There are no ordering guarantees for items configured to expire the same instant. Also note that delays are rounded to the closest millisecond.
Implementation
The DelayQueue
is backed by the same hashed timing wheel implementation as
Timer
as such, it offers the same performance benefits. See Timer
for further implementation notes.
State associated with each entry is stored in a slab
. This allows
amortizing the cost of allocation. Space created for expired entries is
reused when inserting new entries.
Capacity can be checked using capacity
and allocated preemptively by using
the reserve
method.
Usage
Using DelayQueue
to manage cache entries.
#[macro_use]
extern crate futures;
extern crate tokio;
use tokio::timer::{delay_queue, DelayQueue, Error};
use futures::{Async, Poll, Stream};
use std::collections::HashMap;
use std::time::Duration;
struct Cache {
entries: HashMap<CacheKey, (Value, delay_queue::Key)>,
expirations: DelayQueue<CacheKey>,
}
const TTL_SECS: u64 = 30;
impl Cache {
fn insert(&mut self, key: CacheKey, value: Value) {
let delay = self.expirations
.insert(key.clone(), Duration::from_secs(TTL_SECS));
self.entries.insert(key, (value, delay));
}
fn get(&self, key: &CacheKey) -> Option<&Value> {
self.entries.get(key)
.map(|&(ref v, _)| v)
}
fn remove(&mut self, key: &CacheKey) {
if let Some((_, cache_key)) = self.entries.remove(key) {
self.expirations.remove(&cache_key);
}
}
fn poll_purge(&mut self) -> Poll<(), Error> {
while let Some(entry) = try_ready!(self.expirations.poll()) {
self.entries.remove(entry.get_ref());
}
Ok(Async::Ready(()))
}
}
Implementations
sourceimpl<T> DelayQueue<T>
impl<T> DelayQueue<T>
sourcepub fn new() -> DelayQueue<T>
pub fn new() -> DelayQueue<T>
Create a new, empty, DelayQueue
The queue will not allocate storage until items are inserted into it.
Examples
let delay_queue: DelayQueue<u32> = DelayQueue::new();
sourcepub fn with_capacity_and_handle(
capacity: usize,
handle: &Handle
) -> DelayQueue<T>
pub fn with_capacity_and_handle(
capacity: usize,
handle: &Handle
) -> DelayQueue<T>
Create a new, empty, DelayQueue
backed by the specified timer.
The queue will not allocate storage until items are inserted into it.
Examples
use tokio_timer::timer::Handle;
let handle = Handle::default();
let delay_queue: DelayQueue<u32> = DelayQueue::with_capacity_and_handle(0, &handle);
sourcepub fn with_capacity(capacity: usize) -> DelayQueue<T>
pub fn with_capacity(capacity: usize) -> DelayQueue<T>
Create a new, empty, DelayQueue
with the specified capacity.
The queue will be able to hold at least capacity
elements without
reallocating. If capacity
is 0, the queue will not allocate for
storage.
Examples
let mut delay_queue = DelayQueue::with_capacity(10);
// These insertions are done without further allocation
for i in 0..10 {
delay_queue.insert(i, Duration::from_secs(i));
}
// This will make the queue allocate additional storage
delay_queue.insert(11, Duration::from_secs(11));
sourcepub fn insert_at(&mut self, value: T, when: Instant) -> Key
pub fn insert_at(&mut self, value: T, when: Instant) -> Key
Insert value
into the queue set to expire at a specific instant in
time.
This function is identical to insert
, but takes an Instant
instead
of a Duration
.
value
is stored in the queue until when
is reached. At which point,
value
will be returned from poll
. If when
has already been
reached, then value
is immediately made available to poll.
The return value represents the insertion and is used at an argument to
remove
and reset
. Note that Key
is token and is reused once
value
is removed from the queue either by calling poll
after
when
is reached or by calling remove
. At this point, the caller
must take care to not use the returned Key
again as it may reference
a different item in the queue.
See type level documentation for more details.
Panics
This function panics if when
is too far in the future.
Examples
Basic usage
use tokio::timer::DelayQueue;
use std::time::{Instant, Duration};
let mut delay_queue = DelayQueue::new();
let key = delay_queue.insert_at(
"foo", Instant::now() + Duration::from_secs(5));
// Remove the entry
let item = delay_queue.remove(&key);
assert_eq!(*item.get_ref(), "foo");
sourcepub fn insert(&mut self, value: T, timeout: Duration) -> Key
pub fn insert(&mut self, value: T, timeout: Duration) -> Key
Insert value
into the queue set to expire after the requested duration
elapses.
This function is identical to insert_at
, but takes a Duration
instead of an Instant
.
value
is stored in the queue until when
is reached. At which point,
value
will be returned from poll
. If when
has already been
reached, then value
is immediately made available to poll.
The return value represents the insertion and is used at an argument to
remove
and reset
. Note that Key
is token and is reused once
value
is removed from the queue either by calling poll
after
when
is reached or by calling remove
. At this point, the caller
must take care to not use the returned Key
again as it may reference
a different item in the queue.
See type level documentation for more details.
Panics
This function panics if timeout
is greater than the maximum supported
duration.
Examples
Basic usage
use tokio::timer::DelayQueue;
use std::time::Duration;
let mut delay_queue = DelayQueue::new();
let key = delay_queue.insert("foo", Duration::from_secs(5));
// Remove the entry
let item = delay_queue.remove(&key);
assert_eq!(*item.get_ref(), "foo");
sourcepub fn remove(&mut self, key: &Key) -> Expired<T>
pub fn remove(&mut self, key: &Key) -> Expired<T>
Remove the item associated with key
from the queue.
There must be an item associated with key
. The function returns the
removed item as well as the Instant
at which it will the delay will
have expired.
Panics
The function panics if key
is not contained by the queue.
Examples
Basic usage
use tokio::timer::DelayQueue;
use std::time::Duration;
let mut delay_queue = DelayQueue::new();
let key = delay_queue.insert("foo", Duration::from_secs(5));
// Remove the entry
let item = delay_queue.remove(&key);
assert_eq!(*item.get_ref(), "foo");
sourcepub fn reset_at(&mut self, key: &Key, when: Instant)
pub fn reset_at(&mut self, key: &Key, when: Instant)
Sets the delay of the item associated with key
to expire at when
.
This function is identical to reset
but takes an Instant
instead of
a Duration
.
The item remains in the queue but the delay is set to expire at when
.
If when
is in the past, then the item is immediately made available to
the caller.
Panics
This function panics if when
is too far in the future or if key
is
not contained by the queue.
Examples
Basic usage
use tokio::timer::DelayQueue;
use std::time::{Duration, Instant};
let mut delay_queue = DelayQueue::new();
let key = delay_queue.insert("foo", Duration::from_secs(5));
// "foo" is scheduled to be returned in 5 seconds
delay_queue.reset_at(&key, Instant::now() + Duration::from_secs(10));
// "foo"is now scheduled to be returned in 10 seconds
sourcepub fn reset(&mut self, key: &Key, timeout: Duration)
pub fn reset(&mut self, key: &Key, timeout: Duration)
Sets the delay of the item associated with key
to expire after
timeout
.
This function is identical to reset_at
but takes a Duration
instead
of an Instant
.
The item remains in the queue but the delay is set to expire after
timeout
. If timeout
is zero, then the item is immediately made
available to the caller.
Panics
This function panics if timeout
is greater than the maximum supported
duration or if key
is not contained by the queue.
Examples
Basic usage
use tokio::timer::DelayQueue;
use std::time::Duration;
let mut delay_queue = DelayQueue::new();
let key = delay_queue.insert("foo", Duration::from_secs(5));
// "foo" is scheduled to be returned in 5 seconds
delay_queue.reset(&key, Duration::from_secs(10));
// "foo"is now scheduled to be returned in 10 seconds
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the queue, removing all items.
After calling clear
, poll
will return Ok(Ready(None))
.
Note that this method has no effect on the allocated capacity.
Examples
use tokio::timer::DelayQueue;
use std::time::Duration;
let mut delay_queue = DelayQueue::new();
delay_queue.insert("foo", Duration::from_secs(5));
assert!(!delay_queue.is_empty());
delay_queue.clear();
assert!(delay_queue.is_empty());
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the queue can hold without reallocating.
Examples
let delay_queue: DelayQueue<i32> = DelayQueue::with_capacity(10);
assert_eq!(delay_queue.capacity(), 10);
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserve capacity for at least additional
more items to be queued
without allocating.
reserve
does nothing if the queue already has sufficient capacity for
additional
more values. If more capacity is required, a new segment of
memory will be allocated and all existing values will be copied into it.
As such, if the queue is already very large, a call to reserve
can end
up being expensive.
The queue may reserve more than additional
extra space in order to
avoid frequent reallocations.
Panics
Panics if the new capacity exceeds the maximum number of entries the queue can contain.
Examples
let mut delay_queue = DelayQueue::new();
delay_queue.insert("hello", Duration::from_secs(10));
delay_queue.reserve(10);
assert!(delay_queue.capacity() >= 11);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if there are no items in the queue.
Note that this function returns false
even if all items have not yet
expired and a call to poll
will return NotReady
.
Examples
use std::time::Duration;
let mut delay_queue = DelayQueue::new();
assert!(delay_queue.is_empty());
delay_queue.insert("hello", Duration::from_secs(5));
assert!(!delay_queue.is_empty());
Trait Implementations
sourceimpl<T> Debug for DelayQueue<T> where
T: Debug,
impl<T> Debug for DelayQueue<T> where
T: Debug,
sourceimpl<T> Stream for DelayQueue<T>
impl<T> Stream for DelayQueue<T>
sourcepub fn poll(
&mut self
) -> Result<Async<Option<<DelayQueue<T> as Stream>::Item>>, Error>
pub fn poll(
&mut self
) -> Result<Async<Option<<DelayQueue<T> as Stream>::Item>>, Error>
Attempt to pull out the next value of this stream, returning None
if
the stream is finished. Read more
sourcefn wait(self) -> Wait<Self>ⓘNotable traits for Wait<S>impl<S> Iterator for Wait<S> where
S: Stream, type Item = Result<<S as Stream>::Item, <S as Stream>::Error>;
fn wait(self) -> Wait<Self>ⓘNotable traits for Wait<S>impl<S> Iterator for Wait<S> where
S: Stream, type Item = Result<<S as Stream>::Item, <S as Stream>::Error>;
S: Stream, type Item = Result<<S as Stream>::Item, <S as Stream>::Error>;
Creates an iterator which blocks the current thread until each item of this stream is resolved. Read more
sourcefn into_future(self) -> StreamFuture<Self>
fn into_future(self) -> StreamFuture<Self>
Converts this stream into a Future
. Read more
sourcefn map<U, F>(self, f: F) -> Map<Self, F> where
F: FnMut(Self::Item) -> U,
fn map<U, F>(self, f: F) -> Map<Self, F> where
F: FnMut(Self::Item) -> U,
Converts a stream of type T
to a stream of type U
. Read more
sourcefn map_err<U, F>(self, f: F) -> MapErr<Self, F> where
F: FnMut(Self::Error) -> U,
fn map_err<U, F>(self, f: F) -> MapErr<Self, F> where
F: FnMut(Self::Error) -> U,
Converts a stream of error type T
to a stream of error type U
. Read more
sourcefn filter<F>(self, f: F) -> Filter<Self, F> where
F: FnMut(&Self::Item) -> bool,
fn filter<F>(self, f: F) -> Filter<Self, F> where
F: FnMut(&Self::Item) -> bool,
Filters the values produced by this stream according to the provided predicate. Read more
sourcefn filter_map<F, B>(self, f: F) -> FilterMap<Self, F> where
F: FnMut(Self::Item) -> Option<B>,
fn filter_map<F, B>(self, f: F) -> FilterMap<Self, F> where
F: FnMut(Self::Item) -> Option<B>,
Filters the values produced by this stream while simultaneously mapping them to a different type. Read more
sourcefn then<F, U>(self, f: F) -> Then<Self, F, U> where
F: FnMut(Result<Self::Item, Self::Error>) -> U,
U: IntoFuture,
fn then<F, U>(self, f: F) -> Then<Self, F, U> where
F: FnMut(Result<Self::Item, Self::Error>) -> U,
U: IntoFuture,
Chain on a computation for when a value is ready, passing the resulting
item to the provided closure f
. Read more
sourcefn and_then<F, U>(self, f: F) -> AndThen<Self, F, U> where
F: FnMut(Self::Item) -> U,
U: IntoFuture<Error = Self::Error>,
fn and_then<F, U>(self, f: F) -> AndThen<Self, F, U> where
F: FnMut(Self::Item) -> U,
U: IntoFuture<Error = Self::Error>,
Chain on a computation for when a value is ready, passing the successful
results to the provided closure f
. Read more
sourcefn or_else<F, U>(self, f: F) -> OrElse<Self, F, U> where
F: FnMut(Self::Error) -> U,
U: IntoFuture<Item = Self::Item>,
fn or_else<F, U>(self, f: F) -> OrElse<Self, F, U> where
F: FnMut(Self::Error) -> U,
U: IntoFuture<Item = Self::Item>,
Chain on a computation for when an error happens, passing the
erroneous result to the provided closure f
. Read more
sourcefn collect(self) -> Collect<Self>
fn collect(self) -> Collect<Self>
Collect all of the values of this stream into a vector, returning a future representing the result of that computation. Read more
sourcefn concat2(self) -> Concat2<Self> where
Self::Item: Extend<<Self::Item as IntoIterator>::Item>,
Self::Item: IntoIterator,
Self::Item: Default,
fn concat2(self) -> Concat2<Self> where
Self::Item: Extend<<Self::Item as IntoIterator>::Item>,
Self::Item: IntoIterator,
Self::Item: Default,
Concatenate all results of a stream into a single extendable destination, returning a future representing the end result. Read more
sourcefn concat(self) -> Concat<Self> where
Self::Item: Extend<<Self::Item as IntoIterator>::Item>,
Self::Item: IntoIterator,
fn concat(self) -> Concat<Self> where
Self::Item: Extend<<Self::Item as IntoIterator>::Item>,
Self::Item: IntoIterator,
please use Stream::concat2
instead
Concatenate all results of a stream into a single extendable destination, returning a future representing the end result. Read more
sourcefn fold<F, T, Fut>(self, init: T, f: F) -> Fold<Self, F, Fut, T> where
F: FnMut(T, Self::Item) -> Fut,
Fut: IntoFuture<Item = T>,
Self::Error: From<<Fut as IntoFuture>::Error>,
fn fold<F, T, Fut>(self, init: T, f: F) -> Fold<Self, F, Fut, T> where
F: FnMut(T, Self::Item) -> Fut,
Fut: IntoFuture<Item = T>,
Self::Error: From<<Fut as IntoFuture>::Error>,
Execute an accumulating computation over a stream, collecting all the values into one final result. Read more
sourcefn flatten(self) -> Flatten<Self> where
Self::Item: Stream,
<Self::Item as Stream>::Error: From<Self::Error>,
fn flatten(self) -> Flatten<Self> where
Self::Item: Stream,
<Self::Item as Stream>::Error: From<Self::Error>,
Flattens a stream of streams into just one continuous stream. Read more
sourcefn skip_while<P, R>(self, pred: P) -> SkipWhile<Self, P, R> where
P: FnMut(&Self::Item) -> R,
R: IntoFuture<Item = bool, Error = Self::Error>,
fn skip_while<P, R>(self, pred: P) -> SkipWhile<Self, P, R> where
P: FnMut(&Self::Item) -> R,
R: IntoFuture<Item = bool, Error = Self::Error>,
Skip elements on this stream while the predicate provided resolves to
true
. Read more
sourcefn take_while<P, R>(self, pred: P) -> TakeWhile<Self, P, R> where
P: FnMut(&Self::Item) -> R,
R: IntoFuture<Item = bool, Error = Self::Error>,
fn take_while<P, R>(self, pred: P) -> TakeWhile<Self, P, R> where
P: FnMut(&Self::Item) -> R,
R: IntoFuture<Item = bool, Error = Self::Error>,
Take elements from this stream while the predicate provided resolves to
true
. Read more
sourcefn for_each<F, U>(self, f: F) -> ForEach<Self, F, U> where
F: FnMut(Self::Item) -> U,
U: IntoFuture<Item = (), Error = Self::Error>,
fn for_each<F, U>(self, f: F) -> ForEach<Self, F, U> where
F: FnMut(Self::Item) -> U,
U: IntoFuture<Item = (), Error = Self::Error>,
Runs this stream to completion, executing the provided closure for each element on the stream. Read more
sourcefn from_err<E>(self) -> FromErr<Self, E> where
E: From<Self::Error>,
fn from_err<E>(self) -> FromErr<Self, E> where
E: From<Self::Error>,
Map this stream’s error to any error implementing From
for
this stream’s Error
, returning a new stream. Read more
sourcefn take(self, amt: u64) -> Take<Self>
fn take(self, amt: u64) -> Take<Self>
Creates a new stream of at most amt
items of the underlying stream. Read more
sourcefn skip(self, amt: u64) -> Skip<Self>
fn skip(self, amt: u64) -> Skip<Self>
Creates a new stream which skips amt
items of the underlying stream. Read more
sourcefn fuse(self) -> Fuse<Self>
fn fuse(self) -> Fuse<Self>
Fuse a stream such that poll
will never again be called once it has
finished. Read more
sourcefn catch_unwind(self) -> CatchUnwind<Self> where
Self: UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self> where
Self: UnwindSafe,
Catches unwinding panics while polling the stream. Read more
sourcefn buffered(self, amt: usize) -> Buffered<Self> where
Self::Item: IntoFuture,
<Self::Item as IntoFuture>::Error == Self::Error,
fn buffered(self, amt: usize) -> Buffered<Self> where
Self::Item: IntoFuture,
<Self::Item as IntoFuture>::Error == Self::Error,
An adaptor for creating a buffered list of pending futures. Read more
sourcefn buffer_unordered(self, amt: usize) -> BufferUnordered<Self> where
Self::Item: IntoFuture,
<Self::Item as IntoFuture>::Error == Self::Error,
fn buffer_unordered(self, amt: usize) -> BufferUnordered<Self> where
Self::Item: IntoFuture,
<Self::Item as IntoFuture>::Error == Self::Error,
An adaptor for creating a buffered list of pending futures (unordered). Read more
sourcefn merge<S>(self, other: S) -> Merge<Self, S> where
S: Stream<Error = Self::Error>,
fn merge<S>(self, other: S) -> Merge<Self, S> where
S: Stream<Error = Self::Error>,
functionality provided by select
now
An adapter for merging the output of two streams. Read more
sourcefn zip<S>(self, other: S) -> Zip<Self, S> where
S: Stream<Error = Self::Error>,
fn zip<S>(self, other: S) -> Zip<Self, S> where
S: Stream<Error = Self::Error>,
An adapter for zipping two streams together. Read more
sourcefn chain<S>(self, other: S) -> Chain<Self, S> where
S: Stream<Item = Self::Item, Error = Self::Error>,
fn chain<S>(self, other: S) -> Chain<Self, S> where
S: Stream<Item = Self::Item, Error = Self::Error>,
Adapter for chaining two stream. Read more
sourcefn peekable(self) -> Peekable<Self>
fn peekable(self) -> Peekable<Self>
Creates a new stream which exposes a peek
method. Read more
sourcefn chunks(self, capacity: usize) -> Chunks<Self>
fn chunks(self, capacity: usize) -> Chunks<Self>
An adaptor for chunking up items of the stream inside a vector. Read more
sourcefn select<S>(self, other: S) -> Select<Self, S> where
S: Stream<Item = Self::Item, Error = Self::Error>,
fn select<S>(self, other: S) -> Select<Self, S> where
S: Stream<Item = Self::Item, Error = Self::Error>,
Creates a stream that selects the next element from either this stream or the provided one, whichever is ready first. Read more
sourcefn forward<S>(self, sink: S) -> Forward<Self, S> where
S: Sink<SinkItem = Self::Item>,
Self::Error: From<<S as Sink>::SinkError>,
fn forward<S>(self, sink: S) -> Forward<Self, S> where
S: Sink<SinkItem = Self::Item>,
Self::Error: From<<S as Sink>::SinkError>,
A future that completes after the given stream has been fully processed into the sink, including flushing. Read more
sourcefn split(self) -> (SplitSink<Self>, SplitStream<Self>) where
Self: Sink,
fn split(self) -> (SplitSink<Self>, SplitStream<Self>) where
Self: Sink,
Splits this Stream + Sink
object into separate Stream
and Sink
objects. Read more
sourcefn inspect<F>(self, f: F) -> Inspect<Self, F> where
F: FnMut(&Self::Item),
fn inspect<F>(self, f: F) -> Inspect<Self, F> where
F: FnMut(&Self::Item),
Do something with each item of this stream, afterwards passing it on. Read more
sourcefn inspect_err<F>(self, f: F) -> InspectErr<Self, F> where
F: FnMut(&Self::Error),
fn inspect_err<F>(self, f: F) -> InspectErr<Self, F> where
F: FnMut(&Self::Error),
Do something with the error of this stream, afterwards passing it on. Read more
Auto Trait Implementations
impl<T> !RefUnwindSafe for DelayQueue<T>
impl<T> Send for DelayQueue<T> where
T: Send,
impl<T> Sync for DelayQueue<T> where
T: Sync,
impl<T> Unpin for DelayQueue<T> where
T: Unpin,
impl<T> !UnwindSafe for DelayQueue<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more