pub struct RateLimiter { /* private fields */ }Expand description
An async-ready rate limiter.
Call acquire().await before each operation. If the system is
ahead of the target rate, it returns immediately. If behind, it
awaits until a permit is available. The blocked time is the
wait_time that surfaces coordinated omission.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub async fn acquire(&self) -> i64
pub async fn acquire(&self) -> i64
Acquire one operation permit. Blocks (async) if rate-limited.
Returns the current backlog in ticks (waiting pool value).
Reads ticks_per_op atomically, so a concurrent
Self::reconfigure takes effect on the next call.
Sourcepub fn wait_time_nanos(&self) -> u64
pub fn wait_time_nanos(&self) -> u64
Current wait time in nanoseconds (backlog converted to nanos).
Sourcepub fn total_blocks(&self) -> u64
pub fn total_blocks(&self) -> u64
Total number of acquire calls.
Sourcepub fn reconfigure(&self, spec: RateSpec) -> Result<(), String>
pub fn reconfigure(&self, spec: RateSpec) -> Result<(), String>
Live reconfigure. Atomically swaps the target rate / burst
ratio / unit without stopping the refill task. The next
Self::acquire call reads the new ticks_per_op; the
next refill cycle reads the new burst config.
In-flight backlog (waiting_pool) and already-issued
active-pool permits are preserved. If the new rate is much
higher than the old one the active pool will fill up over
the next few refill ticks; if much lower, already-granted
permits still drain before pressure builds.
Validation of the new spec is the caller’s responsibility —
a negative or zero ops_per_sec will panic in
ticks_per_op. Callers that wire this up through a
Control<RateSpec> should install a validator on the
control to reject bad values before this method is called.