Skip to main content

KeyedRateLimiter

Struct KeyedRateLimiter 

Source
pub struct KeyedRateLimiter<B: RateLimitBackend> { /* private fields */ }
Available on crate feature in-memory only.
Expand description

Per-key rate limiter that tracks separate rate limits for each key.

Keys either use the default quota or a per-key override set via with_quota_for_key. Overrides live in a DashMap consulted by borrowed key on every check; the check decision itself is delegated straight to the shared backend, so the warm path performs no allocation.

§Example

use throttle_kit::{KeyedRateLimiter, Quota, InMemoryBackend};

#[tokio::main]
async fn main() {
    let backend = InMemoryBackend::new();
    let limiter = KeyedRateLimiter::new(Quota::per_second(100), backend);

    // Tighter limit for login endpoint
    limiter.with_quota_for_key("login", Quota::per_second(10));

    let result = limiter.check("192.168.1.1").await;
    assert!(result.allowed);
}

Implementations§

Source§

impl<B: RateLimitBackend> KeyedRateLimiter<B>

Source

pub fn new(default_quota: Quota, backend: B) -> Self

Create a new keyed rate limiter with the given default quota and backend.

Source

pub fn with_quota_for_key(&self, key: &str, quota: Quota)

Override the rate limit quota for a specific key.

This replaces any existing override for the key. The next call to check uses this quota.

Source

pub async fn check(&self, key: &str) -> RateLimitResult

Check whether the caller identified by key is allowed to proceed.

The warm path (any repeated check) is allocation-free: the override map is read by borrowed key, and the decision comes straight from the shared backend — no per-key limiter is materialized or cloned. The map guard is never held across the backend await.

Source

pub fn check_sync(&self, key: &str) -> RateLimitResult

Synchronous version of check.

Blocks the current thread until the check completes. Useful for synchronous frameworks (e.g. Actix, Rocket).

Auto Trait Implementations§

§

impl<B> !RefUnwindSafe for KeyedRateLimiter<B>

§

impl<B> Freeze for KeyedRateLimiter<B>
where Arc<B>: Freeze,

§

impl<B> Send for KeyedRateLimiter<B>
where Arc<B>: Send,

§

impl<B> Sync for KeyedRateLimiter<B>
where Arc<B>: Sync,

§

impl<B> Unpin for KeyedRateLimiter<B>
where Arc<B>: Unpin,

§

impl<B> UnsafeUnpin for KeyedRateLimiter<B>
where Arc<B>: UnsafeUnpin,

§

impl<B> UnwindSafe for KeyedRateLimiter<B>
where Arc<B>: 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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