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§
- Fake
Relative Clock - 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.
- Monotonic
Clock - The monotonic clock implemented by
Instant. - Quanta
Clock - A clock using the default
quanta::Clockstructure. - Quanta
Instant - A nanosecond-scale opaque instant (already scaled to reference time) returned from a
QuantaClock. - Quanta
Upkeep Clock - A clock using the default
quanta::Clockstructure and an upkeep thread. - System
Clock - The non-monotonic clock implemented by
SystemTime.
Traits§
- Clock
- A time source used by rate limiters.
- Reasonably
Realtime - Identifies clocks that run similarly to the monotonic realtime clock.
- Reference
- A measurement from a clock.
Type Aliases§
- Default
Clock - The default clock using
quantafor extremely fast timekeeping (at a 100ns resolution).