refactor: remove thread p2p-moniter, move the Ping to the connect_and_monitor (#1670)

This commit is contained in:
Gary Yu 2018-10-08 01:24:06 +08:00 committed by Ignotus Peverell
parent acf61db463
commit 1a6101f870
2 changed files with 10 additions and 15 deletions

View file

@ -90,21 +90,6 @@ impl Server {
/// Starts a new TCP server and listen to incoming connections. This is a
/// blocking call until the TCP server stops.
pub fn listen(&self) -> Result<(), Error> {
// start peer monitoring thread
let peers_inner = self.peers.clone();
let stop = self.stop.clone();
let _ = thread::Builder::new()
.name("p2p-monitor".to_string())
.spawn(move || loop {
let total_diff = peers_inner.total_difficulty();
let total_height = peers_inner.total_height();
peers_inner.check_all(total_diff, total_height);
thread::sleep(Duration::from_secs(10));
if stop.load(Ordering::Relaxed) {
break;
}
});
// start TCP listener and handle incoming connections
let addr = SocketAddr::new(self.config.host, self.config.port);
let listener = TcpListener::bind(addr)?;

View file

@ -25,6 +25,7 @@ use std::sync::{mpsc, Arc};
use std::{cmp, io, str, thread, time};
use p2p;
use p2p::ChainAdapter;
use pool::DandelionConfig;
use util::LOGGER;
@ -61,6 +62,7 @@ pub fn connect_and_monitor(
);
let mut prev = MIN_DATE.and_hms(0, 0, 0);
let mut prev_ping = Utc::now();
let mut start_attempt = 0;
while !stop.load(Ordering::Relaxed) {
@ -85,6 +87,14 @@ pub fn connect_and_monitor(
start_attempt = cmp::min(6, start_attempt + 1);
}
// Ping connected peers on every 10s to monitor peers.
if Utc::now() - prev_ping > Duration::seconds(10) {
let total_diff = peers.total_difficulty();
let total_height = peers.total_height();
peers.check_all(total_diff, total_height);
prev_ping = Utc::now();
}
thread::sleep(time::Duration::from_secs(1));
}
});