pub struct RateLimiter { /* private fields */ }Expand description
Per-user token buckets, one per (user, Class).
§Algorithm
A classic token bucket, refilled continuously rather than on a timer:
capacity is one minute’s allowance, tokens accrue at
per_minute / 60 per second, and each admitted request spends one.
A bucket is refilled lazily when it is read, so there is no background
thread and no periodic sweep — which is what lets this stay
synchronous, allocation-light, and free on an idle node.
Capacity equal to a full minute means an agent may burst a minute’s worth at once and then proceeds at the sustained rate, which is the shape real agent traffic has: a batch of work, then a wait.
§Bounds
The map holds one entry per (authenticated user, class) that has been
seen. Usernames come from the operator’s --auth-file, and the caller
only consults the limiter for authenticated users, so the map is
bounded by the credential count times two and cannot be grown by an
unauthenticated caller.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(
api_per_minute: Option<NonZeroU32>,
git_per_minute: Option<NonZeroU32>,
) -> Self
pub fn new( api_per_minute: Option<NonZeroU32>, git_per_minute: Option<NonZeroU32>, ) -> Self
A limiter with the given requests-per-minute ceilings. None for a
class means that class is not limited.
The ceilings are NonZeroU32 so that “limit to zero” — a value
that refuses every request forever and has no sensible
Retry-After — is not representable.