pub struct RateLimiter { /* private fields */ }Expand description
Rate limiter implementation.
§Example
use auth_framework::utils::rate_limit::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::new(5, Duration::from_secs(60));
assert!(limiter.is_allowed("client-1"));Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(max_requests: u32, window: Duration) -> Self
pub fn new(max_requests: u32, window: Duration) -> Self
Create a new rate limiter.
§Example
use auth_framework::utils::rate_limit::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::new(100, Duration::from_secs(60));Sourcepub fn check_rate_limit(&self, key: &str) -> Result<bool>
pub fn check_rate_limit(&self, key: &str) -> Result<bool>
Check if a request is allowed for the given key.
§Example
use auth_framework::utils::rate_limit::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::new(2, Duration::from_secs(60));
assert_eq!(limiter.check_rate_limit("k").unwrap(), true);
assert_eq!(limiter.check_rate_limit("k").unwrap(), true);
assert_eq!(limiter.check_rate_limit("k").unwrap(), false);Sourcepub fn is_allowed(&self, key: &str) -> bool
pub fn is_allowed(&self, key: &str) -> bool
Alias for check_rate_limit for compatibility.
§Example
use auth_framework::utils::rate_limit::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::new(1, Duration::from_secs(60));
assert!(limiter.is_allowed("k"));
assert!(!limiter.is_allowed("k"));Sourcepub fn remaining_requests(&self, key: &str) -> Result<u32>
pub fn remaining_requests(&self, key: &str) -> Result<u32>
Alias for get_remaining_requests for compatibility.
§Example
use auth_framework::utils::rate_limit::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::new(5, Duration::from_secs(60));
assert_eq!(limiter.remaining_requests("k").unwrap(), 5);Sourcepub fn get_request_count(&self, key: &str) -> Result<usize>
pub fn get_request_count(&self, key: &str) -> Result<usize>
Get the number of requests for a key.
§Example
use auth_framework::utils::rate_limit::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::new(10, Duration::from_secs(60));
limiter.is_allowed("k");
assert_eq!(limiter.get_request_count("k").unwrap(), 1);Sourcepub fn cleanup(&self) -> Result<usize>
pub fn cleanup(&self) -> Result<usize>
Clean up expired entries.
§Example
use auth_framework::utils::rate_limit::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::new(10, Duration::from_secs(60));
let removed = limiter.cleanup().unwrap();
assert_eq!(removed, 0);Sourcepub fn reset(&self, key: &str) -> Result<()>
pub fn reset(&self, key: &str) -> Result<()>
Reset rate limit for a specific key.
§Example
use auth_framework::utils::rate_limit::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::new(1, Duration::from_secs(60));
limiter.is_allowed("k");
limiter.reset("k").unwrap();
assert!(limiter.is_allowed("k"));Sourcepub fn get_remaining_requests(&self, key: &str) -> Result<u32>
pub fn get_remaining_requests(&self, key: &str) -> Result<u32>
Get remaining requests for a key.
§Example
use auth_framework::utils::rate_limit::RateLimiter;
use std::time::Duration;
let limiter = RateLimiter::new(5, Duration::from_secs(60));
limiter.is_allowed("k");
assert_eq!(limiter.get_remaining_requests("k").unwrap(), 4);Trait Implementations§
Source§impl Clone for RateLimiter
impl Clone for RateLimiter
Source§fn clone(&self) -> RateLimiter
fn clone(&self) -> RateLimiter
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for RateLimiter
impl RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
impl UnwindSafe for RateLimiter
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more