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): don't close connections upon failures #3947

Merged
merged 26 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6481aa8
Handle unreachable case explicitly
thomaseizinger May 15, 2023
a1d9a53
ReadyUpgrade cannot time out
thomaseizinger May 15, 2023
dcb2256
Directly map error to `Failure::Other`
thomaseizinger May 15, 2023
61f4b1c
fixup! ReadyUpgrade cannot time out
thomaseizinger May 15, 2023
6840638
Embed timeout in `send_ping`
thomaseizinger May 15, 2023
da502da
Exhaustively match
thomaseizinger May 15, 2023
3e9d527
fixup! Embed timeout in `send_ping`
thomaseizinger May 15, 2023
6ea4e75
Remove `Result` type alias
thomaseizinger May 15, 2023
ceb7121
Remove unnecessary path prefixes
thomaseizinger May 15, 2023
ceb9899
Move logging to handler
thomaseizinger May 15, 2023
c854561
Remove `Pong` event
thomaseizinger May 15, 2023
3240725
Don't close connection
thomaseizinger May 15, 2023
458b35a
Apply suggestions from code review
thomaseizinger May 15, 2023
f0b230c
Merge branch 'master' into feat/no-close-connection-ping-failures
thomaseizinger May 16, 2023
45605b0
Merge branch 'feat/no-close-connection-ping-failures' of github.com:l…
thomaseizinger May 16, 2023
b6540fe
Merge branch 'master' into feat/no-close-connection-ping-failures
thomaseizinger May 17, 2023
60f250e
Merge branch 'master' into feat/no-close-connection-ping-failures
thomaseizinger May 23, 2023
2a910fa
Add `Swarm::close_connection` API
thomaseizinger May 23, 2023
772c891
Expose connection ID on event
thomaseizinger May 23, 2023
ea7c01c
Update protocols/ping/CHANGELOG.md
thomaseizinger May 23, 2023
c1fe6a1
Document health check / connection management with ping
thomaseizinger May 23, 2023
6e12acf
Merge branch 'feat/no-close-connection-ping-failures' of github.com:l…
thomaseizinger May 23, 2023
ad6a882
Fix compile errors
thomaseizinger May 24, 2023
9c4a656
Tidy up docs
thomaseizinger May 24, 2023
5c79d30
Fix typo
thomaseizinger May 24, 2023
198d164
Merge branch 'master' into feat/no-close-connection-ping-failures
mergify[bot] May 24, 2023
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
Prev Previous commit
Next Next commit
Remove Pong event
This simplifies the API and reduces code. The hypothesis is that
nobody is really interested in `Pong` events.
  • Loading branch information
