Skip to main content

KeyedRateLimiter

Struct KeyedRateLimiter 

Source
pub struct KeyedRateLimiter<K: Hash + Eq> { /* private fields */ }
Expand description

Allows limit requests per per and key, as a burst, refilling one request every per / limit (generic cell rate algorithm: one timestamp per key). The keys are sharded, so concurrent requests from different keys rarely wait for each other.

It counts in this process: each instance of the app keeps its own counts. The per-IP limit and route throttles (RouteDef::throttle) count like this, and in a shared RateLimitStore too when http.rate_limit_store names one.

use std::time::Duration;
use navree_http::KeyedRateLimiter;

let limiter = KeyedRateLimiter::new(2, Duration::from_secs(60));
assert!(limiter.check(&"client-a").is_ok());
assert!(limiter.check(&"client-a").is_ok());
let limited = limiter.check(&"client-a").unwrap_err();
assert!(limited.retry_after > Duration::ZERO);
assert!(limiter.check(&"client-b").is_ok());

Implementations§

Source§

impl<K: Hash + Eq + Clone> KeyedRateLimiter<K>

Source

pub fn new(limit: u32, per: Duration) -> Self

limit requests per per (a limit of 0 is treated as 1).

Source

pub fn check(&self, key: &K) -> Result<(), Limited>

Counts a request for key: Limited when over the limit.

Trait Implementations§

Source§

impl<K: Debug + Hash + Eq> Debug for KeyedRateLimiter<K>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K> !RefUnwindSafe for KeyedRateLimiter<K>

§

impl<K> Forget for KeyedRateLimiter<K>

§

impl<K> Freeze for KeyedRateLimiter<K>
where DashMap<K, Slot>: Freeze,

§

impl<K> Move for KeyedRateLimiter<K>

§

impl<K> Send for KeyedRateLimiter<K>
where DashMap<K, Slot>: Send,

§

impl<K> Sync for KeyedRateLimiter<K>
where DashMap<K, Slot>: Sync,

§

impl<K> Unpin for KeyedRateLimiter<K>
where DashMap<K, Slot>: Unpin,

§

impl<K> UnsafeUnpin for KeyedRateLimiter<K>
where DashMap<K, Slot>: UnsafeUnpin,

§

impl<K> UnwindSafe for KeyedRateLimiter<K>
where DashMap<K, Slot>: 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<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> Same for T

Source§

type Output = T

Should always be Self
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