Skip to content

Commit

Permalink
update ratelimit to use clocksource 0.7.1 (#91)
Browse files Browse the repository at this point in the history
Updates ratelimit to use clocksource 0.7.1
  • Loading branch information
brayniac authored Dec 15, 2023
1 parent 8963348 commit f71961d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ratelimit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ratelimit"
version = "0.7.1"
version = "0.8.0"
authors = ["Brian Martin <brian@pelikan.io>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -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"
27 changes: 8 additions & 19 deletions ratelimit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Nanoseconds<u64>>;
type Instant = clocksource::Instant<Nanoseconds<u64>>;
type AtomicInstant = clocksource::Instant<Nanoseconds<AtomicU64>>;

#[derive(Error, Debug, PartialEq, Eq)]
pub enum Error {
#[error("available tokens cannot be set higher than max tokens")]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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::<Nanoseconds<u64>>::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
Expand Down Expand Up @@ -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::<Nanoseconds<u64>>::from_nanos(
self.refill_interval.as_nanos() as u64,
),
);
let refill_at = AtomicInstant::new(Instant::now() + self.refill_interval);

Ok(Ratelimiter {
available,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f71961d

Please sign in to comment.