Skip to content

Commit

Permalink
net/kad: Bound maximum number of queries
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv committed Jul 3, 2024
1 parent a9b74d9 commit d250dcb
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions substrate/client/network/src/litep2p/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const MDNS_QUERY_INTERVAL: Duration = Duration::from_secs(30);
/// Minimum number of confirmations received before an address is verified.
const MIN_ADDRESS_CONFIRMATIONS: usize = 5;

/// Maximum number of in-flight `FIND_NODE` queries.
const MAX_INFLIGHT_FIND_NODE_QUERIES: usize = 16;

/// Discovery events.
#[derive(Debug)]
pub enum DiscoveryEvent {
Expand Down Expand Up @@ -469,7 +472,9 @@ impl Stream for Discovery {
match delay.poll_unpin(cx) {
Poll::Ready(()) => {
let num_peers = this.num_connected_peers();
if num_peers < this.discovery_only_if_under_num {
if num_peers < this.discovery_only_if_under_num &&
this.find_node_queries.len() < MAX_INFLIGHT_FIND_NODE_QUERIES
{
let peer = PeerId::random();
log::debug!(target: LOG_TARGET, "start next kademlia query for {peer:?}");

Expand All @@ -488,7 +493,9 @@ impl Stream for Discovery {
} else {
log::debug!(
target: LOG_TARGET,
"discovery is disabled as we have {num_peers} connected peers."
"discovery is paused: {num_peers}/{} connected peers and in flight queries: {}/{MAX_INFLIGHT_FIND_NODE_QUERIES}",
this.discovery_only_if_under_num,
this.find_node_queries.len(),
);
}

Expand Down

0 comments on commit d250dcb

Please sign in to comment.