From 369cfb8889ee34bfb15fec14994745927c0f8649 Mon Sep 17 00:00:00 2001 From: Simon Paitrault Date: Thu, 21 Mar 2024 16:27:21 +0100 Subject: [PATCH] fix(p2p): rework ticks of bootstrap query interval Signed-off-by: Simon Paitrault --- crates/topos-p2p/src/behaviour/discovery.rs | 14 +++++++++++--- .../src/runtime/handle_event/discovery.rs | 6 ++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/topos-p2p/src/behaviour/discovery.rs b/crates/topos-p2p/src/behaviour/discovery.rs index 67a18aee4..cd1962890 100644 --- a/crates/topos-p2p/src/behaviour/discovery.rs +++ b/crates/topos-p2p/src/behaviour/discovery.rs @@ -80,7 +80,10 @@ impl DiscoveryBehaviour { next_bootstrap_query: if known_peers.is_empty() { None } else { - Some(Box::pin(tokio::time::interval(config.bootstrap_interval))) + let mut interval = tokio::time::interval(config.bootstrap_interval); + interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + + Some(Box::pin(interval)) }, health_status: if known_peers.is_empty() { HealthStatus::Healthy @@ -105,9 +108,14 @@ impl DiscoveryBehaviour { } /// Change the interval of the next bootstrap queries - pub fn change_interval(&mut self, duration: Duration) -> Result<(), P2PError> { + pub async fn change_interval(&mut self, duration: Duration) -> Result<(), P2PError> { if let Some(interval) = self.next_bootstrap_query.as_mut() { - interval.set(tokio::time::interval(duration)); + let mut new_interval = tokio::time::interval(duration); + // Delay the next tick + new_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + // ignore first tick + _ = new_interval.tick().await; + interval.set(new_interval); } Ok(()) diff --git a/crates/topos-p2p/src/runtime/handle_event/discovery.rs b/crates/topos-p2p/src/runtime/handle_event/discovery.rs index c08195829..be1c67f67 100644 --- a/crates/topos-p2p/src/runtime/handle_event/discovery.rs +++ b/crates/topos-p2p/src/runtime/handle_event/discovery.rs @@ -53,7 +53,8 @@ impl EventHandler> for Runtime { behaviour.discovery.health_status = HealthStatus::Unhealthy; _ = behaviour .discovery - .change_interval(self.config.discovery.fast_bootstrap_interval); + .change_interval(self.config.discovery.fast_bootstrap_interval) + .await; } Event::OutboundQueryProgressed { @@ -115,7 +116,8 @@ impl EventHandler> for Runtime { behaviour.discovery.health_status = HealthStatus::Healthy; _ = behaviour .discovery - .change_interval(self.config.discovery.bootstrap_interval); + .change_interval(self.config.discovery.bootstrap_interval) + .await; } Event::OutboundQueryProgressed {