Bug fix, tuning of peer message rate counting

This commit is contained in:
Ignotus Peverell 2018-11-07 22:15:12 +00:00
parent ab30f714fc
commit f7d0fe1840
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
3 changed files with 19 additions and 9 deletions

View file

@ -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;

View file

@ -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 {

View file

@ -22,7 +22,6 @@ use std::time::{Duration, SystemTime};
/// rates are worst-case estimates.
pub struct RateCounter {
last_min_bytes: Vec<u64>,
last_min_count: u64,
last_min_times: Vec<u64>,
}
@ -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
}
}