mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-01 22:01:49 +00:00
Fix rate limiter (#4560)
This commit is contained in:
parent
ef4bb3cc40
commit
99d01e186a
1 changed files with 19 additions and 1 deletions
|
@ -158,7 +158,7 @@ impl<K: Eq + Hash, C: MapLevel> MapLevel for Map<K, C> {
|
||||||
|
|
||||||
// Evaluated if `some_children_remaining` is false
|
// Evaluated if `some_children_remaining` is false
|
||||||
let total_has_refill_in_future = || {
|
let total_has_refill_in_future = || {
|
||||||
group.total.into_iter().all(|(action_type, bucket)| {
|
group.total.into_iter().any(|(action_type, bucket)| {
|
||||||
#[allow(clippy::indexing_slicing)]
|
#[allow(clippy::indexing_slicing)]
|
||||||
let config = configs[action_type];
|
let config = configs[action_type];
|
||||||
bucket.update(now, config).tokens != config.capacity
|
bucket.update(now, config).tokens != config.capacity
|
||||||
|
@ -416,5 +416,23 @@ mod tests {
|
||||||
rate_limiter.remove_full_buckets(now);
|
rate_limiter.remove_full_buckets(now);
|
||||||
assert!(rate_limiter.ipv4_buckets.is_empty());
|
assert!(rate_limiter.ipv4_buckets.is_empty());
|
||||||
assert!(rate_limiter.ipv6_buckets.is_empty());
|
assert!(rate_limiter.ipv6_buckets.is_empty());
|
||||||
|
|
||||||
|
// `remove full buckets` should not remove empty buckets
|
||||||
|
let ip = "1.1.1.1".parse().unwrap();
|
||||||
|
// empty the bucket with 2 requests
|
||||||
|
assert!(rate_limiter.check(ActionType::Post, ip, now));
|
||||||
|
assert!(rate_limiter.check(ActionType::Post, ip, now));
|
||||||
|
|
||||||
|
rate_limiter.remove_full_buckets(now);
|
||||||
|
assert!(!rate_limiter.ipv4_buckets.is_empty());
|
||||||
|
|
||||||
|
// `remove full buckets` should not remove partial buckets
|
||||||
|
now.secs += 2;
|
||||||
|
let ip = "1.1.1.1".parse().unwrap();
|
||||||
|
// Only make one request, so bucket still has 1 token
|
||||||
|
assert!(rate_limiter.check(ActionType::Post, ip, now));
|
||||||
|
|
||||||
|
rate_limiter.remove_full_buckets(now);
|
||||||
|
assert!(!rate_limiter.ipv4_buckets.is_empty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue