pub trait Meter:
Send
+ Sync
+ Debug {
// Required methods
fn try_debit(&self, cost: u64) -> Option<u64>;
fn credit(&self, cost: u64, epoch: u64);
fn time_until(&self, cost: u64) -> Duration;
fn snapshot(&self) -> MeterStatus;
}Expand description
A throttle that admits or rejects weighted units, exposing enough state to compose several
into a MultiRateLimiter and to answer “which limit blocked me, and for how long”.
Implemented by both RateLimiter (continuous token bucket) and
WindowedRateLimiter (fixed-window counter), so a single gate can mix the two. The trait
is deliberately synchronous — the async acquire paths are built on try_debit +
time_until + tokio::time::sleep, which keeps Meter object-safe and lets a composite
never double-debit. Units are weighted (cost): a request-count tier uses cost = 1; a
usage-metered tier uses the call’s cost (e.g. token count).
Required Methods§
Sourcefn try_debit(&self, cost: u64) -> Option<u64>
fn try_debit(&self, cost: u64) -> Option<u64>
Debit cost units if capacity allows. Returns Some(epoch) on success — an opaque token
identifying the accounting session the debit belongs to, which must be passed back to
credit to refund it — or None if rejected. No metrics.
Sourcefn credit(&self, cost: u64, epoch: u64)
fn credit(&self, cost: u64, epoch: u64)
Return cost units previously debited in epoch (used by Reservation rollback). If
the meter’s accounting has moved on (e.g. a fixed window has since reset), the refund no
longer applies and is silently dropped. No metrics.
Sourcefn time_until(&self, cost: u64) -> Duration
fn time_until(&self, cost: u64) -> Duration
Time until cost units are available; Duration::ZERO if they already are.
Sourcefn snapshot(&self) -> MeterStatus
fn snapshot(&self) -> MeterStatus
A point-in-time view for observability.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".