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
impl RateLimiter
Sourcepub fn builder() -> RateLimiterBuilder
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();Sourcepub async fn check(&self, client_id: impl Into<String>) -> bool
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");
}Sourcepub async fn check_tier(
&self,
tier: impl Into<String>,
client_id: impl Into<String>,
) -> bool
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");
}Sourcepub async fn reset(&self, client_id: impl Into<String>)
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.
Sourcepub async fn reset_tier(
&self,
tier: impl Into<String>,
client_id: impl Into<String>,
)
pub async fn reset_tier( &self, tier: impl Into<String>, client_id: impl Into<String>, )
Reset the state for a specific client and tier
Sourcepub fn client_count(&self) -> usize
pub fn client_count(&self) -> usize
Get the number of clients currently being tracked
Auto Trait Implementations§
impl Freeze for RateLimiter
impl !RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl !UnwindSafe for RateLimiter
Blanket Implementations§
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
Mutably borrows from an owned value. Read more