Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
fix(p2p): rework ticks of bootstrap query interval
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Paitrault <[email protected]>
  • Loading branch information
Freyskeyd committed Mar 21, 2024
1 parent b2a1af0 commit 369cfb8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 11 additions & 3 deletions crates/topos-p2p/src/behaviour/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(())
Expand Down
6 changes: 4 additions & 2 deletions crates/topos-p2p/src/runtime/handle_event/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ impl EventHandler<Box<Event>> 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 {
Expand Down Expand Up @@ -115,7 +116,8 @@ impl EventHandler<Box<Event>> 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 {
Expand Down

0 comments on commit 369cfb8

Please sign in to comment.