From 43badfe59cc5a84f7fb525e9dfaae1f68013e534 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 17 Oct 2022 07:11:06 +0200 Subject: [PATCH] test tokio timer --- ntex-util/Cargo.toml | 1 + ntex-util/src/time/wheel.rs | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/ntex-util/Cargo.toml b/ntex-util/Cargo.toml index 7eb9961c..f96cee81 100644 --- a/ntex-util/Cargo.toml +++ b/ntex-util/Cargo.toml @@ -26,6 +26,7 @@ futures-timer = "3.0.2" futures-core = { version = "0.3", default-features = false, features = ["alloc"] } futures-sink = { version = "0.3", default-features = false, features = ["alloc"] } pin-project-lite = "0.2.6" +tokio = { version = "1", features = ["time"] } [dev-dependencies] ntex = { version = "0.5", features = ["tokio"] } diff --git a/ntex-util/src/time/wheel.rs b/ntex-util/src/time/wheel.rs index c7949f30..b4dc048a 100644 --- a/ntex-util/src/time/wheel.rs +++ b/ntex-util/src/time/wheel.rs @@ -6,8 +6,9 @@ use std::cell::RefCell; use std::time::{Duration, Instant, SystemTime}; use std::{cmp::max, future::Future, mem, pin::Pin, rc::Rc, task, task::Poll}; -use futures_timer::Delay; +// use futures_timer::Delay; use slab::Slab; +use tokio::time::{Sleep as Delay, sleep}; use crate::task::LocalWaker; @@ -149,14 +150,14 @@ struct Timer { next_expiry: u64, flags: Flags, driver: LocalWaker, - driver_sleep: Delay, + driver_sleep: Pin>, buckets: Vec, /// Bit field tracking which bucket currently contain entries. occupied: [u64; WHEEL_SIZE], lowres_time: Option, lowres_stime: Option, lowres_driver: LocalWaker, - lowres_driver_sleep: Delay, + lowres_driver_sleep: Pin>, } impl Timer { @@ -169,12 +170,12 @@ impl Timer { next_expiry: u64::MAX, flags: Flags::empty(), driver: LocalWaker::new(), - driver_sleep: Delay::new(Duration::ZERO), + driver_sleep: Box::pin(sleep(Duration::ZERO)), occupied: [0; WHEEL_SIZE], lowres_time: None, lowres_stime: None, lowres_driver: LocalWaker::new(), - lowres_driver_sleep: Delay::new(Duration::ZERO), + lowres_driver_sleep: Box::pin(sleep(Duration::ZERO)), } } @@ -558,7 +559,7 @@ struct TimerDriver(Rc>); impl TimerDriver { fn start(slf: &mut Timer, cell: &Rc>) { slf.flags.insert(Flags::DRIVER_STARTED); - slf.driver_sleep = Delay::new(Duration::from_millis(slf.next_expiry_ms())); + slf.driver_sleep = Box::pin(sleep(Duration::from_millis(slf.next_expiry_ms()))); crate::spawn(TimerDriver(cell.clone())); } @@ -588,7 +589,7 @@ impl Future for TimerDriver { } else { Duration::from_millis(inner.next_expiry_ms()) }; - inner.driver_sleep.reset(deadline); + inner.driver_sleep = Box::pin(sleep(deadline)); } loop { @@ -601,7 +602,7 @@ impl Future for TimerDriver { if let Some(next_expiry) = inner.next_pending_bucket() { inner.next_expiry = next_expiry; let dur = Duration::from_millis(inner.next_expiry_ms()); - inner.driver_sleep.reset(dur); + inner.driver_sleep = Box::pin(sleep(dur)); continue; } else { inner.next_expiry = u64::MAX; @@ -618,7 +619,7 @@ struct LowresTimerDriver(Rc>); impl LowresTimerDriver { fn start(slf: &mut Timer, cell: &Rc>) { slf.flags.insert(Flags::LOWRES_DRIVER); - slf.lowres_driver_sleep = Delay::new(LOWRES_RESOLUTION); + slf.lowres_driver_sleep = Box::pin(sleep(LOWRES_RESOLUTION)); crate::spawn(LowresTimerDriver(cell.clone())); } @@ -649,7 +650,7 @@ impl Future for LowresTimerDriver { return Poll::Pending; } else { inner.flags.insert(Flags::LOWRES_TIMER); - inner.lowres_driver_sleep.reset(LOWRES_RESOLUTION); + inner.lowres_driver_sleep = Box::pin(sleep(LOWRES_RESOLUTION)); } } }