mcaptcha decrement by count

This commit is contained in:
Aravinth Manivannan 2021-06-06 16:14:52 +05:30
parent afaad883cd
commit 68f95f99c2
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
2 changed files with 24 additions and 0 deletions

View File

@ -264,6 +264,18 @@ pub mod tests {
.unwrap()
}
#[test]
fn mcaptcha_decrement_by_works() {
let mut m = get_mcaptcha();
for _ in 0..100 {
m.add_visitor();
}
m.decrement_visitor_by(50);
assert_eq!(m.get_visitors(), 50);
m.decrement_visitor_by(500);
assert_eq!(m.get_visitors(), 0);
}
#[actix_rt::test]
async fn counter_defense_tightenup_works() {
let addr: MyActor = get_counter().start();

View File

@ -98,6 +98,18 @@ impl MCaptcha {
}
}
/// decrements the visitor count by specified count
#[inline]
pub fn decrement_visitor_by(&mut self, count: u32) {
if self.visitor_threshold > 0 {
if self.visitor_threshold >= count {
self.visitor_threshold -= count;
} else {
self.visitor_threshold = 0;
}
}
}
/// get current difficulty factor
#[inline]
pub fn get_difficulty(&self) -> u32 {