From a37f7bbcab91d7577d839204e07b7eb9924a77ec Mon Sep 17 00:00:00 2001 From: Gary Yu Date: Sun, 7 Oct 2018 08:00:53 +0800 Subject: [PATCH] improvement on peer lock for ping/pong (#1668) * peer lock optimization for ping/pong * is_banned peer lock use read lock instead of write --- p2p/src/peers.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/p2p/src/peers.rs b/p2p/src/peers.rs index 4fd8fc36e..a2cac8475 100644 --- a/p2p/src/peers.rs +++ b/p2p/src/peers.rs @@ -656,8 +656,13 @@ impl NetAdapter for Peers { ); } - if diff.to_num() > 0 { - if let Some(peer) = self.get_connected_peer(&addr) { + if let Some(peer) = self.get_connected_peer(&addr) { + let (prev_diff, prev_height) = { + let peer = peer.read().unwrap(); + (peer.info.total_difficulty, peer.info.height) + }; + + if diff != prev_diff || height != prev_height { let mut peer = peer.write().unwrap(); peer.info.total_difficulty = diff; peer.info.height = height; @@ -667,7 +672,7 @@ impl NetAdapter for Peers { fn is_banned(&self, addr: SocketAddr) -> bool { if let Some(peer) = self.get_connected_peer(&addr) { - let peer = peer.write().unwrap(); + let peer = peer.read().unwrap(); peer.is_banned() } else { false