From f2b4c6dc079b2922d00c461085d09b87b8b9a654 Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Wed, 3 Oct 2018 09:16:32 -0700 Subject: [PATCH] Resurrect a random peer on every monitor (#1646) Picks a random defunct peer and marks it healthy again anytime we're querying peers to find more. Over enough time, any peer will see another as defunct, leading to peer list atrophy. This allows us random retries. In addition, we need to cleanup peers after a while, we quickly accumulate hundreds of dead peers over time. This should be tracked by a different issue however. Related: #1632 --- servers/src/grin/seed.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/servers/src/grin/seed.rs b/servers/src/grin/seed.rs index 7c4da3c74..5947d4dd3 100644 --- a/servers/src/grin/seed.rs +++ b/servers/src/grin/seed.rs @@ -18,6 +18,7 @@ use chrono::prelude::Utc; use chrono::{Duration, MIN_DATE}; +use rand::{thread_rng, Rng}; use std::net::{SocketAddr, ToSocketAddrs}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{mpsc, Arc}; @@ -101,7 +102,7 @@ fn monitor_peers( let total_count = peers.all_peers().len(); let mut healthy_count = 0; let mut banned_count = 0; - let mut defunct_count = 0; + let mut defuncts = vec![]; for x in peers.all_peers() { match x.flags { p2p::State::Banned => { @@ -118,7 +119,7 @@ fn monitor_peers( } } p2p::State::Healthy => healthy_count += 1, - p2p::State::Defunct => defunct_count += 1, + p2p::State::Defunct => defuncts.push(x), } } @@ -133,7 +134,7 @@ fn monitor_peers( total_count, healthy_count, banned_count, - defunct_count, + defuncts.len(), ); // maintenance step first, clean up p2p server peers @@ -176,6 +177,13 @@ fn monitor_peers( None => debug!(LOGGER, "monitor_peers: no preferred peers"), } + // take a random defunct peer and mark it healthy: over a long period any + // peer will see another as defunct eventually, gives us a chance to retry + if defuncts.len() > 0 { + thread_rng().shuffle(&mut defuncts); + let _ = peers.update_state(defuncts[0].addr, p2p::State::Healthy); + } + // find some peers from our db // and queue them up for a connection attempt let new_peers = peers.find_peers(