RateLimiter

Struct RateLimiter 

Source
pub struct RateLimiter { /* private fields */ }
Expand description

RateLimiter is the main entry point for rate limiting

It can be used in single-tier mode or multi-tier mode.

Implementations§

Source§

impl RateLimiter

Source

pub fn builder() -> RateLimiterBuilder

Create a builder for configuring the rate limiter

§Example
use rateflow::{RateLimiter, Window};

let limiter = RateLimiter::builder()
    .limit(100)
    .window(Window::Minute)
    .build()
    .unwrap();
Source

pub async fn check(&self, client_id: impl Into<String>) -> bool

Check if a request is allowed for the given client

Returns true if the request should be allowed, false otherwise.

§Example
use rateflow::{RateLimiter, Window};

let limiter = RateLimiter::builder()
    .limit(10)
    .window(Window::Second)
    .build()
    .unwrap();

if limiter.check("user@example.com").await {
    println!("Request allowed");
} else {
    println!("Rate limit exceeded");
}
Source

pub async fn check_tier( &self, tier: impl Into<String>, client_id: impl Into<String>, ) -> bool

Check if a request is allowed for the given client and tier

Returns true if the request should be allowed, false otherwise.

§Example
use rateflow::{RateLimiter, Tier, Window};

let limiter = RateLimiter::builder()
    .add_tier(Tier::new("free").limit(100))
    .add_tier(Tier::new("premium").limit(1000))
    .build()
    .unwrap();

if limiter.check_tier("premium", "user@example.com").await {
    println!("Request allowed");
}
Source

pub async fn reset(&self, client_id: impl Into<String>)

Reset the state for a specific client

This removes all tracking data for the client, effectively resetting their rate limit.

Source

pub async fn reset_tier( &self, tier: impl Into<String>, client_id: impl Into<String>, )

Reset the state for a specific client and tier

Source

pub fn client_count(&self) -> usize

Get the number of clients currently being tracked

Auto Trait Implementations§

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, 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, 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.