Expand description
Time sources for rate limiters.
The time sources contained in this module allow the rate limiter to be (optionally) independent of std, and additionally allow mocking the passage of time.
You can supply a custom time source by implementing both Reference
and Clock
for your own types, and by implementing Add<Nanos>
for
your Reference
type:
use governor::clock::{Reference, Clock};
use governor::nanos::Nanos;
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct MyInstant(u64);
impl Add<Nanos> for MyInstant {
type Output = Self;
fn add(self, other: Nanos) -> Self {
Self(self.0 + other.as_u64())
}
}
impl Reference for MyInstant {
fn duration_since(&self, earlier: Self) -> Nanos {
self.0.checked_sub(earlier.0).unwrap_or(0).into()
}
fn saturating_sub(&self, duration: Nanos) -> Self {
Self(self.0.checked_sub(duration.into()).unwrap_or(self.0))
}
}
#[derive(Clone)]
struct MyCounter(u64);
impl Clock for MyCounter {
type Instant = MyInstant;
fn now(&self) -> Self::Instant {
MyInstant(self.0)
}
}
Structs§
- A mock implementation of a clock. All it does is keep track of what “now” is (relative to some point meaningful to the program), and returns that.
- The monotonic clock implemented by
Instant
. - A clock using the default
quanta::Clock
structure. - A nanosecond-scale opaque instant (already scaled to reference time) returned from a
QuantaClock
. - A clock using the default
quanta::Clock
structure and an upkeep thread. - The non-monotonic clock implemented by
SystemTime
.
Traits§
- A time source used by rate limiters.
- Identifies clocks that run similarly to the monotonic realtime clock.
- A measurement from a clock.
Type Aliases§
- The default clock using
quanta
for extremely fast timekeeping (at a 100ns resolution).