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.
This commit is contained in:
Thomas Eizinger
2023-05-24 14:33:18 +02:00
committed by GitHub
parent a5cd0d0e03
commit 25bc30f07e
12 changed files with 144 additions and 196 deletions

View File

@ -699,6 +699,24 @@ where
}
}
/// Attempt to gracefully close a connection.
///
/// Closing a connection is asynchronous but this function will return immediately.
/// A [`SwarmEvent::ConnectionClosed`] event will be emitted once the connection is actually closed.
///
/// # Returns
///
/// - `true` if the connection was established and is now being closed.
/// - `false` if the connection was not found or is no longer established.
pub fn close_connection(&mut self, connection_id: ConnectionId) -> bool {
if let Some(established) = self.pool.get_established(connection_id) {
established.start_close();
return true;
}
false
}
/// Checks whether there is an established connection to a peer.
pub fn is_connected(&self, peer_id: &PeerId) -> bool {
self.pool.is_connected(*peer_id)