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
This commit is contained in:
Ignotus Peverell 2018-10-03 09:16:32 -07:00 committed by GitHub
parent 8e66aae592
commit f2b4c6dc07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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