pub struct GcraRateLimiter { /* private fields */ }Expand description
Sharded GCRA limiter over one or more stacked windows (DW-017).
Implements the RateLimiter trait: check(key, cost) reserves
cost units in every stacked window for key (see the module docs
for the stop-at-first-denial consumption semantics). Keys are opaque
strings; the caller composes them (see RateLimitEngine).
Clocks: the GCRA math runs on governor’s own quanta monotonic
clock; there is deliberately no clock injection for it. The EVICTION
bookkeeping (per-key last-touch stamps) has a separate, injectable
millisecond clock: new uses elapsed monotonic time, tests use
GcraRateLimiter::with_clock to advance deterministically. The two
clocks only meet in the idle-eviction threshold (one full refill of
the window’s bucket), where both advance with wall time.
Implementations§
Source§impl GcraRateLimiter
impl GcraRateLimiter
Sourcepub fn new(specs: Vec<GcraWindowSpec>) -> Option<Self>
pub fn new(specs: Vec<GcraWindowSpec>) -> Option<Self>
New limiter over the given window specs (any order; internally
sorted shortest window first). Returns None for an empty spec
list — a limiter with no windows cannot make decisions.
Sourcepub fn with_clock(
specs: Vec<GcraWindowSpec>,
now_ms: fn() -> u64,
) -> Option<Self>
pub fn with_clock( specs: Vec<GcraWindowSpec>, now_ms: fn() -> u64, ) -> Option<Self>
New limiter using a caller-supplied millisecond clock for the per-key eviction bookkeeping (NOT the GCRA decisions — see the clocks note in the type docs).
Intended for tests and other time-controlled setups; production
code should prefer GcraRateLimiter::new. now_ms must return
non-decreasing milliseconds and is called once per window per
check (cheap).
Sourcepub fn live_keys(&self) -> usize
pub fn live_keys(&self) -> usize
Live per-key cell count across all stacked windows (sum of the
shard maps; approximate under concurrent checks). Bounded by
GCRA_STORE_SHARDS * crate::config::limits::MAX_RATE_LIMITER_KEYS_PER_SHARD per
window — the bound the spray tests assert.
Sourcepub fn evictions(&self) -> u64
pub fn evictions(&self) -> u64
Cells dropped by eviction sweeps across all stacked windows (monotonic for the limiter’s lifetime; a reload builds a fresh limiter, so the counter resets with the generation).
Sourcepub fn check(&self, key: &str, cost: u32) -> GcraOutcome
pub fn check(&self, key: &str, cost: u32) -> GcraOutcome
Check-and-reserve cost units for key across all stacked
windows (see module docs). cost 0 is treated as 1 (a request
always costs at least one unit); a cost larger than any window’s
bucket is always denied.
Trait Implementations§
Source§impl Debug for GcraRateLimiter
impl Debug for GcraRateLimiter
Source§impl RateLimiter for GcraRateLimiter
impl RateLimiter for GcraRateLimiter
Source§fn check<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
cost: u32,
) -> Pin<Box<dyn Future<Output = Result<RateDecision, ExtensionsError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn check<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
cost: u32,
) -> Pin<Box<dyn Future<Output = Result<RateDecision, ExtensionsError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
cost units for key.Auto Trait Implementations§
impl Freeze for GcraRateLimiter
impl RefUnwindSafe for GcraRateLimiter
impl Send for GcraRateLimiter
impl Sync for GcraRateLimiter
impl Unpin for GcraRateLimiter
impl UnwindSafe for GcraRateLimiter
Blanket Implementations§
§impl<T> AnyExt for T
impl<T> AnyExt for T
§fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
fn downcast_ref<T>(this: &Self) -> Option<&T>where
T: Any,
T behind reference§fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
fn downcast_mut<T>(this: &mut Self) -> Option<&mut T>where
T: Any,
T behind mutable reference§fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
fn downcast_rc<T>(this: Rc<Self>) -> Result<Rc<T>, Rc<Self>>where
T: Any,
T behind Rc pointer§fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
fn downcast_arc<T>(this: Arc<Self>) -> Result<Arc<T>, Arc<Self>>where
T: Any,
T behind Arc pointer§fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>where
T: Any,
fn downcast_box<T>(this: Box<Self>) -> Result<Box<T>, Box<Self>>where
T: Any,
T behind Box pointer§fn downcast_move<T>(this: Self) -> Option<T>
fn downcast_move<T>(this: Self) -> Option<T>
Self to T,
useful only in generic context as a workaround for specializationSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T, X> CoerceTo<T> for Xwhere
T: CoerceFrom<X> + ?Sized,
impl<T, X> CoerceTo<T> for Xwhere
T: CoerceFrom<X> + ?Sized,
fn coerce_rc_to(self: Rc<X>) -> Rc<T>
fn coerce_box_to(self: Box<X>) -> Box<T>
fn coerce_ref_to(&self) -> &T
fn coerce_mut_to(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more