Repository: pelikan-io/rustcommon (language: rust)

Issue:
cargo audit warning in ratelimit/clocksource due to unmaintained mach dependency
Running `cargo audit` in a project with `ratelimit` as dependecy raises a warning

```
Crate:     mach
Version:   0.3.2
Warning:   unmaintained
Title:     mach is unmaintained
Date:      2020-07-14
ID:        RUSTSEC-2020-0168
URL:       https://rustsec.org/advisories/RUSTSEC-2020-0168
Dependency tree:
mach 0.3.2
└── clocksource 0.6.0
    └── ratelimit 0.7.1
```

[mach2](https://github.com/JohnTitor/mach2) might be a valid alternative


Discussion / hints:

Fix patch:
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 <brian@pelikan.io>"]
 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<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")]
@@ -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::<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
@@ -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,
@@ -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


Test patch: