From 3de34290e6bddb65220802089662cd41c949e579 Mon Sep 17 00:00:00 2001 From: Deirdre Connolly Date: Mon, 21 Oct 2019 14:59:47 -0400 Subject: [PATCH] Construct LIVE_PEER_DURATION from other timeout and interval constants Use constants::HEARTBEAT_INTERVAL in our ping generator, add a test to check that LIVE_PEER_DURATION is consistent with the other constants. --- zebra-network/src/constants.rs | 20 +++++++++++++++++++- zebra-network/src/peer/connector.rs | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/zebra-network/src/constants.rs b/zebra-network/src/constants.rs index 5906532c..f90e02f6 100644 --- a/zebra-network/src/constants.rs +++ b/zebra-network/src/constants.rs @@ -10,7 +10,11 @@ pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(10); /// 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); +pub const LIVE_PEER_DURATION: Duration = Duration::from_secs(60 + 10 + 10 + 10); + +/// Regular interval for sending keepalive `Ping` messages to each +/// connected peer. +pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(60); /// The User-Agent string provided by the node. pub const USER_AGENT: &'static str = "🦓Zebra v2.0.0-alpha.0🦓"; @@ -29,3 +33,17 @@ pub mod magics { /// The testnet. pub const TESTNET: Magic = Magic([0xfa, 0x1a, 0xf9, 0xbf]); } + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn ensure_live_peer_duration_value_matches_others() { + let constructed_live_peer_duration = + HEARTBEAT_INTERVAL + REQUEST_TIMEOUT + REQUEST_TIMEOUT + REQUEST_TIMEOUT; + + assert_eq!(LIVE_PEER_DURATION, constructed_live_peer_duration); + } +} diff --git a/zebra-network/src/peer/connector.rs b/zebra-network/src/peer/connector.rs index 42d7583f..870ccca3 100644 --- a/zebra-network/src/peer/connector.rs +++ b/zebra-network/src/peer/connector.rs @@ -217,7 +217,7 @@ where let mut server_tx = server_tx; - let mut interval_stream = Interval::new_interval(Duration::from_secs(60)); + let mut interval_stream = Interval::new_interval(constants::HEARTBEAT_INTERVAL); loop { interval_stream.next().await;