diff --git a/zebra-network/src/constants.rs b/zebra-network/src/constants.rs index f89c8fe2c..11aa40ae6 100644 --- a/zebra-network/src/constants.rs +++ b/zebra-network/src/constants.rs @@ -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🦓"; diff --git a/zebra-network/src/timestamp_collector.rs b/zebra-network/src/timestamp_collector.rs index 30efd5603..ad973a10d 100644 --- a/zebra-network/src/timestamp_collector.rs +++ b/zebra-network/src/timestamp_collector.rs @@ -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); @@ -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();