pub struct Limiter<T>{ /* private fields */ }
Implementations§
Source§impl<T> Limiter<T>
impl<T> Limiter<T>
pub fn new() -> Self
Sourcepub fn add_limited_entity(
&self,
entity: T,
max_limit: usize,
refresh_rate: Duration,
)
pub fn add_limited_entity( &self, entity: T, max_limit: usize, refresh_rate: Duration, )
Adds a entity to the limiter
entity
is something hashable like a IP, username, etc…
max_limit
is the max number of requests in the given timeframe that you allow for that specific entity
refresh_rate
is the timeframe after which the entity gets a renewed limit
Sourcepub fn remove_limited_entity(&self, entity: T) -> Option<AssociatedEntity>
pub fn remove_limited_entity(&self, entity: T) -> Option<AssociatedEntity>
Removes a entity from the limiter
Removes a key from the map, returning the value at the key if the key was previously in the map. Keeps the allocated memory for reuse.
The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.
Sourcepub fn is_entity_limited(&mut self, entity: &T) -> Option<bool>
pub fn is_entity_limited(&mut self, entity: &T) -> Option<bool>
Checks whether a entity has requests left to consume.
entity
has been added by you previously with add_limited_entity
§returns:
None
-> entity was not found by the limiter, create one with add_limited_entity
.
Some(false)
-> entity is rate limited, no requests to consume.
Some(true)
-> everything worked, entity had requests left.
Sourcepub fn get_bucket_remaining(&self, entity: &T) -> Option<usize>
pub fn get_bucket_remaining(&self, entity: &T) -> Option<usize>
Returns the current amount of requests left in the entity’s bucket.
entity
has been added by you previously with add_limited_entity
§returns:
None
-> entity was not found by the limiter, create one with add_limited_entity
.
Some(usize)
-> the current number of requests left in the entity’s bucket.