From 359a6d6d03755ae2c03b4cefc7858e0a284c13be Mon Sep 17 00:00:00 2001 From: Arpan Kapoor Date: Thu, 28 Sep 2023 09:50:27 +0530 Subject: [PATCH 1/4] perf: do not return failure events repeatedly --- protocols/upnp/src/behaviour.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/protocols/upnp/src/behaviour.rs b/protocols/upnp/src/behaviour.rs index 063734d9b5b..45b82edc562 100644 --- a/protocols/upnp/src/behaviour.rs +++ b/protocols/upnp/src/behaviour.rs @@ -513,12 +513,7 @@ impl NetworkBehaviour for Behaviour { self.mappings.renew(gateway, cx); return Poll::Pending; } - GatewayState::GatewayNotFound => { - return Poll::Ready(ToSwarm::GenerateEvent(Event::GatewayNotFound)); - } - GatewayState::NonRoutableGateway(_) => { - return Poll::Ready(ToSwarm::GenerateEvent(Event::NonRoutableGateway)); - } + _ => return Poll::Pending, } } } From 66f569ff0825775a566eb495ddc93d857aa77f82 Mon Sep 17 00:00:00 2001 From: Arpan Kapoor Date: Thu, 28 Sep 2023 09:51:59 +0530 Subject: [PATCH 2/4] add high cpu usage example --- Cargo.lock | 3 +-- examples/ping/Cargo.toml | 5 ++--- examples/ping/src/main.rs | 33 ++++++++++++++++++++++++--------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 89d3b70ba8c..3fcf81ae2f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4008,11 +4008,10 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" name = "ping-example" version = "0.1.0" dependencies = [ - "async-std", - "async-trait", "env_logger 0.10.0", "futures", "libp2p", + "tokio", ] [[package]] diff --git a/examples/ping/Cargo.toml b/examples/ping/Cargo.toml index 33c9e56b45c..4c00d820cb4 100644 --- a/examples/ping/Cargo.toml +++ b/examples/ping/Cargo.toml @@ -6,8 +6,7 @@ publish = false license = "MIT" [dependencies] -async-std = { version = "1.12", features = ["attributes"] } -async-trait = "0.1" env_logger = "0.10.0" futures = "0.3.28" -libp2p = { path = "../../libp2p", features = ["async-std", "dns", "macros", "noise", "ping", "tcp", "websocket", "yamux"] } +libp2p = { path = "../../libp2p", features = ["dns", "identify", "macros", "noise", "ping", "tcp", "tokio", "upnp", "websocket", "yamux"] } +tokio = { version = "1.32.0", features = ["full"] } diff --git a/examples/ping/src/main.rs b/examples/ping/src/main.rs index 898a25813e0..37783603a6c 100644 --- a/examples/ping/src/main.rs +++ b/examples/ping/src/main.rs @@ -23,29 +23,44 @@ use futures::prelude::*; use libp2p::core::upgrade::Version; use libp2p::{ - identity, noise, ping, - swarm::{SwarmBuilder, SwarmEvent}, - tcp, yamux, Multiaddr, PeerId, Transport, + identify, identity, noise, ping, + swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}, + tcp, upnp, yamux, Multiaddr, PeerId, Transport, }; use std::error::Error; use std::time::Duration; -#[async_std::main] +#[derive(NetworkBehaviour)] +struct Behaviour { + ping: ping::Behaviour, + identify: identify::Behaviour, + upnp: upnp::tokio::Behaviour, +} + +#[tokio::main] async fn main() -> Result<(), Box> { env_logger::init(); let local_key = identity::Keypair::generate_ed25519(); let local_peer_id = PeerId::from(local_key.public()); - let transport = tcp::async_io::Transport::default() + let transport = tcp::tokio::Transport::default() .upgrade(Version::V1Lazy) .authenticate(noise::Config::new(&local_key)?) .multiplex(yamux::Config::default()) .boxed(); - let mut swarm = - SwarmBuilder::with_async_std_executor(transport, ping::Behaviour::default(), local_peer_id) - .idle_connection_timeout(Duration::from_secs(60)) // For illustrative purposes, keep idle connections alive for a minute so we can observe a few pings. - .build(); + let behaviour = Behaviour { + ping: ping::Behaviour::new(ping::Config::new()), + identify: identify::Behaviour::new(identify::Config::new( + "/TODO/0.0.1".to_string(), + local_key.public(), + )), + upnp: upnp::tokio::Behaviour::default(), + }; + + let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id) + .idle_connection_timeout(Duration::from_secs(60)) // For illustrative purposes, keep idle connections alive for a minute so we can observe a few pings. + .build(); // Tell the swarm to listen on all interfaces and a random, OS-assigned // port. From 8d77d6a313d767844393adb90c84b8a35ba130c2 Mon Sep 17 00:00:00 2001 From: Arpan Kapoor Date: Thu, 28 Sep 2023 11:56:49 +0530 Subject: [PATCH 3/4] Revert "add high cpu usage example" This reverts commit 66f569ff0825775a566eb495ddc93d857aa77f82. --- Cargo.lock | 3 ++- examples/ping/Cargo.toml | 5 +++-- examples/ping/src/main.rs | 33 +++++++++------------------------ 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3fcf81ae2f7..89d3b70ba8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4008,10 +4008,11 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" name = "ping-example" version = "0.1.0" dependencies = [ + "async-std", + "async-trait", "env_logger 0.10.0", "futures", "libp2p", - "tokio", ] [[package]] diff --git a/examples/ping/Cargo.toml b/examples/ping/Cargo.toml index 4c00d820cb4..33c9e56b45c 100644 --- a/examples/ping/Cargo.toml +++ b/examples/ping/Cargo.toml @@ -6,7 +6,8 @@ publish = false license = "MIT" [dependencies] +async-std = { version = "1.12", features = ["attributes"] } +async-trait = "0.1" env_logger = "0.10.0" futures = "0.3.28" -libp2p = { path = "../../libp2p", features = ["dns", "identify", "macros", "noise", "ping", "tcp", "tokio", "upnp", "websocket", "yamux"] } -tokio = { version = "1.32.0", features = ["full"] } +libp2p = { path = "../../libp2p", features = ["async-std", "dns", "macros", "noise", "ping", "tcp", "websocket", "yamux"] } diff --git a/examples/ping/src/main.rs b/examples/ping/src/main.rs index 37783603a6c..898a25813e0 100644 --- a/examples/ping/src/main.rs +++ b/examples/ping/src/main.rs @@ -23,44 +23,29 @@ use futures::prelude::*; use libp2p::core::upgrade::Version; use libp2p::{ - identify, identity, noise, ping, - swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}, - tcp, upnp, yamux, Multiaddr, PeerId, Transport, + identity, noise, ping, + swarm::{SwarmBuilder, SwarmEvent}, + tcp, yamux, Multiaddr, PeerId, Transport, }; use std::error::Error; use std::time::Duration; -#[derive(NetworkBehaviour)] -struct Behaviour { - ping: ping::Behaviour, - identify: identify::Behaviour, - upnp: upnp::tokio::Behaviour, -} - -#[tokio::main] +#[async_std::main] async fn main() -> Result<(), Box> { env_logger::init(); let local_key = identity::Keypair::generate_ed25519(); let local_peer_id = PeerId::from(local_key.public()); - let transport = tcp::tokio::Transport::default() + let transport = tcp::async_io::Transport::default() .upgrade(Version::V1Lazy) .authenticate(noise::Config::new(&local_key)?) .multiplex(yamux::Config::default()) .boxed(); - let behaviour = Behaviour { - ping: ping::Behaviour::new(ping::Config::new()), - identify: identify::Behaviour::new(identify::Config::new( - "/TODO/0.0.1".to_string(), - local_key.public(), - )), - upnp: upnp::tokio::Behaviour::default(), - }; - - let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id) - .idle_connection_timeout(Duration::from_secs(60)) // For illustrative purposes, keep idle connections alive for a minute so we can observe a few pings. - .build(); + let mut swarm = + SwarmBuilder::with_async_std_executor(transport, ping::Behaviour::default(), local_peer_id) + .idle_connection_timeout(Duration::from_secs(60)) // For illustrative purposes, keep idle connections alive for a minute so we can observe a few pings. + .build(); // Tell the swarm to listen on all interfaces and a random, OS-assigned // port. From a1fe0cbcf50774adff9ce55d11c70647b55353e5 Mon Sep 17 00:00:00 2001 From: Arpan Kapoor Date: Thu, 28 Sep 2023 12:01:28 +0530 Subject: [PATCH 4/4] update CHANGELOG --- protocols/upnp/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/protocols/upnp/CHANGELOG.md b/protocols/upnp/CHANGELOG.md index 13b2f88e853..8ebea5e728e 100644 --- a/protocols/upnp/CHANGELOG.md +++ b/protocols/upnp/CHANGELOG.md @@ -1,5 +1,8 @@ ## 0.1.1 - unreleased +- Fix high CPU usage due to repeated generation of failure events. + See [PR 4569](https://github.com/libp2p/rust-libp2p/pull/4569). + - Fix port mapping protocol used for a UDP multiaddress. See [PR 4542](https://github.com/libp2p/rust-libp2p/pull/4542).