Skip to content

Commit

Permalink
fix(ping): honor ping interval in case of errors
Browse files Browse the repository at this point in the history
This PR adds a delay to ping connection handler after exceeding the failure rate to prevent opening a outbound stream right after an error.

Resolves #4410.

Pull-Request: #4423.
  • Loading branch information
dariusc93 authored Sep 9, 2023
1 parent 19be774 commit cd32435
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
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
8 changes: 8 additions & 0 deletions protocols/ping/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.43.1 - unreleased

- Honor ping interval in case of errors.
Previously, we would immediately open another ping stream if the current one failed.
See [PR 4423].

[PR 4423]: https://github.com/libp2p/rust-libp2p/pull/4423

## 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
18 changes: 11 additions & 7 deletions protocols/ping/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ impl ConnectionHandler for Handler {
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Ok(rtt)));
}
Poll::Ready(Err(e)) => {
self.interval.reset(self.config.interval);
self.pending_errors.push_front(e);
}
},
Expand All @@ -321,13 +322,16 @@ impl ConnectionHandler for Handler {
self.outbound = Some(OutboundState::OpenStream);
break;
}
None => {
self.outbound = Some(OutboundState::OpenStream);
let protocol = SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ());
return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol,
});
}
None => match self.interval.poll_unpin(cx) {
Poll::Pending => break,
Poll::Ready(()) => {
self.outbound = Some(OutboundState::OpenStream);
let protocol = SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ());
return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol,
});
}
},
}
}

Expand Down
3 changes: 1 addition & 2 deletions protocols/ping/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ pub const PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/ipfs/ping/1.0.0"
///
/// At most a single inbound and outbound substream is kept open at
/// any time. In case of a ping timeout or another error on a substream, the
/// substream is dropped. If a configurable number of consecutive
/// outbound pings fail, the connection is closed.
/// substream is dropped.
///
/// Successful pings report the round-trip time.
///
Expand Down

0 comments on commit cd32435

Please sign in to comment.