mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 17:21:34 +00:00
protocols/ping: Revise naming of symbols (#2215)
Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
@ -39,7 +39,7 @@ use wasm_timer::Delay;
|
||||
|
||||
/// The configuration for outbound pings.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PingConfig {
|
||||
pub struct Config {
|
||||
/// The timeout of an outbound ping.
|
||||
timeout: Duration,
|
||||
/// The duration between the last successful outbound or inbound ping
|
||||
@ -54,13 +54,13 @@ pub struct PingConfig {
|
||||
keep_alive: bool,
|
||||
}
|
||||
|
||||
impl PingConfig {
|
||||
impl Config {
|
||||
/// Creates a new `PingConfig` with the following default settings:
|
||||
///
|
||||
/// * [`PingConfig::with_interval`] 15s
|
||||
/// * [`PingConfig::with_timeout`] 20s
|
||||
/// * [`PingConfig::with_max_failures`] 1
|
||||
/// * [`PingConfig::with_keep_alive`] false
|
||||
/// * [`Config::with_interval`] 15s
|
||||
/// * [`Config::with_timeout`] 20s
|
||||
/// * [`Config::with_max_failures`] 1
|
||||
/// * [`Config::with_keep_alive`] false
|
||||
///
|
||||
/// These settings have the following effect:
|
||||
///
|
||||
@ -116,12 +116,9 @@ impl PingConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// The result of an inbound or outbound ping.
|
||||
pub type PingResult = Result<PingSuccess, PingFailure>;
|
||||
|
||||
/// The successful result of processing an inbound or outbound ping.
|
||||
#[derive(Debug)]
|
||||
pub enum PingSuccess {
|
||||
pub enum Success {
|
||||
/// Received a ping and sent back a pong.
|
||||
Pong,
|
||||
/// Sent a ping and received back a pong.
|
||||
@ -132,7 +129,7 @@ pub enum PingSuccess {
|
||||
|
||||
/// An outbound ping failure.
|
||||
#[derive(Debug)]
|
||||
pub enum PingFailure {
|
||||
pub enum Failure {
|
||||
/// The ping timed out, i.e. no response was received within the
|
||||
/// configured ping timeout.
|
||||
Timeout,
|
||||
@ -144,22 +141,22 @@ pub enum PingFailure {
|
||||
},
|
||||
}
|
||||
|
||||
impl fmt::Display for PingFailure {
|
||||
impl fmt::Display for Failure {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
PingFailure::Timeout => f.write_str("Ping timeout"),
|
||||
PingFailure::Other { error } => write!(f, "Ping error: {}", error),
|
||||
PingFailure::Unsupported => write!(f, "Ping protocol not supported"),
|
||||
Failure::Timeout => f.write_str("Ping timeout"),
|
||||
Failure::Other { error } => write!(f, "Ping error: {}", error),
|
||||
Failure::Unsupported => write!(f, "Ping protocol not supported"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for PingFailure {
|
||||
impl Error for Failure {
|
||||
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
||||
match self {
|
||||
PingFailure::Timeout => None,
|
||||
PingFailure::Other { error } => Some(&**error),
|
||||
PingFailure::Unsupported => None,
|
||||
Failure::Timeout => None,
|
||||
Failure::Other { error } => Some(&**error),
|
||||
Failure::Unsupported => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -168,14 +165,14 @@ impl Error for PingFailure {
|
||||
/// and answering ping queries.
|
||||
///
|
||||
/// If the remote doesn't respond, produces an error that closes the connection.
|
||||
pub struct PingHandler {
|
||||
pub struct Handler {
|
||||
/// Configuration options.
|
||||
config: PingConfig,
|
||||
config: Config,
|
||||
/// The timer used for the delay to the next ping as well as
|
||||
/// the ping timeout.
|
||||
timer: Delay,
|
||||
/// Outbound ping failures that are pending to be processed by `poll()`.
|
||||
pending_errors: VecDeque<PingFailure>,
|
||||
pending_errors: VecDeque<Failure>,
|
||||
/// The number of consecutive ping failures that occurred.
|
||||
///
|
||||
/// Each successful ping resets this counter to 0.
|
||||
@ -203,10 +200,10 @@ enum State {
|
||||
Active,
|
||||
}
|
||||
|
||||
impl PingHandler {
|
||||
impl Handler {
|
||||
/// Builds a new `PingHandler` with the given configuration.
|
||||
pub fn new(config: PingConfig) -> Self {
|
||||
PingHandler {
|
||||
pub fn new(config: Config) -> Self {
|
||||
Handler {
|
||||
config,
|
||||
timer: Delay::new(Duration::new(0, 0)),
|
||||
pending_errors: VecDeque::with_capacity(2),
|
||||
@ -218,10 +215,10 @@ impl PingHandler {
|
||||
}
|
||||
}
|
||||
|
||||
impl ProtocolsHandler for PingHandler {
|
||||
impl ProtocolsHandler for Handler {
|
||||
type InEvent = Void;
|
||||
type OutEvent = PingResult;
|
||||
type Error = PingFailure;
|
||||
type OutEvent = crate::Result;
|
||||
type Error = Failure;
|
||||
type InboundProtocol = protocol::Ping;
|
||||
type OutboundProtocol = protocol::Ping;
|
||||
type OutboundOpenInfo = ();
|
||||
@ -253,8 +250,8 @@ impl ProtocolsHandler for PingHandler {
|
||||
return;
|
||||
}
|
||||
// Note: This timeout only covers protocol negotiation.
|
||||
ProtocolsHandlerUpgrErr::Timeout => PingFailure::Timeout,
|
||||
e => PingFailure::Other { error: Box::new(e) },
|
||||
ProtocolsHandlerUpgrErr::Timeout => Failure::Timeout,
|
||||
e => Failure::Other { error: Box::new(e) },
|
||||
};
|
||||
|
||||
self.pending_errors.push_front(error);
|
||||
@ -271,14 +268,14 @@ impl ProtocolsHandler for PingHandler {
|
||||
fn poll(
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<ProtocolsHandlerEvent<protocol::Ping, (), PingResult, Self::Error>> {
|
||||
) -> Poll<ProtocolsHandlerEvent<protocol::Ping, (), crate::Result, Self::Error>> {
|
||||
match self.state {
|
||||
State::Inactive { reported: true } => {
|
||||
return Poll::Pending; // nothing to do on this connection
|
||||
}
|
||||
State::Inactive { reported: false } => {
|
||||
self.state = State::Inactive { reported: true };
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Err(PingFailure::Unsupported)));
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Err(Failure::Unsupported)));
|
||||
}
|
||||
State::Active => {}
|
||||
}
|
||||
@ -294,7 +291,7 @@ impl ProtocolsHandler for PingHandler {
|
||||
Poll::Ready(Ok(stream)) => {
|
||||
// A ping from a remote peer has been answered, wait for the next.
|
||||
self.inbound = Some(protocol::recv_ping(stream).boxed());
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Ok(PingSuccess::Pong)));
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Ok(Success::Pong)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -328,7 +325,7 @@ impl ProtocolsHandler for PingHandler {
|
||||
Some(PingState::Ping(mut ping)) => match ping.poll_unpin(cx) {
|
||||
Poll::Pending => {
|
||||
if self.timer.poll_unpin(cx).is_ready() {
|
||||
self.pending_errors.push_front(PingFailure::Timeout);
|
||||
self.pending_errors.push_front(Failure::Timeout);
|
||||
} else {
|
||||
self.outbound = Some(PingState::Ping(ping));
|
||||
break;
|
||||
@ -338,13 +335,13 @@ impl ProtocolsHandler for PingHandler {
|
||||
self.failures = 0;
|
||||
self.timer.reset(self.config.interval);
|
||||
self.outbound = Some(PingState::Idle(stream));
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Ok(PingSuccess::Ping {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Custom(Ok(Success::Ping {
|
||||
rtt,
|
||||
})));
|
||||
}
|
||||
Poll::Ready(Err(e)) => {
|
||||
self.pending_errors
|
||||
.push_front(PingFailure::Other { error: Box::new(e) });
|
||||
.push_front(Failure::Other { error: Box::new(e) });
|
||||
}
|
||||
},
|
||||
Some(PingState::Idle(stream)) => match self.timer.poll_unpin(cx) {
|
||||
@ -357,7 +354,7 @@ impl ProtocolsHandler for PingHandler {
|
||||
self.outbound = Some(PingState::Ping(protocol::send_ping(stream).boxed()));
|
||||
}
|
||||
Poll::Ready(Err(e)) => {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Close(PingFailure::Other {
|
||||
return Poll::Ready(ProtocolsHandlerEvent::Close(Failure::Other {
|
||||
error: Box::new(e),
|
||||
}))
|
||||
}
|
||||
|
@ -40,62 +40,78 @@
|
||||
//! [`Swarm`]: libp2p_swarm::Swarm
|
||||
//! [`Transport`]: libp2p_core::Transport
|
||||
|
||||
pub mod handler;
|
||||
pub mod protocol;
|
||||
|
||||
use handler::PingHandler;
|
||||
pub use handler::{PingConfig, PingFailure, PingResult, PingSuccess};
|
||||
mod handler;
|
||||
mod protocol;
|
||||
|
||||
use handler::Handler;
|
||||
pub use handler::{Config, Failure, Success};
|
||||
use libp2p_core::{connection::ConnectionId, PeerId};
|
||||
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
|
||||
use std::{collections::VecDeque, task::Context, task::Poll};
|
||||
use std::{
|
||||
collections::VecDeque,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
/// `Ping` is a [`NetworkBehaviour`] that responds to inbound pings and
|
||||
#[deprecated(
|
||||
since = "0.30.0",
|
||||
note = "Use re-exports that omit `Ping` prefix, i.e. `libp2p::ping::Config` etc"
|
||||
)]
|
||||
pub use self::{
|
||||
Config as PingConfig, Event as PingEvent, Failure as PingFailure, Result as PingResult,
|
||||
Success as PingSuccess,
|
||||
};
|
||||
#[deprecated(since = "0.30.0", note = "Use libp2p::ping::Behaviour instead.")]
|
||||
pub use Behaviour as Ping;
|
||||
|
||||
/// The result of an inbound or outbound ping.
|
||||
pub type Result = std::result::Result<Success, Failure>;
|
||||
|
||||
/// A [`NetworkBehaviour`] that responds to inbound pings and
|
||||
/// periodically sends outbound pings on every established connection.
|
||||
///
|
||||
/// See the crate root documentation for more information.
|
||||
pub struct Ping {
|
||||
pub struct Behaviour {
|
||||
/// Configuration for outbound pings.
|
||||
config: PingConfig,
|
||||
config: Config,
|
||||
/// Queue of events to yield to the swarm.
|
||||
events: VecDeque<PingEvent>,
|
||||
events: VecDeque<Event>,
|
||||
}
|
||||
|
||||
/// Event generated by the `Ping` network behaviour.
|
||||
#[derive(Debug)]
|
||||
pub struct PingEvent {
|
||||
pub struct Event {
|
||||
/// The peer ID of the remote.
|
||||
pub peer: PeerId,
|
||||
/// The result of an inbound or outbound ping.
|
||||
pub result: PingResult,
|
||||
pub result: Result,
|
||||
}
|
||||
|
||||
impl Ping {
|
||||
impl Behaviour {
|
||||
/// Creates a new `Ping` network behaviour with the given configuration.
|
||||
pub fn new(config: PingConfig) -> Self {
|
||||
Ping {
|
||||
pub fn new(config: Config) -> Self {
|
||||
Self {
|
||||
config,
|
||||
events: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Ping {
|
||||
impl Default for Behaviour {
|
||||
fn default() -> Self {
|
||||
Ping::new(PingConfig::new())
|
||||
Self::new(Config::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl NetworkBehaviour for Ping {
|
||||
type ProtocolsHandler = PingHandler;
|
||||
type OutEvent = PingEvent;
|
||||
impl NetworkBehaviour for Behaviour {
|
||||
type ProtocolsHandler = Handler;
|
||||
type OutEvent = Event;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
PingHandler::new(self.config.clone())
|
||||
Handler::new(self.config.clone())
|
||||
}
|
||||
|
||||
fn inject_event(&mut self, peer: PeerId, _: ConnectionId, result: PingResult) {
|
||||
self.events.push_front(PingEvent { peer, result })
|
||||
fn inject_event(&mut self, peer: PeerId, _: ConnectionId, result: Result) {
|
||||
self.events.push_front(Event { peer, result })
|
||||
}
|
||||
|
||||
fn poll(
|
||||
|
Reference in New Issue
Block a user