Skip to content

Commit

Permalink
Add time::Sleep::elapse() method
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Dec 3, 2024
1 parent b5a4a3c commit 8e6827b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ntex-util/CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion ntex-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-util"
version = "2.6.1"
version = "2.7.0"
authors = ["ntex contributors <[email protected]>"]
description = "Utilities for ntex framework"
keywords = ["network", "framework", "async", "futures"]
Expand Down
19 changes: 18 additions & 1 deletion ntex-util/src/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down
17 changes: 13 additions & 4 deletions ntex-util/src/time/wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -345,10 +358,6 @@ impl Timer {
}
})
}

// fn remove_timer(&self, handle: usize) {
// self.0.inner.borrow_mut().remove_timer_bucket(handle, true)
// }
}

impl TimerMod {
Expand Down

0 comments on commit 8e6827b

Please sign in to comment.