Skip to main content

RateLimitBuilder

Struct RateLimitBuilder 

Source
pub struct RateLimitBuilder<K, S = (), P = u64, F = DefaultResponseFactory> { /* private fields */ }
Expand description

Builder for a rate-limit layer with compile-time store/resolver/factory types.

Implementations§

Source§

impl<K, S, P, F> RateLimitBuilder<K, S, P, F>

Source

pub fn with_store<S2>(self, store: S2) -> RateLimitBuilder<K, S2, P, F>

Inject the rate-limit store and update the builder’s store type state.

Source

pub fn limit(self, limit: u64) -> RateLimitBuilder<K, S, u64, F>

Set a fixed quota limit and update the provider type state.

Source

pub fn limit_provider<P2>( self, limit_provider: P2, ) -> RateLimitBuilder<K, S, P2, F>

Replace the fixed provider with a custom asynchronous limit provider.

Source

pub fn response_factory<F2>( self, response_factory: F2, ) -> RateLimitBuilder<K, S, P, F2>

Replace the response factory and update its type state.

Source

pub fn window(self, window: Duration) -> Self

Set the fixed-window duration.

Source

pub fn policy_name(self, policy_name: impl Into<String>) -> Self

Set the stable policy identifier used in the scoped key and response metadata.

Source

pub fn with_key_encoder<E>(self, encoder: E) -> Self
where E: Fn(&str) -> String + Send + Sync + 'static,

Encode the scoped key before passing it to the crate::Store.

The callback runs in the middleware future’s polling path. It must be deterministic, non-blocking, free of I/O, collision-resistant for the caller’s key space, and non-panicking. Without this method, the complete scoped key is passed to the Store unchanged.

Source

pub fn skip<Predicate>(self, predicate: Predicate) -> Self
where Predicate: Fn(&Request<()>) -> bool + Send + Sync + 'static,

Bypass rate limiting when predicate returns true for the request.

The predicate receives the request head and extensions with a unit body. It runs synchronously before client-key extraction and must be non-blocking, free of I/O, and non-panicking. A bypassed request calls the inner service without resolving a limit, charging the Store, or adding rate-limit context or response fields.

Source

pub fn store_failure_mode(self, mode: StoreFailureMode) -> Self

Select the mode to use when the Store fails.

Source

pub fn store_failure_tracing_level(self, level: Level) -> Self

Select the tracing level used for Store failure events.

This configuration is available with the tracing Cargo feature. The default is tracing::Level::WARN. It affects both StoreFailureMode::Allow and StoreFailureMode::Reject events.

Source

pub fn rate_limit_fields(self, fields: RateLimitFields) -> Self

Select the Rate Limit Fields revision emitted in responses.

Source§

impl<K, S, P, F> RateLimitBuilder<K, S, P, F>
where S: Store, P: LimitProvider,

Source

pub fn build(self) -> Result<RateLimitLayer<K, S, P, F>, ConfigError>

Validate the builder and produce a configured layer.

Trait Implementations§

Source§

impl<K: Debug, S: Debug, P: Debug, F: Debug> Debug for RateLimitBuilder<K, S, P, F>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, S = (), P = u64, F = DefaultResponseFactory> !RefUnwindSafe for RateLimitBuilder<K, S, P, F>

§

impl<K, S = (), P = u64, F = DefaultResponseFactory> !UnwindSafe for RateLimitBuilder<K, S, P, F>

§

impl<K, S, P, F> Freeze for RateLimitBuilder<K, S, P, F>
where K: Freeze, S: Freeze, P: Freeze, F: Freeze,

§

impl<K, S, P, F> Send for RateLimitBuilder<K, S, P, F>
where K: Send, S: Send, P: Send, F: Send,

§

impl<K, S, P, F> Sync for RateLimitBuilder<K, S, P, F>
where K: Sync, S: Sync, P: Sync, F: Sync,

§

impl<K, S, P, F> Unpin for RateLimitBuilder<K, S, P, F>
where K: Unpin, S: Unpin, P: Unpin, F: Unpin,

§

impl<K, S, P, F> UnsafeUnpin for RateLimitBuilder<K, S, P, F>

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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