pub struct Manager { /* private fields */ }
Expand description
Data used to limit requests.
One instance of this is used per Listener
.
It counts using a atomic::AtomicUsize
and keeps the count of requests per
IpAddr
in a Mutex
, for fast access times and cheap cloning.
Implementations§
source§impl LimitManager
impl LimitManager
sourcepub fn new(max_requests: usize, check_every: usize, reset_seconds: f64) -> Self
pub fn new(max_requests: usize, check_every: usize, reset_seconds: f64) -> Self
Creates a new manager.
Use LimitManager::default
for sane defaults.
The number of allowed requests per reset is max_requests * check_every
.
After reset_seconds
, all data is cleared.
As the math implies, increasing max_requests
and lowering reset_seconds
does nothing to the amount of accepted requests.
Though, if you have large reset_seconds
, it’ll take longer
for the limits to clear after the user has reached max_requests
.
sourcepub fn set_check_every(&mut self, check_every: usize) -> &mut Self
pub fn set_check_every(&mut self, check_every: usize) -> &mut Self
Sets the number of calls in between checking the request.
This is here to not have to check every request.
sourcepub fn set_max_requests(&mut self, max_requests: usize) -> &mut Self
pub fn set_max_requests(&mut self, max_requests: usize) -> &mut Self
Sets the max requests in the current cycle
(which resets after Self::set_reset_seconds
).
If the amount of requests from a IP address exceeds
max_requests
in the cycle, the request is denied.
sourcepub fn set_reset_seconds(&mut self, reset_seconds: f64) -> &mut Self
pub fn set_reset_seconds(&mut self, reset_seconds: f64) -> &mut Self
Sets the interval to clear all limits.
See Self::new
for considerations when making this value large.
sourcepub fn register(&self, addr: IpAddr) -> Action
pub fn register(&self, addr: IpAddr) -> Action
Registers a request from addr
.
This is called twice, once when a new connection is established, and once when a new request is made.
Does not always lock the Mutex
, only once per check_every
.
It only atomic::AtomicUsize::fetch_add
else, with atomic::Ordering::Relaxed
.
This is less reliable, but faster. We do not require this to be to be exact.