Struct RatePolicy
pub struct RatePolicy { /* private fields */ }Expand description
A Policy that limits the rate of inputs: one unit is spent from a
shared token bucket per input.
Two modes:
RatePolicy::abort: an input beyond the budget is aborted withRateLimitReached— the classic rate-limit path (e.g. mapped to429 Too Many Requests, using the carriedretry_after).RatePolicy::wait: an input beyond the budget waits until the bucket allows it — natural pacing, no error path.
Cloning shares the underlying RateLimiter (same budget), just like
cloning a ConcurrentPolicy shares its tracker. To share one budget
across differently-configured policies or stacks, construct a
RateLimiter yourself and use RatePolicy::abort_with_limiter /
RatePolicy::wait_with_limiter.
To combine “at most N in flight” with “at most M per second”, stack two limit layers, with the rate limit outside the concurrency limit (cheap rejection first, no concurrency slot consumed).
Implementations§
§impl RatePolicy
impl RatePolicy
pub fn wait(rate: Rate) -> RatePolicy
pub fn wait(rate: Rate) -> RatePolicy
Create a new RatePolicy that paces inputs beyond
the given Rate: they wait, they never fail.
pub fn abort(rate: Rate) -> RatePolicy
pub fn abort(rate: Rate) -> RatePolicy
Create a new RatePolicy that aborts inputs beyond
the given Rate with RateLimitReached.
pub fn wait_with_limiter(limiter: RateLimiter) -> RatePolicy
pub fn wait_with_limiter(limiter: RateLimiter) -> RatePolicy
Like RatePolicy::wait, with a caller-provided (shareable)
RateLimiter.
pub fn abort_with_limiter(limiter: RateLimiter) -> RatePolicy
pub fn abort_with_limiter(limiter: RateLimiter) -> RatePolicy
Like RatePolicy::abort, with a caller-provided (shareable)
RateLimiter.
pub fn with_burst(self, burst: u64) -> RatePolicy
pub fn with_burst(self, burst: u64) -> RatePolicy
Override the burst capacity (default: one period worth of units).
This rebuilds the policy’s own RateLimiter: any previously
shared budget handle is disconnected.
pub fn set_burst(&mut self, burst: u64) -> &mut RatePolicy
pub fn set_burst(&mut self, burst: u64) -> &mut RatePolicy
Override the burst capacity (default: one period worth of units).
This rebuilds the policy’s own RateLimiter: any previously
shared budget handle is disconnected.
pub fn limiter(&self) -> &RateLimiter
pub fn limiter(&self) -> &RateLimiter
The RateLimiter enforcing this policy’s budget
(clone it to share the budget elsewhere).
Trait Implementations§
§impl Clone for RatePolicy
impl Clone for RatePolicy
§fn clone(&self) -> RatePolicy
fn clone(&self) -> RatePolicy
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Debug for RatePolicy
impl Debug for RatePolicy
§impl<Input> Policy<Input> for RatePolicywhere
Input: Send + 'static,
impl<Input> Policy<Input> for RatePolicywhere
Input: Send + 'static,
§type Error = RateLimitReached
type Error = RateLimitReached
§async fn check(
&self,
input: Input,
) -> PolicyResult<Input, <RatePolicy as Policy<Input>>::Guard, <RatePolicy as Policy<Input>>::Error>
async fn check( &self, input: Input, ) -> PolicyResult<Input, <RatePolicy as Policy<Input>>::Guard, <RatePolicy as Policy<Input>>::Error>
Auto Trait Implementations§
impl !RefUnwindSafe for RatePolicy
impl !UnwindSafe for RatePolicy
impl Freeze for RatePolicy
impl Send for RatePolicy
impl Sync for RatePolicy
impl Unpin for RatePolicy
impl UnsafeUnpin for RatePolicy
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§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§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more