&ip_addr,
rate_limit.message,
rate_limit.message_per_second,
- false,
)?;
drop(limiter);
&ip_addr,
rate_limit.post,
rate_limit.post_per_second,
- true,
)?;
}
RateLimitType::Register => {
&ip_addr,
rate_limit.register,
rate_limit.register_per_second,
- true,
)?;
}
RateLimitType::Image => {
&ip_addr,
rate_limit.image,
rate_limit.image_per_second,
- false,
)?;
}
RateLimitType::Comment => {
&ip_addr,
rate_limit.comment,
rate_limit.comment_per_second,
- false,
)?;
}
};
let res = fut.await;
- // after
- {
- let mut limiter = self.rate_limiter.lock().await;
- if res.is_ok() {
- match self.type_ {
- RateLimitType::Post => {
- limiter.check_rate_limit_full(
- self.type_,
- &ip_addr,
- rate_limit.post,
- rate_limit.post_per_second,
- false,
- )?;
- }
- RateLimitType::Register => {
- limiter.check_rate_limit_full(
- self.type_,
- &ip_addr,
- rate_limit.register,
- rate_limit.register_per_second,
- false,
- )?;
- }
- _ => (),
- };
- }
- }
-
res
}
}
ip: &IpAddr,
rate: i32,
per: i32,
- check_only: bool,
) -> Result<(), LemmyError> {
self.insert_ip(ip);
if let Some(bucket) = self.buckets.get_mut(&type_) {
rate_limit.last_checked = current;
rate_limit.allowance += time_passed * (rate as f64 / per as f64);
- if !check_only && rate_limit.allowance > rate as f64 {
+ if rate_limit.allowance > rate as f64 {
rate_limit.allowance = rate as f64;
}
"too_many_requests",
))
} else {
- if !check_only {
- rate_limit.allowance -= 1.0;
- }
+ rate_limit.allowance -= 1.0;
Ok(())
}
} else {