From f7d0fe18408a648e66318a14d24b6cf9d13021fd Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Wed, 7 Nov 2018 22:15:12 +0000 Subject: [PATCH] Bug fix, tuning of peer message rate counting --- p2p/src/peer.rs | 12 +++++++++++- p2p/src/peers.rs | 6 +++++- util/src/rate_counter.rs | 10 +++------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/p2p/src/peer.rs b/p2p/src/peer.rs index 4d06a087f..e9d3f0e44 100644 --- a/p2p/src/peer.rs +++ b/p2p/src/peer.rs @@ -30,7 +30,7 @@ use types::{ }; const MAX_TRACK_SIZE: usize = 30; -const MAX_PEER_MSG_PER_MIN: u64 = 300; +const MAX_PEER_MSG_PER_MIN: u64 = 500; #[derive(Debug, Clone, Copy, PartialEq, Eq)] /// Remind: don't mix up this 'State' with that 'State' in p2p/src/store.rs, @@ -183,6 +183,16 @@ impl Peer { None } + pub fn last_min_message_counts(&self) -> Option<(u64, u64)> { + if let Some(ref tracker) = self.connection { + let conn = tracker.lock(); + let received_bytes = conn.received_bytes.read(); + let sent_bytes = conn.sent_bytes.read(); + return Some((sent_bytes.count_per_min(), received_bytes.count_per_min())); + } + None + } + /// Set this peer status to banned pub fn set_banned(&self) { *self.state.write() = State::Banned; diff --git a/p2p/src/peers.rs b/p2p/src/peers.rs index c90ad7660..73027a79b 100644 --- a/p2p/src/peers.rs +++ b/p2p/src/peers.rs @@ -390,7 +390,11 @@ impl Peers { debug!("clean_peers {:?}, not connected", peer.info.addr); rm.push(peer.info.addr.clone()); } else if peer.is_abusive() { - debug!("clean_peers {:?}, abusive", peer.info.addr); + let counts = peer.last_min_message_counts().unwrap(); + debug!( + "clean_peers {:?}, abusive ({} sent, {} recv)", + peer.info.addr, counts.0, counts.1, + ); let _ = self.update_state(peer.info.addr, State::Banned); rm.push(peer.info.addr.clone()); } else { diff --git a/util/src/rate_counter.rs b/util/src/rate_counter.rs index f4e929130..3d115dce7 100644 --- a/util/src/rate_counter.rs +++ b/util/src/rate_counter.rs @@ -22,7 +22,6 @@ use std::time::{Duration, SystemTime}; /// rates are worst-case estimates. pub struct RateCounter { last_min_bytes: Vec, - last_min_count: u64, last_min_times: Vec, } @@ -31,7 +30,6 @@ impl RateCounter { pub fn new() -> RateCounter { RateCounter { last_min_bytes: vec![], - last_min_count: 0, last_min_times: vec![], } } @@ -41,11 +39,9 @@ impl RateCounter { let now_millis = millis_since_epoch(); self.last_min_times.push(now_millis); self.last_min_bytes.push(bytes); - self.last_min_count += 1; while self.last_min_times.len() > 0 && self.last_min_times[0] > now_millis + 60000 { - self.last_min_times.pop(); - self.last_min_bytes.pop(); - self.last_min_count -= 1; + self.last_min_times.remove(0); + self.last_min_bytes.remove(0); } } @@ -56,7 +52,7 @@ impl RateCounter { /// Count of increases in the last minute pub fn count_per_min(&self) -> u64 { - self.last_min_count + self.last_min_bytes.len() as u64 } }