pub struct RateLimitGate { /* private fields */ }Expand description
Locally short-circuits further requests for a fixed cooldown once an upstream has rate-limited (or auth-rejected) this client.
A mechanism, not a policy: the gate holds no notion of scope. The caller decides
whether one gate covers a whole process (deps-github-actions: one GitHub API, one
gate) or one host each (deps-gitlab-ci, spec §9.3) by choosing where it stores it.
Both load and store use Ordering::Relaxed: a stale read costs at most one extra
doomed request (if a trip is not yet visible) or one extra short-circuit (if a clear
is not yet visible) — never a correctness violation, so no stronger ordering is needed.
§Examples
use deps_core::rate_limit::RateLimitGate;
let gate = RateLimitGate::new(300);
assert!(!gate.is_tripped());
gate.trip();
assert!(gate.is_tripped());Implementations§
Source§impl RateLimitGate
impl RateLimitGate
Sourcepub const fn new(cooldown_secs: u64) -> Self
pub const fn new(cooldown_secs: u64) -> Self
Creates an untripped gate whose Self::trip/Self::trip_verified blocks further
requests for cooldown_secs.
Sourcepub fn is_tripped(&self) -> bool
pub fn is_tripped(&self) -> bool
Whether a request should be short-circuited right now.
Sourcepub fn verified(&self) -> bool
pub fn verified(&self) -> bool
Whether the trip currently in effect was tripped via Self::trip_verified rather
than the plain Self::trip (#1295 critic S3). Meaningless when Self::is_tripped
is false — a caller short-circuiting on a tripped gate should build its error to match
this, instead of always reporting an unverified guess for every call within the
cooldown window regardless of how the first call actually confirmed it.
Sourcepub fn evidence(&self) -> RateLimitEvidence
pub fn evidence(&self) -> RateLimitEvidence
Self::verified, projected onto RateLimitEvidence — lets a caller building the
error for an already-tripped gate write some_rate_limit_error(gate.evidence()) instead
of re-deriving the if gate.verified() { .. } else { .. } branch at every call site
(#1480 item 8: this replaces GithubActionsRegistry::rate_limited_error’s and
GitlabCiRegistry::fetch_route’s identical hand-rolled versions of this projection).
Meaningless when Self::is_tripped is false, exactly like Self::verified itself.
Sourcepub fn trip(&self)
pub fn trip(&self)
Trips the gate until now + cooldown_secs, recording that this trip was not backed
by confirmed evidence (see Self::verified).
Sourcepub fn trip_verified(&self)
pub fn trip_verified(&self)
Like Self::trip, but records that this trip was backed by confirmed evidence —
Self::verified then returns true for as long as this trip stays in effect.