From f71961d858e8f0e3123c67454a4731cfc1efd494 Mon Sep 17 00:00:00 2001 From: Brian Martin Date: Fri, 15 Dec 2023 11:19:58 -0800 Subject: [PATCH] update ratelimit to use clocksource 0.7.1 (#91) Updates ratelimit to use clocksource 0.7.1 --- ratelimit/Cargo.toml | 4 ++-- ratelimit/src/lib.rs | 27 ++++++++------------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/ratelimit/Cargo.toml b/ratelimit/Cargo.toml index 6c16a172..9c31af45 100644 --- a/ratelimit/Cargo.toml +++ b/ratelimit/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ratelimit" -version = "0.7.1" +version = "0.8.0" authors = ["Brian Martin "] edition = "2021" license = "MIT OR Apache-2.0" @@ -9,6 +9,6 @@ homepage = "https://github.com/pelikan-io/rustcommon/ratelimit" repository = "https://github.com/pelikan-io/rustcommon" [dependencies] -clocksource = { version = "0.6.0" } +clocksource = { version = "0.7.1" } parking_lot = "0.12.1" thiserror = "1.0.40" \ No newline at end of file diff --git a/ratelimit/src/lib.rs b/ratelimit/src/lib.rs index 223f0f5c..ff97a83c 100644 --- a/ratelimit/src/lib.rs +++ b/ratelimit/src/lib.rs @@ -53,15 +53,11 @@ //! } //! ``` -use clocksource::Nanoseconds; +use clocksource::precise::{AtomicInstant, Duration, Instant}; use core::sync::atomic::{AtomicU64, Ordering}; use parking_lot::RwLock; use thiserror::Error; -type Duration = clocksource::Duration>; -type Instant = clocksource::Instant>; -type AtomicInstant = clocksource::Instant>; - #[derive(Error, Debug, PartialEq, Eq)] pub enum Error { #[error("available tokens cannot be set higher than max tokens")] @@ -109,10 +105,10 @@ impl Ratelimiter { } /// Return the current interval between refills. - pub fn refill_interval(&self) -> Duration { + pub fn refill_interval(&self) -> core::time::Duration { let parameters = self.parameters.read(); - Duration::from_nanos(parameters.refill_interval.as_nanos()) + core::time::Duration::from_nanos(parameters.refill_interval.as_nanos()) } /// Allows for changing the interval between refills at runtime. @@ -220,10 +216,8 @@ impl Ratelimiter { intervals = (time - refill_at).as_nanos() / parameters.refill_interval.as_nanos() + 1; // calculate when the following refill would be - let next_refill = refill_at - + clocksource::Duration::>::from_nanos( - intervals * parameters.refill_interval.as_nanos(), - ); + let next_refill = + refill_at + Duration::from_nanos(intervals * parameters.refill_interval.as_nanos()); // compare/exchange, if race, loop and check if we still need to // refill before trying again @@ -397,12 +391,7 @@ impl Builder { refill_interval: Duration::from_nanos(self.refill_interval.as_nanos() as u64), }; - let refill_at = AtomicInstant::new( - Instant::now() - + clocksource::Duration::>::from_nanos( - self.refill_interval.as_nanos() as u64, - ), - ); + let refill_at = AtomicInstant::new(Instant::now() + self.refill_interval); Ok(Ratelimiter { available, @@ -455,8 +444,8 @@ mod tests { } } - assert!(count >= 800); - assert!(count <= 1200); + assert!(count >= 600); + assert!(count <= 1400); } // quick test that an idle ratelimiter doesn't build up excess capacity