Optimized network stuff.

This commit is contained in:
Revertron
2021-04-04 13:13:32 +02:00
parent 09a5750fc1
commit fd294580c7
3 changed files with 58 additions and 20 deletions
+17 -13
View File
@@ -63,6 +63,7 @@ impl Network {
// Starting peer connections to bootstrap nodes
peers.connect_peers(peers_addrs, &poll.registry(), &mut unique_token, yggdrasil_only);
let mut peers_timer = Instant::now();
loop {
// Poll Mio for events, blocking until we get an event.
poll.poll(&mut events, POLL_TIMEOUT).expect("Error polling sockets");
@@ -131,19 +132,22 @@ impl Network {
}
events.clear();
// Send pings to idle peers
let (height, hash) = {
let mut context = context.lock().unwrap();
let height = context.chain.height();
let nodes = peers.get_peers_active_count();
if nodes > 0 {
context.bus.post(crate::event::Event::NetworkStatus { nodes, blocks: height });
}
(height, context.chain.last_hash())
};
mine_locker_block(Arc::clone(&context));
peers.send_pings(poll.registry(), height, hash);
peers.connect_new_peers(poll.registry(), &mut unique_token, yggdrasil_only);
if peers_timer.elapsed().as_millis() > 100 {
// Send pings to idle peers
let (height, hash) = {
let mut context = context.lock().unwrap();
let height = context.chain.height();
let nodes = peers.get_peers_active_count();
if nodes > 0 {
context.bus.post(crate::event::Event::NetworkStatus { nodes, blocks: height });
}
(height, context.chain.last_hash())
};
mine_locker_block(Arc::clone(&context));
peers.send_pings(poll.registry(), height, hash);
peers.connect_new_peers(poll.registry(), &mut unique_token, yggdrasil_only);
peers_timer = Instant::now();
}
}
info!("Network loop finished");
});