Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kad): correctly handle NoKnownPeers error when bootstrap #5349

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
See [PR 5317](https://github.com/libp2p/rust-libp2p/pull/5317).
- Use `web-time` instead of `instant`.
See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347).
- Correctly handle the `NoKnownPeers` error on automatic bootstrap.
See [PR 5349](https://github.com/libp2p/rust-libp2p/pull/5349).

## 0.45.3

Expand Down
1 change: 1 addition & 0 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ where
};
let peers = self.kbuckets.closest_keys(&local_key).collect::<Vec<_>>();
if peers.is_empty() {
self.bootstrap_status.reset_timers();
Err(NoKnownPeers())
} else {
self.bootstrap_status.on_started();
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
Loading