pub struct RateLimit<Inner = PipeHandle, Extractor = KeyExtractor, Clk = TimeClock> { /* private fields */ }Expand description
Token-bucket rate limiter. Generic over the inner handle, the extractor
type, AND the clock — mirrors resilience::Retry<Inner, Clk>’s injected-clock
seam so refill timing is deterministically testable. Clk defaults to
TimeClock, the production monotonic clock, so every existing caller
(RateLimit::new, RateLimitConfig, RateLimitFactory) is unaffected.
Sans-IO: the limiter owns no runtime and spawns no task. Map growth is bounded
by caps.max_keys (LRU eviction on insert, inline on the call path). Proactive
reclamation of idle buckets is the host’s job — call sweep_idle on whatever
cadence fits (read caps.sweep_period for the recommended interval) from a
proxima_core::time::interval loop on your own runtime. That keeps the primitive
runtime-agnostic (prime, tokio, embassy, bare-metal — it neither knows nor
cares which drives the host’s tick).
Implementations§
Source§impl<Inner, Extractor> RateLimit<Inner, Extractor, TimeClock>
impl<Inner, Extractor> RateLimit<Inner, Extractor, TimeClock>
pub fn new( inner: Inner, config: TokenBucketConfig, extractor: Extractor, ) -> Self
pub fn with_caps( inner: Inner, config: TokenBucketConfig, extractor: Extractor, caps: RateLimitCaps, ) -> Self
Source§impl<Inner, Extractor, Clk> RateLimit<Inner, Extractor, Clk>
impl<Inner, Extractor, Clk> RateLimit<Inner, Extractor, Clk>
Sourcepub fn with_clock(
inner: Inner,
config: TokenBucketConfig,
extractor: Extractor,
caps: RateLimitCaps,
clock: Clk,
) -> Self
pub fn with_clock( inner: Inner, config: TokenBucketConfig, extractor: Extractor, caps: RateLimitCaps, clock: Clk, ) -> Self
Materialise with an explicit clock — the seam a deterministic test or
example injects a fake clock through; production code goes via new
/ with_caps, which default Clk to TimeClock.
pub fn with_action(self, action: ExceededAction) -> Self
Source§impl<Inner, Extractor, Clk: Clock> RateLimit<Inner, Extractor, Clk>
impl<Inner, Extractor, Clk: Clock> RateLimit<Inner, Extractor, Clk>
Sourcepub fn sweep_idle(&self)
pub fn sweep_idle(&self)
Evict buckets idle longer than caps.idle_ttl. Sans-IO: the host calls
this on its own cadence (the limiter spawns nothing). Bounded growth does
not depend on it — caps.max_keys already caps the map via LRU eviction
on insert; this just reclaims idle memory sooner.
Source§impl RateLimit<PipeHandle, KeyExtractor, TimeClock>
impl RateLimit<PipeHandle, KeyExtractor, TimeClock>
pub fn from_spec(inner: PipeHandle, value: &Value) -> Result<Self, ProximaError>
Trait Implementations§
Source§impl<Extractor, Clk> Pipe for RateLimit<ThreadLocalPipeHandle, Extractor, Clk>
impl<Extractor, Clk> Pipe for RateLimit<ThreadLocalPipeHandle, Extractor, Clk>
Source§type Err = ProximaError
type Err = ProximaError
Auto Trait Implementations§
impl<Inner = Arc<dyn SendDynPipe<Request<Bytes>, Response<Bytes>>>, Extractor = KeyExtractor, Clk = TimeClock> !Freeze for RateLimit<Inner, Extractor, Clk>
impl<Inner, Extractor, Clk> RefUnwindSafe for RateLimit<Inner, Extractor, Clk>
impl<Inner, Extractor, Clk> Send for RateLimit<Inner, Extractor, Clk>
impl<Inner, Extractor, Clk> Sync for RateLimit<Inner, Extractor, Clk>
impl<Inner, Extractor, Clk> Unpin for RateLimit<Inner, Extractor, Clk>
impl<Inner, Extractor, Clk> UnsafeUnpin for RateLimit<Inner, Extractor, Clk>
impl<Inner, Extractor, Clk> UnwindSafe for RateLimit<Inner, Extractor, Clk>
Blanket Implementations§
Source§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<Implementor> Handler for Implementor
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<P> PipeExt for Pwhere
P: Pipe,
impl<P> PipeExt for Pwhere
P: Pipe,
Source§fn and_then<Next>(self, next: Next) -> AndThen<Self, Next>
fn and_then<Next>(self, next: Next) -> AndThen<Self, Next>
self.and_then(next) is AndThen::new(self, next) — reads
left-to-right at the call site.Source§fn fanout(self, other: Self) -> FanOut<Self, AllOrNothing>
fn fanout(self, other: Self) -> FanOut<Self, AllOrNothing>
self and other, all-or-nothing.