Struct ThrottlesService

Source
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

Source

pub fn new( ip: String, max_attempts: u32, period: Duration, prefix: &str, ) -> Self

Creates a new ThrottlesService instance.

§Arguments
  • ip - The IP address to track
  • max_attempts - Maximum number of allowed attempts in the time period
  • period - Duration of the throttling window
  • prefix - Prefix for cache keys to avoid collisions
Source

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.

Source

pub fn key(&self) -> String

Generates the cache key for this IP.

Source

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.

Source

pub fn hit(&mut self, cache: &Cache)

Records an attempt (hit) from the IP.

Increments the attempt count and resets the expiration time.

Source

pub fn remove(&self, cache: &Cache)

Clears the attempt count for the IP.

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