Skip to content

Commit

Permalink
Skip recursive discovery query if no useful ENRs (sigp#6207)
Browse files Browse the repository at this point in the history
* Skip recursive discovery query if no useful ENRs
  • Loading branch information
dapplion authored and chong-he committed Nov 26, 2024
1 parent 29d2678 commit 0d03914
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 6 additions & 0 deletions beacon_node/lighthouse_network/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ pub static DISCOVERY_SESSIONS: LazyLock<Result<IntGauge>> = LazyLock::new(|| {
"The number of active discovery sessions with peers",
)
});
pub static DISCOVERY_NO_USEFUL_ENRS: LazyLock<Result<IntCounter>> = LazyLock::new(|| {
try_create_int_counter(
"discovery_no_useful_enrs_found",
"Total number of counts a query returned no useful ENRs to dial",
)
});

pub static PEERS_PER_CLIENT: LazyLock<Result<IntGaugeVec>> = LazyLock::new(|| {
try_create_int_gauge_vec(
Expand Down
16 changes: 14 additions & 2 deletions beacon_node/lighthouse_network/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ impl<E: EthSpec> PeerManager<E> {
/// This function decides whether or not to dial these peers.
pub fn peers_discovered(&mut self, results: HashMap<Enr, Option<Instant>>) {
let mut to_dial_peers = 0;
let results_count = results.len();
let connected_or_dialing = self.network_globals.connected_or_dialing_peers();
for (enr, min_ttl) in results {
// There are two conditions in deciding whether to dial this peer.
Expand Down Expand Up @@ -352,8 +353,19 @@ impl<E: EthSpec> PeerManager<E> {
}
}

// Queue another discovery if we need to
self.maintain_peer_count(to_dial_peers);
// The heartbeat will attempt new discovery queries every N seconds if the node needs more
// peers. As an optimization, this function can recursively trigger new discovery queries
// immediatelly if we don't fulfill our peers needs after completing a query. This
// recursiveness results in an infinite loop in networks where there not enough peers to
// reach out target. To prevent the infinite loop, if a query returns no useful peers, we
// will cancel the recursiveness and wait for the heartbeat to trigger another query latter.
if results_count > 0 && to_dial_peers == 0 {
debug!(self.log, "Skipping recursive discovery query after finding no useful results"; "results" => results_count);
metrics::inc_counter(&metrics::DISCOVERY_NO_USEFUL_ENRS);
} else {
// Queue another discovery if we need to
self.maintain_peer_count(to_dial_peers);
}
}

/// A STATUS message has been received from a peer. This resets the status timer.
Expand Down

0 comments on commit 0d03914

Please sign in to comment.