mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
Bug fix, tuning of peer message rate counting
This commit is contained in:
parent
ab30f714fc
commit
f7d0fe1840
3 changed files with 19 additions and 9 deletions
|
@ -30,7 +30,7 @@ use types::{
|
||||||
};
|
};
|
||||||
|
|
||||||
const MAX_TRACK_SIZE: usize = 30;
|
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)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
/// Remind: don't mix up this 'State' with that 'State' in p2p/src/store.rs,
|
/// Remind: don't mix up this 'State' with that 'State' in p2p/src/store.rs,
|
||||||
|
@ -183,6 +183,16 @@ impl Peer {
|
||||||
None
|
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
|
/// Set this peer status to banned
|
||||||
pub fn set_banned(&self) {
|
pub fn set_banned(&self) {
|
||||||
*self.state.write() = State::Banned;
|
*self.state.write() = State::Banned;
|
||||||
|
|
|
@ -390,7 +390,11 @@ impl Peers {
|
||||||
debug!("clean_peers {:?}, not connected", peer.info.addr);
|
debug!("clean_peers {:?}, not connected", peer.info.addr);
|
||||||
rm.push(peer.info.addr.clone());
|
rm.push(peer.info.addr.clone());
|
||||||
} else if peer.is_abusive() {
|
} 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);
|
let _ = self.update_state(peer.info.addr, State::Banned);
|
||||||
rm.push(peer.info.addr.clone());
|
rm.push(peer.info.addr.clone());
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -22,7 +22,6 @@ use std::time::{Duration, SystemTime};
|
||||||
/// rates are worst-case estimates.
|
/// rates are worst-case estimates.
|
||||||
pub struct RateCounter {
|
pub struct RateCounter {
|
||||||
last_min_bytes: Vec<u64>,
|
last_min_bytes: Vec<u64>,
|
||||||
last_min_count: u64,
|
|
||||||
last_min_times: Vec<u64>,
|
last_min_times: Vec<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +30,6 @@ impl RateCounter {
|
||||||
pub fn new() -> RateCounter {
|
pub fn new() -> RateCounter {
|
||||||
RateCounter {
|
RateCounter {
|
||||||
last_min_bytes: vec![],
|
last_min_bytes: vec![],
|
||||||
last_min_count: 0,
|
|
||||||
last_min_times: vec![],
|
last_min_times: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,11 +39,9 @@ impl RateCounter {
|
||||||
let now_millis = millis_since_epoch();
|
let now_millis = millis_since_epoch();
|
||||||
self.last_min_times.push(now_millis);
|
self.last_min_times.push(now_millis);
|
||||||
self.last_min_bytes.push(bytes);
|
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 {
|
while self.last_min_times.len() > 0 && self.last_min_times[0] > now_millis + 60000 {
|
||||||
self.last_min_times.pop();
|
self.last_min_times.remove(0);
|
||||||
self.last_min_bytes.pop();
|
self.last_min_bytes.remove(0);
|
||||||
self.last_min_count -= 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +52,7 @@ impl RateCounter {
|
||||||
|
|
||||||
/// Count of increases in the last minute
|
/// Count of increases in the last minute
|
||||||
pub fn count_per_min(&self) -> u64 {
|
pub fn count_per_min(&self) -> u64 {
|
||||||
self.last_min_count
|
self.last_min_bytes.len() as u64
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue