Skip to content

Commit 25bc30f

Browse files
feat(ping): don't close connections upon failures
Previously, the `libp2p-ping` module came with a policy to close a connection after X failed pings. This is only one of many possible policies on how users would want to do connection management. We remove this policy without a replacement. If users wish to restore this functionality, they can easily implement such policy themselves: The default value of `max_failures` was 1. To restore the previous functionality users can simply close the connection upon the first received ping error. In this same patch, we also simplify the API of `ping::Event` by removing the layer of `ping::Success` and instead reporting the RTT to the peer directly. Related: #3591. Pull-Request: #3947.
1 parent a5cd0d0 commit 25bc30f

File tree

12 files changed

+144
-196
lines changed

12 files changed

+144
-196
lines changed

examples/ipfs-private/src/main.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,35 +249,33 @@ async fn main() -> Result<(), Box<dyn Error>> {
249249
match event {
250250
ping::Event {
251251
peer,
252-
result: Result::Ok(ping::Success::Ping { rtt }),
252+
result: Result::Ok(rtt),
253+
..
253254
} => {
254255
println!(
255256
"ping: rtt to {} is {} ms",
256257
peer.to_base58(),
257258
rtt.as_millis()
258259
);
259260
}
260-
ping::Event {
261-
peer,
262-
result: Result::Ok(ping::Success::Pong),
263-
} => {
264-
println!("ping: pong from {}", peer.to_base58());
265-
}
266261
ping::Event {
267262
peer,
268263
result: Result::Err(ping::Failure::Timeout),
264+
..
269265
} => {
270266
println!("ping: timeout to {}", peer.to_base58());
271267
}
272268
ping::Event {
273269
peer,
274270
result: Result::Err(ping::Failure::Unsupported),
271+
..
275272
} => {
276273
println!("ping: {} does not support ping protocol", peer.to_base58());
277274
}
278275
ping::Event {
279276
peer,
280277
result: Result::Err(ping::Failure::Other { error }),
278+
..
281279
} => {
282280
println!("ping: ping::Failure with {}: {error}", peer.to_base58());
283281
}

examples/rendezvous/src/bin/rzv-discover.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ async fn main() {
105105
}
106106
SwarmEvent::Behaviour(MyBehaviourEvent::Ping(ping::Event {
107107
peer,
108-
result: Ok(ping::Success::Ping { rtt }),
108+
result: Ok(rtt),
109+
..
109110
})) if peer != rendezvous_point => {
110111
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
111112
}

examples/rendezvous/src/bin/rzv-identify.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ async fn main() {
106106
}
107107
SwarmEvent::Behaviour(MyBehaviourEvent::Ping(ping::Event {
108108
peer,
109-
result: Ok(ping::Success::Ping { rtt }),
109+
result: Ok(rtt),
110+
..
110111
})) if peer != rendezvous_point => {
111112
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
112113
}

examples/rendezvous/src/bin/rzv-register.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ async fn main() {
104104
}
105105
SwarmEvent::Behaviour(MyBehaviourEvent::Ping(ping::Event {
106106
peer,
107-
result: Ok(ping::Success::Ping { rtt }),
107+
result: Ok(rtt),
108+
..
108109
})) if peer != rendezvous_point => {
109110
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
110111
}

interop-tests/src/bin/ping.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ async fn main() -> Result<()> {
137137

138138
let rtt = loop {
139139
if let Some(SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
140-
peer: _,
141-
result: Ok(ping::Success::Ping { rtt }),
140+
result: Ok(rtt),
141+
..
142142
}))) = swarm.next().await
143143
{
144144
log::info!("Ping successful: {rtt:?}");

misc/metrics/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626
Note that you can use the `_count` metric of the `Histogram` as a replacement for the `Counter`.
2727
See [PR 3927].
2828

29+
- Remove the `pong_received` counter because it is no longer exposed by `libp2p-ping`.
30+
See [PR 3947].
31+
2932
[PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
3033
[PR 3927]: https://github.com/libp2p/rust-libp2p/pull/3927
3134
[PR 3325]: https://github.com/libp2p/rust-libp2p/pull/3325
35+
[PR 3947]: https://github.com/libp2p/rust-libp2p/pull/3947
3236

3337
## 0.12.0
3438

misc/metrics/src/ping.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ enum Failure {
5555
pub(crate) struct Metrics {
5656
rtt: Histogram,
5757
failure: Family<FailureLabels, Counter>,
58-
pong_received: Counter,
5958
}
6059

6160
impl Metrics {
@@ -77,28 +76,14 @@ impl Metrics {
7776
failure.clone(),
7877
);
7978

80-
let pong_received = Counter::default();
81-
sub_registry.register(
82-
"pong_received",
83-
"Number of 'pong's received",
84-
pong_received.clone(),
85-
);
86-
87-
Self {
88-
rtt,
89-
failure,
90-
pong_received,
91-
}
79+
Self { rtt, failure }
9280
}
9381
}
9482

9583
impl super::Recorder<libp2p_ping::Event> for Metrics {
9684
fn record(&self, event: &libp2p_ping::Event) {
9785
match &event.result {
98-
Ok(libp2p_ping::Success::Pong) => {
99-
self.pong_received.inc();
100-
}
101-
Ok(libp2p_ping::Success::Ping { rtt }) => {
86+
Ok(rtt) => {
10287
self.rtt.observe(rtt.as_secs_f64());
10388
}
10489
Err(failure) => {

protocols/ping/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
- Raise MSRV to 1.65.
44
See [PR 3715].
5+
56
- Remove deprecated items. See [PR 3702].
67

8+
- Don't close connections on ping failures.
9+
To restore the previous behaviour, users should call `Swarm::close_connection` upon receiving a `ping::Event` with a `ping::Failure`.
10+
This also removes the `max_failures` config option.
11+
See [PR 3947].
12+
713
[PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
814
[PR 3702]: https://github.com/libp2p/rust-libp2p/pull/3702
15+
[PR 3947]: https://github.com/libp2p/rust-libp2p/pull/3947
916

1017
## 0.42.0
1118

0 commit comments

Comments
 (0)