mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 11:31:08 +03:00
refactor: remove thread p2p-moniter, move the Ping to the connect_and_monitor (#1670)
This commit is contained in:
parent
acf61db463
commit
1a6101f870
2 changed files with 10 additions and 15 deletions
|
@ -90,21 +90,6 @@ impl Server {
|
||||||
/// Starts a new TCP server and listen to incoming connections. This is a
|
/// Starts a new TCP server and listen to incoming connections. This is a
|
||||||
/// blocking call until the TCP server stops.
|
/// blocking call until the TCP server stops.
|
||||||
pub fn listen(&self) -> Result<(), Error> {
|
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
|
// start TCP listener and handle incoming connections
|
||||||
let addr = SocketAddr::new(self.config.host, self.config.port);
|
let addr = SocketAddr::new(self.config.host, self.config.port);
|
||||||
let listener = TcpListener::bind(addr)?;
|
let listener = TcpListener::bind(addr)?;
|
||||||
|
|
|
@ -25,6 +25,7 @@ use std::sync::{mpsc, Arc};
|
||||||
use std::{cmp, io, str, thread, time};
|
use std::{cmp, io, str, thread, time};
|
||||||
|
|
||||||
use p2p;
|
use p2p;
|
||||||
|
use p2p::ChainAdapter;
|
||||||
use pool::DandelionConfig;
|
use pool::DandelionConfig;
|
||||||
use util::LOGGER;
|
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 = MIN_DATE.and_hms(0, 0, 0);
|
||||||
|
let mut prev_ping = Utc::now();
|
||||||
let mut start_attempt = 0;
|
let mut start_attempt = 0;
|
||||||
|
|
||||||
while !stop.load(Ordering::Relaxed) {
|
while !stop.load(Ordering::Relaxed) {
|
||||||
|
@ -85,6 +87,14 @@ pub fn connect_and_monitor(
|
||||||
start_attempt = cmp::min(6, start_attempt + 1);
|
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));
|
thread::sleep(time::Duration::from_secs(1));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue