From 72236490ce2e7e3079f075918bdf777e7151b7a5 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 14 Aug 2023 11:45:28 +0200 Subject: [PATCH] fix: restrict max active queries (#4178) --- crates/net/common/src/ratelimit.rs | 5 +++++ crates/net/dns/src/query.rs | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/net/common/src/ratelimit.rs b/crates/net/common/src/ratelimit.rs index e6d63e3331..26440ae3ca 100644 --- a/crates/net/common/src/ratelimit.rs +++ b/crates/net/common/src/ratelimit.rs @@ -27,6 +27,11 @@ impl RateLimit { RateLimit { rate, state, sleep: Box::pin(tokio::time::sleep_until(until)) } } + /// Returns the configured limit of the [RateLimit] + pub fn limit(&self) -> u64 { + self.rate.limit() + } + /// Checks if the [RateLimit] is ready to handle a new call pub fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<()> { match self.state { diff --git a/crates/net/dns/src/query.rs b/crates/net/dns/src/query.rs index 10e1fbf214..a3cba5caf1 100644 --- a/crates/net/dns/src/query.rs +++ b/crates/net/dns/src/query.rs @@ -78,8 +78,8 @@ impl QueryPool { return Poll::Ready(event) } - // queue in new queries - 'queries: loop { + // queue in new queries if we have capacity + 'queries: while self.active_queries.len() < self.rate_limit.limit() as usize { if self.rate_limit.poll_ready(cx).is_ready() { if let Some(query) = self.queued_queries.pop_front() { self.rate_limit.tick();