Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink capacity in RateLimitStorage::remove_older_than #3536

Merged
merged 7 commits into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions crates/utils/src/rate_limit/rate_limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use enum_map::{enum_map, EnumMap};
use once_cell::sync::Lazy;
use std::{
collections::HashMap,
hash::Hash,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
time::{Duration, Instant},
};
Expand Down Expand Up @@ -206,20 +207,29 @@ impl RateLimitStorage {
.all(|bucket| bucket.last_checked.to_instant() > instant)
};

self.ipv4_buckets.retain(|_, group| is_recently_used(group));
retain_and_shrink(&mut self.ipv4_buckets, |_, group| is_recently_used(group));

self.ipv6_buckets.retain(|_, group_48| {
group_48.children.retain(|_, group_56| {
group_56
.children
.retain(|_, group_64| is_recently_used(group_64));
retain_and_shrink(&mut self.ipv6_buckets, |_, group_48| {
retain_and_shrink(&mut group_48.children, |_, group_56| {
retain_and_shrink(&mut group_56.children, |_, group_64| {
is_recently_used(group_64)
});
!group_56.children.is_empty()
});
!group_48.children.is_empty()
})
}
}

fn retain_and_shrink<K, V, F>(map: &mut HashMap<K, V>, f: F)
where
K: Eq + Hash,
F: FnMut(&K, &mut V) -> bool,
{
map.retain(f);
map.shrink_to_fit();
}

fn split_ipv6(ip: Ipv6Addr) -> ([u8; 6], u8, u8) {
let [a0, a1, a2, a3, a4, a5, b, c, ..] = ip.octets();
([a0, a1, a2, a3, a4, a5], b, c)
Expand Down