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

feat(ping): making the ignoring of the first error configurable #5005

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 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 @@ -91,7 +91,7 @@ libp2p-mplex = { version = "0.41.0", path = "muxers/mplex" }
libp2p-muxer-test-harness = { path = "muxers/test-harness" }
libp2p-noise = { version = "0.44.0", path = "transports/noise" }
libp2p-perf = { version = "0.3.0", path = "protocols/perf" }
libp2p-ping = { version = "0.44.0", path = "protocols/ping" }
libp2p-ping = { version = "0.44.1", path = "protocols/ping" }
libp2p-plaintext = { version = "0.41.0", path = "transports/plaintext" }
libp2p-pnet = { version = "0.24.0", path = "transports/pnet" }
libp2p-quic = { version = "0.10.2", path = "transports/quic" }
Expand Down
5 changes: 5 additions & 0 deletions protocols/ping/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.44.1

- Making the silence of the first ping error configurable.
[PR 5005](https://github.com/libp2p/rust-libp2p/pull/5005).

## 0.44.0


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.44.0"
version = "0.44.1"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
14 changes: 12 additions & 2 deletions protocols/ping/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub struct Config {
timeout: Duration,
/// The duration between outbound pings.
interval: Duration,
/// Whether to ignore the first ping error or not. It is necessary to ignore it to
/// be interoperable with other libp2p implementations (ex: JS).
silent_first_error: bool,
}

impl Config {
Expand All @@ -63,6 +66,7 @@ impl Config {
Self {
timeout: Duration::from_secs(20),
interval: Duration::from_secs(15),
silent_first_error: true,
}
}

Expand All @@ -77,6 +81,12 @@ impl Config {
self.interval = d;
self
}

/// Sets whether to ignore the first ping error or not.
pub fn with_silent_first_error(mut self, silent_first_error: bool) -> Self {
self.silent_first_error = silent_first_error;
self
}
}

impl Default for Config {
Expand Down Expand Up @@ -263,12 +273,12 @@ impl ConnectionHandler for Handler {

self.failures += 1;

// Note: For backward-compatibility the first failure is always "free"
// Note: For backward-compatibility the first failure can be "free"
// and silent. This allows peers who use a new substream
// for each ping to have successful ping exchanges with peers
// that use a single substream, since every successful ping
// resets `failures` to `0`.
if self.failures > 1 {
if !self.config.silent_first_error || self.failures > 1 {
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Err(error)));
}
}
Expand Down