Skip to main content

RateLimitGate

Struct RateLimitGate 

Source
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

Source

pub const fn new(cooldown_secs: u64) -> Self

Creates an untripped gate whose Self::trip/Self::trip_verified blocks further requests for cooldown_secs.

Source

pub fn is_tripped(&self) -> bool

Whether a request should be short-circuited right now.

Source

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.

Source

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.

Source

pub fn trip(&self)

Trips the gate until now + cooldown_secs, recording that this trip was not backed by confirmed evidence (see Self::verified).

Source

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.

Trait Implementations§

Source§

impl Debug for RateLimitGate

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more