thomaseizinger committed May 15, 2023
commit c85456116c217e61e8a6d924ddd931433a76b038
8 changes: 1 addition & 7 deletions examples/ipfs-private/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
match event {
ping::Event {
peer,
result: Result::Ok(ping::Success::Ping { rtt }),
result: Result::Ok(rtt),
} => {
println!(
"ping: rtt to {} is {} ms",
peer.to_base58(),
rtt.as_millis()
);
}
ping::Event {
peer,
result: Result::Ok(ping::Success::Pong),
} => {
println!("ping: pong from {}", peer.to_base58());
}
ping::Event {
peer,
result: Result::Err(ping::Failure::Timeout),
Expand Down
2 changes: 1 addition & 1 deletion examples/rendezvous/src/bin/rzv-discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async fn main() {
}
SwarmEvent::Behaviour(MyBehaviourEvent::Ping(ping::Event {
peer,
result: Ok(ping::Success::Ping { rtt }),
result: Ok(rtt),
})) if peer != rendezvous_point => {
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rendezvous/src/bin/rzv-identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async fn main() {
}
SwarmEvent::Behaviour(MyBehaviourEvent::Ping(ping::Event {
peer,
result: Ok(ping::Success::Ping { rtt }),
result: Ok(rtt),
})) if peer != rendezvous_point => {
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rendezvous/src/bin/rzv-register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn main() {
}
SwarmEvent::Behaviour(MyBehaviourEvent::Ping(ping::Event {
peer,
result: Ok(ping::Success::Ping { rtt }),
result: Ok(rtt),
})) if peer != rendezvous_point => {
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
}
Expand Down
2 changes: 1 addition & 1 deletion interop-tests/src/bin/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async fn main() -> Result<()> {
let rtt = loop {
if let Some(SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
peer: _,
result: Ok(ping::Success::Ping { rtt }),
result: Ok(rtt),
}))) = swarm.next().await
{
log::info!("Ping successful: {rtt:?}");
Expand Down
2 changes: 2 additions & 0 deletions misc/metrics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
- Raise MSRV to 1.65.
See [PR 3715].

- TODO: Removed `pong_received`.

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

Expand Down
19 changes: 2 additions & 17 deletions misc/metrics/src/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ enum Failure {
pub(crate) struct Metrics {
rtt: Histogram,
failure: Family<FailureLabels, Counter>,
pong_received: Counter,
}

impl Metrics {
Expand All @@ -77,28 +76,14 @@ impl Metrics {
failure.clone(),
);

let pong_received = Counter::default();
sub_registry.register(
"pong_received",
"Number of 'pong's received",
pong_received.clone(),
);

Self {
rtt,
failure,
pong_received,
}
Self { rtt, failure }
}
}

impl super::Recorder<libp2p_ping::Event> for Metrics {
fn record(&self, event: &libp2p_ping::Event) {
match &event.result {
Ok(libp2p_ping::Success::Pong) => {
self.pong_received.inc();
}
Ok(libp2p_ping::Success::Ping { rtt }) => {
Ok(rtt) => {
self.rtt.observe(rtt.as_secs_f64());
}
Err(failure) => {
Expand Down
20 changes: 3 additions & 17 deletions protocols/ping/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,6 @@ impl Default for Config {
}
}

/// The successful result of processing an inbound or outbound ping.
#[derive(Debug)]
pub enum Success {
/// Received a ping and sent back a pong.
Pong,
/// Sent a ping and received back a pong.
///
/// Includes the round-trip time.
Ping { rtt: Duration },
}

/// An outbound ping failure.
#[derive(Debug)]
pub enum Failure {
Expand Down Expand Up @@ -243,7 +232,7 @@ impl Handler {

impl ConnectionHandler for Handler {
type FromBehaviour = Void;
type ToBehaviour = Result<Success, Failure>;
type ToBehaviour = Result<Duration, Failure>;
type Error = Failure;
type InboundProtocol = ReadyUpgrade<StreamProtocol>;
type OutboundProtocol = ReadyUpgrade<StreamProtocol>;
Expand All @@ -267,7 +256,7 @@ impl ConnectionHandler for Handler {
ConnectionHandlerEvent<
ReadyUpgrade<StreamProtocol>,
(),
Result<Success, Failure>,
Result<Duration, Failure>,
Self::Error,
>,
> {
Expand Down Expand Up @@ -295,7 +284,6 @@ impl ConnectionHandler for Handler {

// A ping from a remote peer has been answered, wait for the next.
self.inbound = Some(protocol::recv_ping(stream).boxed());
return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(Success::Pong)));
}
}
}
Expand Down Expand Up @@ -337,9 +325,7 @@ impl ConnectionHandler for Handler {
self.failures = 0;
self.interval.reset(self.config.interval);
self.outbound = Some(OutboundState::Idle(stream));
return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(Success::Ping {
rtt,
})));
return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(rtt)));
}
Poll::Ready(Err(e)) => {
self.pending_errors.push_front(e);
Expand Down
5 changes: 3 additions & 2 deletions protocols/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,20 @@ mod handler;
mod protocol;

use handler::Handler;
pub use handler::{Config, Failure, Success};
use libp2p_core::{Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::{
behaviour::FromSwarm, ConnectionDenied, ConnectionId, NetworkBehaviour, PollParameters,
THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
};
use std::time::Duration;
use std::{
collections::VecDeque,
task::{Context, Poll},
};

pub use self::protocol::PROTOCOL_NAME;
pub use handler::{Config, Failure};

/// A [`NetworkBehaviour`] that responds to inbound pings and
/// periodically sends outbound pings on every established connection.
Expand All @@ -74,7 +75,7 @@ pub struct Event {
/// The peer ID of the remote.
pub peer: PeerId,
/// The result of an inbound or outbound ping.
pub result: Result<Success, Failure>,
pub result: Result<Duration, Failure>,
}

impl Behaviour {
Expand Down
9 changes: 3 additions & 6 deletions protocols/ping/tests/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ fn ping_pong() {
}

fn assert_ping_rtt_less_than_50ms(e: ping::Event) {
let success = e.result.expect("a ping success");
let rtt = e.result.expect("a ping success");

if let ping::Success::Ping { rtt } = success {
assert!(rtt < Duration::from_millis(50))
}
assert!(rtt < Duration::from_millis(50))
}

/// Tests that the connection is closed upon a configurable
Expand Down Expand Up @@ -102,8 +100,7 @@ async fn count_ping_failures_until_connection_closed(mut swarm: Swarm<Behaviour>
loop {
match swarm.next_swarm_event().await {
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
result: Ok(ping::Success::Ping { .. }),
..
result: Ok(_rtt), ..
})) => {
failure_count = 0; // there may be an occasional success
}
Expand Down