pub struct ThrottlesService { /* private fields */ }Expand description
A service for throttling attempts from an IP address.
Tracks the number of attempts (hits) from a given IP address and determines whether further attempts should be allowed based on configured limits.
§Examples
use std::time::Duration;
use cache_ro::Cache;
use throttle_ro::ThrottlesService;
let cache = Cache::new(Default::default()); // In real usage, configure properly
let ip = "127.0.0.1".to_string();
let mut service = ThrottlesService::new(
ip,
5, // max attempts
Duration::from_secs(60), // time window
"rate_limit_"
);
if service.can_go(&cache) {
service.hit(&cache);
// Process the request
} else {
// Reject the request - rate limit exceeded
}Implementations§
Source§impl ThrottlesService
impl ThrottlesService
Sourcepub fn new(
ip: String,
max_attempts: u32,
period: Duration,
prefix: &str,
) -> Self
pub fn new( ip: String, max_attempts: u32, period: Duration, prefix: &str, ) -> Self
Creates a new ThrottlesService instance.
§Arguments
ip- The IP address to trackmax_attempts- Maximum number of allowed attempts in the time periodperiod- Duration of the throttling windowprefix- Prefix for cache keys to avoid collisions
Sourcepub fn can_go(&mut self, cache: &Cache) -> bool
pub fn can_go(&mut self, cache: &Cache) -> bool
Checks whether the IP is allowed to make another attempt.
Returns true if the current attempt count is below the maximum allowed.
Sourcepub fn get_expire(&mut self, cache: &Cache) -> Duration
pub fn get_expire(&mut self, cache: &Cache) -> Duration
Gets the remaining duration for the current throttling window.
Returns the configured period if no expiration is set in the cache.
Auto Trait Implementations§
impl Freeze for ThrottlesService
impl RefUnwindSafe for ThrottlesService
impl Send for ThrottlesService
impl Sync for ThrottlesService
impl Unpin for ThrottlesService
impl UnwindSafe for ThrottlesService
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