aboutsummaryrefslogtreecommitdiff
path: root/src/time/delay_queue.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/time/delay_queue.rs')
-rw-r--r--src/time/delay_queue.rs41
1 files changed, 40 insertions, 1 deletions
diff --git a/src/time/delay_queue.rs b/src/time/delay_queue.rs
index ee66adb..9136d90 100644
--- a/src/time/delay_queue.rs
+++ b/src/time/delay_queue.rs
@@ -62,7 +62,7 @@ use std::task::{self, Poll, Waker};
/// performance and scalability benefits.
///
/// State associated with each entry is stored in a [`slab`]. This amortizes the cost of allocation,
-/// and allows reuse of the memory allocated for expired entires.
+/// and allows reuse of the memory allocated for expired entries.
///
/// Capacity can be checked using [`capacity`] and allocated preemptively by using
/// the [`reserve`] method.
@@ -874,6 +874,41 @@ impl<T> DelayQueue<T> {
self.slab.compact();
}
+ /// Gets the [`Key`] that [`poll_expired`] will pull out of the queue next, without
+ /// pulling it out or waiting for the deadline to expire.
+ ///
+ /// Entries that have already expired may be returned in any order, but it is
+ /// guaranteed that this method returns them in the same order as when items
+ /// are popped from the `DelayQueue`.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage
+ ///
+ /// ```rust
+ /// use tokio_util::time::DelayQueue;
+ /// use std::time::Duration;
+ ///
+ /// # #[tokio::main]
+ /// # async fn main() {
+ /// let mut delay_queue = DelayQueue::new();
+ ///
+ /// let key1 = delay_queue.insert("foo", Duration::from_secs(10));
+ /// let key2 = delay_queue.insert("bar", Duration::from_secs(5));
+ /// let key3 = delay_queue.insert("baz", Duration::from_secs(15));
+ ///
+ /// assert_eq!(delay_queue.peek().unwrap(), key2);
+ /// # }
+ /// ```
+ ///
+ /// [`Key`]: struct@Key
+ /// [`poll_expired`]: method@Self::poll_expired
+ pub fn peek(&self) -> Option<Key> {
+ use self::wheel::Stack;
+
+ self.expired.peek().or_else(|| self.wheel.peek())
+ }
+
/// Returns the next time to poll as determined by the wheel
fn next_deadline(&mut self) -> Option<Instant> {
self.wheel
@@ -1166,6 +1201,10 @@ impl<T> wheel::Stack for Stack<T> {
}
}
+ fn peek(&self) -> Option<Self::Owned> {
+ self.head
+ }
+
#[track_caller]
fn remove(&mut self, item: &Self::Borrowed, store: &mut Self::Store) {
let key = *item;