Skip to main content

RateLimit

Struct RateLimit 

Source
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>

Source

pub fn new( inner: Inner, config: TokenBucketConfig, extractor: Extractor, ) -> Self

Source

pub fn with_caps( inner: Inner, config: TokenBucketConfig, extractor: Extractor, caps: RateLimitCaps, ) -> Self

Source§

impl<Inner, Extractor, Clk> RateLimit<Inner, Extractor, Clk>

Source

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.

Source

pub fn with_action(self, action: ExceededAction) -> Self

Source§

impl<Inner, Extractor, Clk: Clock> RateLimit<Inner, Extractor, Clk>

Source

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>

Source

pub fn from_spec(inner: PipeHandle, value: &Value) -> Result<Self, ProximaError>

Trait Implementations§

Source§

impl<Extractor, Clk> Pipe for RateLimit<ThreadLocalPipeHandle, Extractor, Clk>
where Request<Bytes>: KeyOf<Extractor, Rejection = Response<Bytes>>, Extractor: Clone + Send + Sync + 'static, Clk: Clock + Send + Sync + 'static,

Source§

type In = Request<Bytes>

Input the pipe consumes.
Source§

type Out = Response<Bytes>

Output the pipe produces.
Source§

type Err = ProximaError

Error the pipe can fail with. Debug + 'static; Send is added only on the cross-core SendPipe form.
Source§

fn call( &self, input: Request<Bytes>, ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>>

Apply the pipe. The returned future is NOT required to be Send.
Source§

impl<Inner, Extractor, Clk> SendPipe for RateLimit<Inner, Extractor, Clk>
where Inner: SendPipe + Clone + Send + Sync + 'static, Inner::In: KeyOf<Extractor, Rejection = Inner::Out> + Labeled + Send + 'static, Inner::Out: Send + 'static, Inner::Err: Send + 'static, Extractor: Clone + Send + Sync + 'static, Clk: Clock + Send + Sync + 'static,

Source§

type In = <Inner as SendPipe>::In

Input the pipe consumes.
Source§

type Out = <Inner as SendPipe>::Out

Output the pipe produces.
Source§

type Err = <Inner as SendPipe>::Err

Error the pipe can fail with — Send here so it can cross cores.
Source§

fn call( &self, input: Inner::In, ) -> impl Future<Output = Result<Inner::Out, Inner::Err>> + Send

Apply the pipe. The returned future IS Send.

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>
where Inner: RefUnwindSafe, Extractor: RefUnwindSafe, Clk: RefUnwindSafe,

§

impl<Inner, Extractor, Clk> Send for RateLimit<Inner, Extractor, Clk>
where Inner: Send, Extractor: Send, Clk: Send,

§

impl<Inner, Extractor, Clk> Sync for RateLimit<Inner, Extractor, Clk>
where Inner: Sync, Extractor: Sync, Clk: Sync,

§

impl<Inner, Extractor, Clk> Unpin for RateLimit<Inner, Extractor, Clk>
where Inner: Unpin, Extractor: Unpin, Clk: Unpin,

§

impl<Inner, Extractor, Clk> UnsafeUnpin for RateLimit<Inner, Extractor, Clk>
where Inner: UnsafeUnpin, Extractor: UnsafeUnpin, Clk: UnsafeUnpin,

§

impl<Inner, Extractor, Clk> UnwindSafe for RateLimit<Inner, Extractor, Clk>
where Inner: UnwindSafe, Extractor: UnwindSafe, Clk: UnwindSafe,

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<P> DynPipe<<P as Pipe>::In, <P as Pipe>::Out> for P
where P: Pipe, ProximaError: From<<P as Pipe>::Err>,

Source§

fn call_dyn( &self, input: <P as Pipe>::In, ) -> Pin<Box<dyn Future<Output = Result<<P as Pipe>::Out, ProximaError>> + '_>>

Erased dispatch — boxes the inner pipe’s future and converts its error into ProximaError.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<Implementor> Handler for Implementor
where Implementor: SendPipe<In = Request<Bytes>, Out = Response<Bytes>, Err = ProximaError>,

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<P> PipeExt for P
where P: Pipe,

Source§

fn and_then<Next>(self, next: Next) -> AndThen<Self, Next>
where Next: Pipe<In = Self::Out>, Next::Err: From<Self::Err>,

self.and_then(next) is AndThen::new(self, next) — reads left-to-right at the call site.
Source§

fn filter<Pred>(self, predicate: Pred) -> AndThen<Pred, Self>
where Pred: Pipe<Out = Self::In>, Self::Err: From<Pred::Err>,

Gate self behind predicate.
Source§

fn fanout(self, other: Self) -> FanOut<Self, AllOrNothing>
where Self: Clone, Self::In: Clone,

Broadcast to self and other, all-or-nothing.
Source§

fn fanin<Strategy>( self, other: Self, strategy: Strategy, ) -> FanIn<Self, Strategy, 2>
where Self: UnpinPipe<In = (), Err = Exhausted> + DropSafe, Strategy: FanInStrategy,

Merge self and other into a two-source FanIn. FanIn sources are UnpinPipe-shaped (In = (), Err = Exhausted), hence the extra bound.
Source§

impl<P> SendDynPipe<<P as SendPipe>::In, <P as SendPipe>::Out> for P
where P: SendPipe, <P as SendPipe>::In: Send, ProximaError: From<<P as SendPipe>::Err>,

Source§

fn call_dyn( &self, input: <P as SendPipe>::In, ) -> Pin<Box<dyn Future<Output = Result<<P as SendPipe>::Out, ProximaError>> + Send + '_>>

Erased dispatch — boxes the inner pipe’s Send future and converts its error into ProximaError.
Source§

impl<Implementor> SourcePipe for Implementor
where Implementor: SendPipe<In = Signal, Out = (), Err = ProximaError>,

Source§

impl<Implementor> ThreadLocalHandler for Implementor
where Implementor: Pipe<In = Request<Bytes>, Out = Response<Bytes>, Err = ProximaError>,

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