Skip recursive discovery query if no useful ENRs (#6207)

* Skip recursive discovery query if no useful ENRs
This commit is contained in:
Lion - dapplion
2024-08-06 04:32:56 +02:00
committed by GitHub
parent acd3151184
commit f7f0bfc9f2
2 changed files with 20 additions and 2 deletions

View File

@@ -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(

View File

@@ -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.
@@ -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.