Skip to content

Commit

Permalink
adding reset_timers fn
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumemichel committed Jun 18, 2024
1 parent f1dfb2b commit b548fc7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,6 @@ where
/// > This parameter is used to call [`Behaviour::bootstrap`] periodically and automatically
/// > to ensure a healthy routing table.
pub fn bootstrap(&mut self) -> Result<QueryId, NoKnownPeers> {
self.bootstrap_status.on_started();
let local_key = *self.kbuckets.local_key();
let info = QueryInfo::Bootstrap {
peer: *local_key.preimage(),
Expand All @@ -940,9 +939,10 @@ where
};
let peers = self.kbuckets.closest_keys(&local_key).collect::<Vec<_>>();
if peers.is_empty() {
self.bootstrap_status.on_finish();
self.bootstrap_status.reset_timers();
Err(NoKnownPeers())
} else {
self.bootstrap_status.on_started();
let inner = QueryInner::new(info);
Ok(self.queries.add_iter_closest(local_key, peers, inner))
}
Expand Down
18 changes: 11 additions & 7 deletions protocols/kad/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,23 @@ impl Status {
}
}

pub(crate) fn reset_timers(&mut self) {
// Canceling the `throttle_timer` if any and resetting the `delay` if any.
self.throttle_timer = None;

if let Some((interval, delay)) = self.interval_and_delay.as_mut() {
delay.reset(*interval);
}
}

pub(crate) fn on_started(&mut self) {
// No periodic or automatic bootstrap will be triggered as long as
// `self.current_bootstrap_requests > 0` but the user could still manually
// trigger a bootstrap.
self.current_bootstrap_requests += 1;

// Canceling the `throttle_timer` if any since a bootstrap request is being triggered right now.
self.throttle_timer = None;

// Resetting the `delay` if any since a bootstrap request is being triggered right now.
if let Some((interval, delay)) = self.interval_and_delay.as_mut() {
delay.reset(*interval);
}
// Resetting the Status timers since a bootstrap request is being triggered right now.
self.reset_timers();
}

pub(crate) fn on_finish(&mut self) {
Expand Down

0 comments on commit b548fc7

Please sign in to comment.