simplify endpoint limiter (#6122)

## Problem

1. Using chrono for durations only is wasteful
2. The arc/mutex was not being utilised
3. Locking every shard in the dashmap every GC could cause latency
spikes
4. More buckets

## Summary of changes

1. Use `Instant` instead of `NaiveTime`.
2. Remove the `Arc<Mutex<_>>` wrapper, utilising that dashmap entry
returns mut access
3. Clear only a random shard, update gc interval accordingly
4. Multiple buckets can be checked before allowing access

When I benchmarked the check function, it took on average 811ns when
multithreaded over the course of 10 million checks.
This commit is contained in:
Conrad Ludgate
2023-12-13 13:53:23 +00:00
committed by GitHub
parent 8460654f61
commit c8316b7a3f
4 changed files with 94 additions and 41 deletions

View File

@@ -9,7 +9,7 @@ use crate::{
console::{self, errors::WakeComputeError, messages::MetricsAuxInfo, Api},
http::StatusCode,
protocol2::WithClientIp,
rate_limiter::EndpointRateLimiter,
rate_limiter::{EndpointRateLimiter, RateBucketInfo},
stream::{PqStream, Stream},
usage_metrics::{Ids, USAGE_METRICS},
};
@@ -308,7 +308,10 @@ pub async fn task_main(
let connections = tokio_util::task::task_tracker::TaskTracker::new();
let cancel_map = Arc::new(CancelMap::default());
let endpoint_rate_limiter = Arc::new(EndpointRateLimiter::new(config.endpoint_rps_limit));
let endpoint_rate_limiter = Arc::new(EndpointRateLimiter::new([RateBucketInfo::new(
config.endpoint_rps_limit,
time::Duration::from_secs(1),
)]));
while let Some(accept_result) =
run_until_cancelled(listener.accept(), &cancellation_token).await