Add peer count data to TimestampData::update trace (#66)

* Add peer count data to TimestampData::update trace

* Update docstring typo
This commit is contained in:
Henry de Valence 2019-10-11 12:41:37 -07:00 committed by GitHub
parent ae1a164ff8
commit b45efbdaf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -1,8 +1,14 @@
//! Definitions of constants.
use std::time::Duration;
// XXX should these constants be split into protocol also?
use crate::protocol::types::*;
/// We expect to receive a message from a live peer at least once in this time duration.
/// XXX this needs to be synchronized with the ping transmission times.
pub const LIVE_PEER_DURATION: Duration = Duration::from_secs(12);
/// The User-Agent string provided by the node.
pub const USER_AGENT: &'static str = "🦓Zebra v2.0.0-alpha.0🦓";

View File

@ -10,6 +10,8 @@ use chrono::{DateTime, Utc};
use futures::channel::mpsc;
use tokio::prelude::*;
use crate::constants;
/// A type alias for a timestamp event sent to a `TimestampCollector`.
pub(crate) type PeerLastSeen = (SocketAddr, DateTime<Utc>);
@ -38,9 +40,23 @@ struct TimestampData {
impl TimestampData {
fn update(&mut self, event: PeerLastSeen) {
use chrono::Duration as CD;
use std::collections::hash_map::Entry;
let (addr, timestamp) = event;
trace!(?addr, ?timestamp);
trace!(
?addr,
?timestamp,
data.total = self.by_time.len(),
// This would be cleaner if it used "variables" but keeping
// it inside the trace! invocation prevents running the range
// query unless the output will actually be used.
data.recent = self
.by_time
.range(
(Utc::now() - CD::from_std(constants::LIVE_PEER_DURATION).unwrap())..Utc::now()
)
.count()
);
match self.by_addr.entry(addr) {
Entry::Occupied(mut entry) => {
let last_timestamp = entry.get();