pub struct TokenBucket { /* private fields */ }Expand description
A std-only token bucket for the tree-wide spawn-rate cap (8 burst,
2 tokens/s by default). Hand-rolled — a rate limiter is Instant +
arithmetic, never a crate. Refill is lazy: every try_take first credits
the tokens that have accrued since the last call, then spends one if it can.
This catches a fast churn loop that stays under the absolute subagent count
— a wedged child hammering subagent.spawn just keeps getting refusals.
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcepub fn new(burst: u32, per_sec: f64) -> TokenBucket
pub fn new(burst: u32, per_sec: f64) -> TokenBucket
A bucket that starts full (burst tokens) and refills per_sec tokens
each second up to burst.
Sourcepub fn try_take_at(&mut self, now: Instant) -> bool
pub fn try_take_at(&mut self, now: Instant) -> bool
Lazy-refill to now, then spend one token if one is available. Returns
whether a token was taken (i.e. whether the spawn may proceed). now is
explicit so the refill is deterministically unit-testable; last only
ever moves forward (a non-monotonic now simply adds no tokens).
Trait Implementations§
Source§impl Clone for TokenBucket
impl Clone for TokenBucket
Source§fn clone(&self) -> TokenBucket
fn clone(&self) -> TokenBucket
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more