2018-11-04 09:47:15 +01:00
|
|
|
// Copyright 2017-2018 Parity Technologies (UK) Ltd.
|
2017-11-22 10:58:06 +01:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
//! This module implements the `/ipfs/ping/1.0.0` protocol.
|
|
|
|
//!
|
Remove libp2p-ping keep-alive functionality. (#1067)
* Fix connection & handler shutdown when using `KeepAlive::Now`.
Delay::new(Instant::now()) is never immediately ready, resulting in
`KeepAlive::Now` to have no effect, since the delay is re-created on
every execution of `poll()` in the `NodeHandlerWrapper`. It can also
send the node handler into a busy-loop, since every newly
created Delay will trigger a task wakeup, which creates a new Delay
with Instant::now(), and so forth.
The use of `Delay::new(Instant::now())` for "immediate" connection shutdown
is therefore removed here entirely. An important assumption is thereby
that as long as the node handler non-empty `negotiating_in` and `negotiating_out`,
the handler is not dependent on such a Delay for task wakeup.
* Correction to the libp2p-ping connection timeout.
The current connection timeout is always short of one `interval`,
because the "countdown" begins with the last received or sent pong
(depending on the policy). In effect, the current default config has
a connection timeout of 5 seconds (20 - 15) from the point when a ping is sent.
Instead, the "countdown" of the connection timeout should always begin
with the next scheduled ping. That also makes all configurations valid,
avoiding pitfalls.
The important properties of the ping handler are now checked to hold for all
configurations, in particular:
* The next ping must be scheduled no earlier than the ping interval
and no later than the connection timeout.
* The "countdown" for the connection timeout starts on the next ping,
i.e. the full connection timeout remains at the instant when the
next ping is sent.
* Do not keep connections alive.
The ping protocol is not supposed to keep otherwise idle connections
alive, only to add an additional condition for terminating them in
the form of a configurable number of consecutive failed ping requests.
In this context, the `PingPolicy` does not seem useful any longer.
2019-04-20 16:16:31 +02:00
|
|
|
//! The ping protocol can be used as a simple application-layer health check
|
|
|
|
//! for connections of any [`Transport`] as well as to measure and record
|
|
|
|
//! round-trip times.
|
2017-11-22 10:58:06 +01:00
|
|
|
//!
|
|
|
|
//! # Usage
|
|
|
|
//!
|
2022-10-01 00:19:34 +10:00
|
|
|
//! The [`Behaviour`] struct implements the [`NetworkBehaviour`] trait. When used with a [`Swarm`],
|
2019-04-16 15:57:29 +02:00
|
|
|
//! it will respond to inbound ping requests and as necessary periodically send outbound
|
2020-08-10 12:54:55 +02:00
|
|
|
//! ping requests on every established connection. If a configurable number of consecutive
|
|
|
|
//! pings fail, the connection will be closed.
|
2017-11-22 10:58:06 +01:00
|
|
|
//!
|
2022-10-01 00:19:34 +10:00
|
|
|
//! The [`Behaviour`] network behaviour produces [`Event`]s, which may be consumed from the [`Swarm`]
|
2019-04-16 15:57:29 +02:00
|
|
|
//! by an application, e.g. to collect statistics.
|
2017-12-06 13:36:41 +01:00
|
|
|
//!
|
2020-08-10 12:54:55 +02:00
|
|
|
//! > **Note**: The ping protocol does not keep otherwise idle connections alive
|
2022-10-01 00:19:34 +10:00
|
|
|
//! > by default, see [`Config::with_keep_alive`] for changing this behaviour.
|
Remove libp2p-ping keep-alive functionality. (#1067)
* Fix connection & handler shutdown when using `KeepAlive::Now`.
Delay::new(Instant::now()) is never immediately ready, resulting in
`KeepAlive::Now` to have no effect, since the delay is re-created on
every execution of `poll()` in the `NodeHandlerWrapper`. It can also
send the node handler into a busy-loop, since every newly
created Delay will trigger a task wakeup, which creates a new Delay
with Instant::now(), and so forth.
The use of `Delay::new(Instant::now())` for "immediate" connection shutdown
is therefore removed here entirely. An important assumption is thereby
that as long as the node handler non-empty `negotiating_in` and `negotiating_out`,
the handler is not dependent on such a Delay for task wakeup.
* Correction to the libp2p-ping connection timeout.
The current connection timeout is always short of one `interval`,
because the "countdown" begins with the last received or sent pong
(depending on the policy). In effect, the current default config has
a connection timeout of 5 seconds (20 - 15) from the point when a ping is sent.
Instead, the "countdown" of the connection timeout should always begin
with the next scheduled ping. That also makes all configurations valid,
avoiding pitfalls.
The important properties of the ping handler are now checked to hold for all
configurations, in particular:
* The next ping must be scheduled no earlier than the ping interval
and no later than the connection timeout.
* The "countdown" for the connection timeout starts on the next ping,
i.e. the full connection timeout remains at the instant when the
next ping is sent.
* Do not keep connections alive.
The ping protocol is not supposed to keep otherwise idle connections
alive, only to add an additional condition for terminating them in
the form of a configurable number of consecutive failed ping requests.
In this context, the `PingPolicy` does not seem useful any longer.
2019-04-20 16:16:31 +02:00
|
|
|
//!
|
2020-02-10 15:17:08 +01:00
|
|
|
//! [`Swarm`]: libp2p_swarm::Swarm
|
2019-04-16 15:57:29 +02:00
|
|
|
//! [`Transport`]: libp2p_core::Transport
|
2017-11-22 10:58:06 +01:00
|
|
|
|
2022-10-24 04:00:20 +02:00
|
|
|
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
|
|
|
|
|
2021-09-07 00:10:48 +10:00
|
|
|
mod handler;
|
|
|
|
mod protocol;
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2021-09-07 00:10:48 +10:00
|
|
|
use handler::Handler;
|
|
|
|
pub use handler::{Config, Failure, Success};
|
2021-07-31 03:48:32 +10:00
|
|
|
use libp2p_core::{connection::ConnectionId, PeerId};
|
2022-11-17 09:28:40 +00:00
|
|
|
use libp2p_swarm::{
|
|
|
|
behaviour::FromSwarm, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
|
|
|
|
};
|
2021-09-07 00:10:48 +10:00
|
|
|
use std::{
|
|
|
|
collections::VecDeque,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
2022-10-01 00:19:34 +10:00
|
|
|
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Config instead.")]
|
|
|
|
pub type PingConfig = Config;
|
|
|
|
|
|
|
|
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Event instead.")]
|
|
|
|
pub type PingEvent = Event;
|
|
|
|
|
|
|
|
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Success instead.")]
|
|
|
|
pub type PingSuccess = Success;
|
|
|
|
|
|
|
|
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Failure instead.")]
|
|
|
|
pub type PingFailure = Failure;
|
|
|
|
|
|
|
|
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Result instead.")]
|
|
|
|
pub type PingResult = Result;
|
|
|
|
|
|
|
|
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Behaviour instead.")]
|
|
|
|
pub type Ping = Behaviour;
|
|
|
|
|
|
|
|
pub use self::protocol::PROTOCOL_NAME;
|
2021-09-07 00:10:48 +10:00
|
|
|
|
|
|
|
/// The result of an inbound or outbound ping.
|
|
|
|
pub type Result = std::result::Result<Success, Failure>;
|
2019-01-03 20:16:44 +01:00
|
|
|
|
2021-09-07 00:10:48 +10:00
|
|
|
/// A [`NetworkBehaviour`] that responds to inbound pings and
|
2019-04-16 15:57:29 +02:00
|
|
|
/// periodically sends outbound pings on every established connection.
|
2019-01-09 11:39:54 +01:00
|
|
|
///
|
|
|
|
/// See the crate root documentation for more information.
|
2021-09-07 00:10:48 +10:00
|
|
|
pub struct Behaviour {
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Configuration for outbound pings.
|
2021-09-07 00:10:48 +10:00
|
|
|
config: Config,
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Queue of events to yield to the swarm.
|
2021-09-07 00:10:48 +10:00
|
|
|
events: VecDeque<Event>,
|
2019-01-03 20:16:44 +01:00
|
|
|
}
|
|
|
|
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Event generated by the `Ping` network behaviour.
|
|
|
|
#[derive(Debug)]
|
2021-09-07 00:10:48 +10:00
|
|
|
pub struct Event {
|
2019-04-16 15:57:29 +02:00
|
|
|
/// The peer ID of the remote.
|
|
|
|
pub peer: PeerId,
|
|
|
|
/// The result of an inbound or outbound ping.
|
2021-09-07 00:10:48 +10:00
|
|
|
pub result: Result,
|
2019-01-03 20:16:44 +01:00
|
|
|
}
|
|
|
|
|
2021-09-07 00:10:48 +10:00
|
|
|
impl Behaviour {
|
2019-04-16 15:57:29 +02:00
|
|
|
/// Creates a new `Ping` network behaviour with the given configuration.
|
2021-09-07 00:10:48 +10:00
|
|
|
pub fn new(config: Config) -> Self {
|
|
|
|
Self {
|
2019-04-16 15:57:29 +02:00
|
|
|
config,
|
|
|
|
events: VecDeque::new(),
|
2019-01-03 20:16:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 00:10:48 +10:00
|
|
|
impl Default for Behaviour {
|
2019-01-03 20:16:44 +01:00
|
|
|
fn default() -> Self {
|
2021-09-07 00:10:48 +10:00
|
|
|
Self::new(Config::new())
|
2019-01-03 20:16:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 00:10:48 +10:00
|
|
|
impl NetworkBehaviour for Behaviour {
|
2022-02-21 13:32:24 +01:00
|
|
|
type ConnectionHandler = Handler;
|
2021-09-07 00:10:48 +10:00
|
|
|
type OutEvent = Event;
|
2019-01-03 20:16:44 +01:00
|
|
|
|
2022-02-21 13:32:24 +01:00
|
|
|
fn new_handler(&mut self) -> Self::ConnectionHandler {
|
2021-09-07 00:10:48 +10:00
|
|
|
Handler::new(self.config.clone())
|
2019-01-03 20:16:44 +01:00
|
|
|
}
|
|
|
|
|
2022-11-17 09:28:40 +00:00
|
|
|
fn on_connection_handler_event(&mut self, peer: PeerId, _: ConnectionId, result: Result) {
|
2021-09-07 00:10:48 +10:00
|
|
|
self.events.push_front(Event { peer, result })
|
2019-01-03 20:16:44 +01:00
|
|
|
}
|
|
|
|
|
2020-07-27 20:27:33 +00:00
|
|
|
fn poll(
|
|
|
|
&mut self,
|
|
|
|
_: &mut Context<'_>,
|
|
|
|
_: &mut impl PollParameters,
|
2022-02-21 13:32:24 +01:00
|
|
|
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
|
2019-04-16 15:57:29 +02:00
|
|
|
if let Some(e) = self.events.pop_back() {
|
2022-04-07 21:37:02 +02:00
|
|
|
let Event { result, peer } = &e;
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(Success::Ping { .. }) => log::debug!("Ping sent to {:?}", peer),
|
|
|
|
Ok(Success::Pong) => log::debug!("Ping received from {:?}", peer),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(NetworkBehaviourAction::GenerateEvent(e))
|
2019-04-16 15:57:29 +02:00
|
|
|
} else {
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Pending
|
2019-01-03 20:16:44 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-17 09:28:40 +00:00
|
|
|
|
|
|
|
fn on_swarm_event(
|
|
|
|
&mut self,
|
|
|
|
event: libp2p_swarm::behaviour::FromSwarm<Self::ConnectionHandler>,
|
|
|
|
) {
|
|
|
|
match event {
|
|
|
|
FromSwarm::ConnectionEstablished(_)
|
|
|
|
| FromSwarm::ConnectionClosed(_)
|
|
|
|
| FromSwarm::AddressChange(_)
|
|
|
|
| FromSwarm::DialFailure(_)
|
|
|
|
| FromSwarm::ListenFailure(_)
|
|
|
|
| FromSwarm::NewListener(_)
|
|
|
|
| FromSwarm::NewListenAddr(_)
|
|
|
|
| FromSwarm::ExpiredListenAddr(_)
|
|
|
|
| FromSwarm::ListenerError(_)
|
|
|
|
| FromSwarm::ListenerClosed(_)
|
|
|
|
| FromSwarm::NewExternalAddr(_)
|
|
|
|
| FromSwarm::ExpiredExternalAddr(_) => {}
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 20:16:44 +01:00
|
|
|
}
|