Skip to content

Commit

Permalink
add high cpu usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
arpankapoor committed Sep 28, 2023
1 parent 359a6d6 commit 66f569f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions examples/ping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
33 changes: 24 additions & 9 deletions examples/ping/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
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.
Expand Down

0 comments on commit 66f569f

Please sign in to comment.