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

protocols/ping: Properly deprecate types with Ping prefix #2937

Merged
merged 6 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
9 changes: 4 additions & 5 deletions examples/ipfs-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ use libp2p::{
identify::{Identify, IdentifyConfig, IdentifyEvent},
identity,
multiaddr::Protocol,
noise,
ping::{self, PingEvent},
noise, ping,
pnet::{PnetConfig, PreSharedKey},
swarm::SwarmEvent,
tcp::TcpTransport,
Expand Down Expand Up @@ -165,7 +164,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
enum MyBehaviourEvent {
Gossipsub(GossipsubEvent),
Identify(IdentifyEvent),
Ping(PingEvent),
Ping(ping::Event),
}

impl From<GossipsubEvent> for MyBehaviourEvent {
Expand All @@ -180,8 +179,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}

impl From<PingEvent> for MyBehaviourEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for MyBehaviourEvent {
fn from(event: ping::Event) -> Self {
MyBehaviourEvent::Ping(event)
}
}
Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/examples/metrics/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use futures::executor::block_on;
use futures::stream::StreamExt;
use libp2p::core::Multiaddr;
use libp2p::metrics::{Metrics, Recorder};
use libp2p::ping::{Ping, PingConfig};
use libp2p::ping;
use libp2p::swarm::SwarmEvent;
use libp2p::{identity, PeerId, Swarm};
use prometheus_client::registry::Registry;
Expand All @@ -72,7 +72,7 @@ fn main() -> Result<(), Box<dyn Error>> {

let mut swarm = Swarm::new(
block_on(libp2p::development_transport(local_key))?,
Ping::new(PingConfig::new().with_keep_alive(true)),
ping::Behaviour::new(ping::Config::new().with_keep_alive(true)),
local_peer_id,
);

Expand Down
4 changes: 2 additions & 2 deletions misc/metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ impl Recorder<libp2p_kad::KademliaEvent> for Metrics {
}

#[cfg(feature = "ping")]
impl Recorder<libp2p_ping::PingEvent> for Metrics {
fn record(&self, event: &libp2p_ping::PingEvent) {
impl Recorder<libp2p_ping::Event> for Metrics {
fn record(&self, event: &libp2p_ping::Event) {
self.ping.record(event)
}
}
Expand Down
18 changes: 9 additions & 9 deletions misc/metrics/src/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ struct FailureLabels {
reason: Failure,
}

impl From<&libp2p_ping::PingFailure> for FailureLabels {
fn from(failure: &libp2p_ping::PingFailure) -> Self {
impl From<&libp2p_ping::Failure> for FailureLabels {
fn from(failure: &libp2p_ping::Failure) -> Self {
match failure {
libp2p_ping::PingFailure::Timeout => FailureLabels {
libp2p_ping::Failure::Timeout => FailureLabels {
reason: Failure::Timeout,
},
libp2p_ping::PingFailure::Unsupported => FailureLabels {
libp2p_ping::Failure::Unsupported => FailureLabels {
reason: Failure::Unsupported,
},
libp2p_ping::PingFailure::Other { .. } => FailureLabels {
libp2p_ping::Failure::Other { .. } => FailureLabels {
reason: Failure::Other,
},
}
Expand Down Expand Up @@ -92,13 +92,13 @@ impl Metrics {
}
}

impl super::Recorder<libp2p_ping::PingEvent> for Metrics {
fn record(&self, event: &libp2p_ping::PingEvent) {
impl super::Recorder<libp2p_ping::Event> for Metrics {
fn record(&self, event: &libp2p_ping::Event) {
match &event.result {
Ok(libp2p_ping::PingSuccess::Pong) => {
Ok(libp2p_ping::Success::Pong) => {
self.pong_received.inc();
}
Ok(libp2p_ping::PingSuccess::Ping { rtt }) => {
Ok(libp2p_ping::Success::Ping { rtt }) => {
self.rtt.observe(rtt.as_secs_f64());
}
Err(failure) => {
Expand Down
13 changes: 6 additions & 7 deletions protocols/dcutr/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ use futures::stream::StreamExt;
use libp2p::core::multiaddr::{Multiaddr, Protocol};
use libp2p::core::transport::OrTransport;
use libp2p::core::upgrade;
use libp2p::dcutr;
use libp2p::dns::DnsConfig;
use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent, IdentifyInfo};
use libp2p::noise;
use libp2p::ping::{Ping, PingConfig, PingEvent};
use libp2p::relay::v2::client::{self, Client};
use libp2p::swarm::{SwarmBuilder, SwarmEvent};
use libp2p::tcp::{GenTcpConfig, TcpTransport};
use libp2p::Transport;
use libp2p::{dcutr, ping};
use libp2p::{identity, NetworkBehaviour, PeerId};
use log::info;
use std::convert::TryInto;
Expand Down Expand Up @@ -108,21 +107,21 @@ fn main() -> Result<(), Box<dyn Error>> {
#[behaviour(out_event = "Event", event_process = false)]
struct Behaviour {
relay_client: Client,
ping: Ping,
ping: ping::Behaviour,
identify: Identify,
dcutr: dcutr::behaviour::Behaviour,
}

#[derive(Debug)]
enum Event {
Ping(PingEvent),
Ping(ping::Event),
Identify(IdentifyEvent),
Relay(client::Event),
Dcutr(dcutr::behaviour::Event),
}

impl From<PingEvent> for Event {
fn from(e: PingEvent) -> Self {
impl From<ping::Event> for Event {
fn from(e: ping::Event) -> Self {
Event::Ping(e)
}
}
Expand All @@ -147,7 +146,7 @@ fn main() -> Result<(), Box<dyn Error>> {

let behaviour = Behaviour {
relay_client: client,
ping: Ping::new(PingConfig::new()),
ping: ping::Behaviour::new(ping::Config::new()),
identify: Identify::new(IdentifyConfig::new(
"/TODO/0.0.1".to_string(),
local_key.public(),
Expand Down
3 changes: 3 additions & 0 deletions protocols/ping/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# 0.39.1 [unreleased]

- Bump rand to 0.8 and quickcheck to 1. See [PR 2857].
- Deprecate types with `Ping` prefix. Prefer importing them via the `ping` namespace, i.e. `libp2p::ping::Event` instead
of `libp2p::ping::PingEvent`. See [PR 2937].

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

# 0.39.0

Expand Down
4 changes: 2 additions & 2 deletions protocols/ping/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct Config {
}

impl Config {
/// Creates a new `PingConfig` with the following default settings:
/// Creates a new [`Config`] with the following default settings:
///
/// * [`Config::with_interval`] 15s
/// * [`Config::with_timeout`] 20s
Expand Down Expand Up @@ -208,7 +208,7 @@ enum State {
}

impl Handler {
/// Builds a new `PingHandler` with the given configuration.
/// Builds a new [`Handler`] with the given configuration.
pub fn new(config: Config) -> Self {
Handler {
config,
Expand Down
35 changes: 22 additions & 13 deletions protocols/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
//!
//! # Usage
//!
//! The [`Ping`] struct implements the [`NetworkBehaviour`] trait. When used with a [`Swarm`],
//! The [`Behaviour`] struct implements the [`NetworkBehaviour`] trait. When used with a [`Swarm`],
//! it will respond to inbound ping requests and as necessary periodically send outbound
//! ping requests on every established connection. If a configurable number of consecutive
//! pings fail, the connection will be closed.
//!
//! The `Ping` network behaviour produces [`PingEvent`]s, which may be consumed from the `Swarm`
//! The [`Behaviour`] network behaviour produces [`Event`]s, which may be consumed from the [`Swarm`]
//! by an application, e.g. to collect statistics.
//!
//! > **Note**: The ping protocol does not keep otherwise idle connections alive
//! > by default, see [`PingConfig::with_keep_alive`] for changing this behaviour.
//! > by default, see [`Config::with_keep_alive`] for changing this behaviour.
//!
//! [`Swarm`]: libp2p_swarm::Swarm
//! [`Transport`]: libp2p_core::Transport
Expand All @@ -52,16 +52,25 @@ use std::{
task::{Context, Poll},
};

#[deprecated(
since = "0.30.0",
note = "Use re-exports that omit `Ping` prefix, i.e. `libp2p::ping::Config` etc"
)]
pub use self::{
protocol::PROTOCOL_NAME, 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;
#[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;

/// The result of an inbound or outbound ping.
pub type Result = std::result::Result<Success, Failure>;
Expand Down
13 changes: 6 additions & 7 deletions protocols/relay/examples/relay_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ use futures::stream::StreamExt;
use libp2p::core::upgrade;
use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent};
use libp2p::multiaddr::Protocol;
use libp2p::ping::{Ping, PingConfig, PingEvent};
use libp2p::relay::v2::relay::{self, Relay};
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::tcp::TcpTransport;
use libp2p::Transport;
use libp2p::{identity, NetworkBehaviour, PeerId};
use libp2p::{noise, Multiaddr};
use libp2p::{ping, Transport};
use std::error::Error;
use std::net::{Ipv4Addr, Ipv6Addr};

Expand Down Expand Up @@ -59,7 +58,7 @@ fn main() -> Result<(), Box<dyn Error>> {

let behaviour = Behaviour {
relay: Relay::new(local_peer_id, Default::default()),
ping: Ping::new(PingConfig::new()),
ping: ping::Behaviour::new(ping::Config::new()),
identify: Identify::new(IdentifyConfig::new(
"/TODO/0.0.1".to_string(),
local_key.public(),
Expand Down Expand Up @@ -96,19 +95,19 @@ fn main() -> Result<(), Box<dyn Error>> {
#[behaviour(out_event = "Event", event_process = false)]
struct Behaviour {
relay: Relay,
ping: Ping,
ping: ping::Behaviour,
identify: Identify,
}

#[derive(Debug)]
enum Event {
Ping(PingEvent),
Ping(ping::Event),
Identify(IdentifyEvent),
Relay(relay::Event),
}

impl From<PingEvent> for Event {
fn from(e: PingEvent) -> Self {
impl From<ping::Event> for Event {
fn from(e: ping::Event) -> Self {
Event::Ping(e)
}
}
Expand Down
27 changes: 13 additions & 14 deletions protocols/relay/tests/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ use libp2p::core::transport::choice::OrTransport;
use libp2p::core::transport::{Boxed, MemoryTransport, Transport};
use libp2p::core::PublicKey;
use libp2p::core::{identity, upgrade, PeerId};
use libp2p::ping::{Ping, PingConfig, PingEvent};
use libp2p::plaintext::PlainText2Config;
use libp2p::relay::v2::client;
use libp2p::relay::v2::relay;
use libp2p::swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
use libp2p::NetworkBehaviour;
use libp2p::{ping, NetworkBehaviour};
use std::time::Duration;

#[test]
Expand Down Expand Up @@ -217,15 +216,15 @@ fn connect() {
match src.select_next_some().await {
SwarmEvent::Dialing(peer_id) if peer_id == relay_peer_id => {}
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == relay_peer_id => {}
SwarmEvent::Behaviour(ClientEvent::Ping(PingEvent { peer, .. }))
SwarmEvent::Behaviour(ClientEvent::Ping(ping::Event { peer, .. }))
if peer == dst_peer_id =>
{
break
}
SwarmEvent::Behaviour(ClientEvent::Relay(
client::Event::OutboundCircuitEstablished { .. },
)) => {}
SwarmEvent::Behaviour(ClientEvent::Ping(PingEvent { peer, .. }))
SwarmEvent::Behaviour(ClientEvent::Ping(ping::Event { peer, .. }))
if peer == relay_peer_id => {}
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == dst_peer_id => {
break
Expand Down Expand Up @@ -299,7 +298,7 @@ fn build_relay() -> Swarm<Relay> {
Swarm::new(
transport,
Relay {
ping: Ping::new(PingConfig::new()),
ping: ping::Behaviour::new(ping::Config::new()),
relay: relay::Relay::new(
local_peer_id,
relay::Config {
Expand All @@ -326,7 +325,7 @@ fn build_client() -> Swarm<Client> {
Swarm::new(
transport,
Client {
ping: Ping::new(PingConfig::new()),
ping: ping::Behaviour::new(ping::Config::new()),
relay: behaviour,
},
local_peer_id,
Expand All @@ -351,13 +350,13 @@ where
#[behaviour(out_event = "RelayEvent", event_process = false)]
struct Relay {
relay: relay::Relay,
ping: Ping,
ping: ping::Behaviour,
}

#[derive(Debug)]
enum RelayEvent {
Relay(relay::Event),
Ping(PingEvent),
Ping(ping::Event),
}

impl From<relay::Event> for RelayEvent {
Expand All @@ -366,8 +365,8 @@ impl From<relay::Event> for RelayEvent {
}
}

impl From<PingEvent> for RelayEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for RelayEvent {
fn from(event: ping::Event) -> Self {
RelayEvent::Ping(event)
}
}
Expand All @@ -376,13 +375,13 @@ impl From<PingEvent> for RelayEvent {
#[behaviour(out_event = "ClientEvent", event_process = false)]
struct Client {
relay: client::Client,
ping: Ping,
ping: ping::Behaviour,
}

#[derive(Debug)]
enum ClientEvent {
Relay(client::Event),
Ping(PingEvent),
Ping(ping::Event),
}

impl From<client::Event> for ClientEvent {
Expand All @@ -391,8 +390,8 @@ impl From<client::Event> for ClientEvent {
}
}

impl From<PingEvent> for ClientEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for ClientEvent {
fn from(event: ping::Event) -> Self {
ClientEvent::Ping(event)
}
}
Expand Down
Loading