diff --git a/ntex-util/CHANGES.md b/ntex-util/CHANGES.md index fbb386ff..bf613a24 100644 --- a/ntex-util/CHANGES.md +++ b/ntex-util/CHANGES.md @@ -1,5 +1,9 @@ # Changes +## [2.7.0] - 2024-12-03 + +* Add time::Sleep::elapse() method + ## [2.6.1] - 2024-11-23 * Remove debug print diff --git a/ntex-util/Cargo.toml b/ntex-util/Cargo.toml index 0e5061f4..611c30c8 100644 --- a/ntex-util/Cargo.toml +++ b/ntex-util/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ntex-util" -version = "2.6.1" +version = "2.7.0" authors = ["ntex contributors "] description = "Utilities for ntex framework" keywords = ["network", "framework", "async", "futures"] diff --git a/ntex-util/src/time/mod.rs b/ntex-util/src/time/mod.rs index 3d181b6c..8b97f7a4 100644 --- a/ntex-util/src/time/mod.rs +++ b/ntex-util/src/time/mod.rs @@ -101,6 +101,12 @@ impl Sleep { self.hnd.is_elapsed() } + /// Complete sleep timer. + #[inline] + pub fn elapse(&self) { + self.hnd.elapse() + } + /// Resets the `Sleep` instance to a new deadline. /// /// Calling this function allows changing the instant at which the `Sleep` @@ -354,7 +360,7 @@ impl crate::Stream for Interval { #[allow(clippy::let_underscore_future)] mod tests { use futures_util::StreamExt; - use std::time; + use std::{future::poll_fn, rc::Rc, time}; use super::*; use crate::future::lazy; @@ -449,6 +455,17 @@ mod tests { fut.await; let second_time = now(); assert!(second_time - first_time < time::Duration::from_millis(1)); + + let first_time = now(); + let fut = Rc::new(sleep(Millis(100000))); + let s = fut.clone(); + ntex::rt::spawn(async move { + s.elapse(); + }); + poll_fn(|cx| fut.poll_elapsed(cx)).await; + assert!(fut.is_elapsed()); + let second_time = now(); + assert!(second_time - first_time < time::Duration::from_millis(1)); } #[ntex_macros::rt_test2] diff --git a/ntex-util/src/time/wheel.rs b/ntex-util/src/time/wheel.rs index 3aa0bdbd..16421772 100644 --- a/ntex-util/src/time/wheel.rs +++ b/ntex-util/src/time/wheel.rs @@ -106,6 +106,11 @@ impl TimerHandle { TIMER.with(|t| t.update_timer(self.0, millis)) } + /// Resets the `TimerHandle` instance to elapsed state. + pub fn elapse(&self) { + TIMER.with(|t| t.remove_timer(self.0)) + } + pub fn is_elapsed(&self) -> bool { TIMER.with(|t| t.with_mod(|m| m.timers[self.0].bucket.is_none())) } @@ -303,6 +308,14 @@ impl Timer { }) } + /// Remove timer and wake task + fn remove_timer(&self, hnd: usize) { + self.with_mod(|inner| { + inner.remove_timer_bucket(hnd, false); + inner.timers[hnd].complete(); + }) + } + /// Update existing timer fn update_timer(&self, hnd: usize, millis: u64) { self.with_mod(|inner| { @@ -345,10 +358,6 @@ impl Timer { } }) } - - // fn remove_timer(&self, handle: usize) { - // self.0.inner.borrow_mut().remove_timer_bucket(handle, true) - // } } impl TimerMod {