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(ping): honor ping interval in case of errors #4423

Merged
merged 11 commits into from
Sep 9, 2023
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ libp2p-mplex = { version = "0.40.0", path = "muxers/mplex" }
libp2p-muxer-test-harness = { path = "muxers/test-harness" }
libp2p-noise = { version = "0.43.1", path = "transports/noise" }
libp2p-perf = { version = "0.2.0", path = "protocols/perf" }
libp2p-ping = { version = "0.43.0", path = "protocols/ping" }
libp2p-ping = { version = "0.43.1", path = "protocols/ping" }
libp2p-plaintext = { version = "0.40.0", path = "transports/plaintext" }
libp2p-pnet = { version = "0.23.0", path = "transports/pnet" }
libp2p-quic = { version = "0.9.2", path = "transports/quic" }
Expand Down
6 changes: 6 additions & 0 deletions protocols/ping/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.43.1 - unreleased
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved
- Add delay after exceeding failure rate
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved
See [PR XXXX].
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved

[PR XXXX]: https://github.com/libp2p/rust-libp2p/pull/XXXX
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved

## 0.43.0

- Raise MSRV to 1.65.
Expand Down
2 changes: 1 addition & 1 deletion protocols/ping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-ping"
edition = "2021"
rust-version = { workspace = true }
description = "Ping protocol for libp2p"
version = "0.43.0"
version = "0.43.1"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
12 changes: 12 additions & 0 deletions protocols/ping/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ pub struct Handler {
state: State,
/// The peer we are connected to.
peer: PeerId,
/// delay after a failure is reported
failure_delay: Option<Delay>,
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -176,6 +178,7 @@ impl Handler {
outbound: None,
inbound: None,
state: State::Active,
failure_delay: None,
}
}

Expand Down Expand Up @@ -271,6 +274,12 @@ impl ConnectionHandler for Handler {
}

loop {
if let Some(delay) = self.failure_delay.as_mut() {
if delay.poll_unpin(cx).is_pending() {
return Poll::Pending;
}
}
dariusc93 marked this conversation as resolved.
Show resolved Hide resolved

// Check for outbound ping failures.
if let Some(error) = self.pending_errors.pop_back() {
log::debug!("Ping failure: {:?}", error);
Expand All @@ -283,10 +292,13 @@ impl ConnectionHandler for Handler {
// that use a single substream, since every successful ping
// resets `failures` to `0`.
if self.failures > 1 {
self.failure_delay = Some(Delay::new(Duration::from_secs(5)));
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Err(error)));
}
}

self.failure_delay = None;

// Continue outbound pings.
match self.outbound.take() {
Some(OutboundState::Ping(mut ping)) => match ping.poll_unpin(cx) {
Expand Down