From 3cf5dc22ed1b4fc03f53c9b4d2609fb37b553236 Mon Sep 17 00:00:00 2001 From: Snowmead Date: Fri, 17 Nov 2023 13:27:36 -0500 Subject: [PATCH 1/4] add connection timeout to swarm configs in example --- examples/autonat/src/bin/autonat_client.rs | 1 + examples/autonat/src/bin/autonat_server.rs | 2 ++ examples/dcutr/src/main.rs | 3 ++- examples/distributed-key-value-store/src/main.rs | 2 ++ examples/file-sharing/src/network.rs | 2 ++ examples/identify/src/main.rs | 3 ++- examples/ipfs-private/src/main.rs | 3 ++- examples/relay-server/src/main.rs | 3 ++- examples/upnp/src/main.rs | 3 ++- 9 files changed, 17 insertions(+), 5 deletions(-) diff --git a/examples/autonat/src/bin/autonat_client.rs b/examples/autonat/src/bin/autonat_client.rs index b071e717731..3fb25aa6222 100644 --- a/examples/autonat/src/bin/autonat_client.rs +++ b/examples/autonat/src/bin/autonat_client.rs @@ -60,6 +60,7 @@ async fn main() -> Result<(), Box> { yamux::Config::default, )? .with_behaviour(|key| Behaviour::new(key.public()))? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); swarm.listen_on( diff --git a/examples/autonat/src/bin/autonat_server.rs b/examples/autonat/src/bin/autonat_server.rs index d1c0c005861..44a53f0d17f 100644 --- a/examples/autonat/src/bin/autonat_server.rs +++ b/examples/autonat/src/bin/autonat_server.rs @@ -27,6 +27,7 @@ use libp2p::swarm::{NetworkBehaviour, SwarmEvent}; use libp2p::{autonat, identify, identity, noise, tcp, yamux}; use std::error::Error; use std::net::Ipv4Addr; +use std::time::Duration; use tracing_subscriber::EnvFilter; #[derive(Debug, Parser)] @@ -52,6 +53,7 @@ async fn main() -> Result<(), Box> { yamux::Config::default, )? .with_behaviour(|key| Behaviour::new(key.public()))? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); swarm.listen_on( diff --git a/examples/dcutr/src/main.rs b/examples/dcutr/src/main.rs index 91beaa02c67..51df670f8a7 100644 --- a/examples/dcutr/src/main.rs +++ b/examples/dcutr/src/main.rs @@ -28,8 +28,8 @@ use libp2p::{ swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, PeerId, }; -use std::error::Error; use std::str::FromStr; +use std::{error::Error, time::Duration}; use tracing_subscriber::EnvFilter; #[derive(Debug, Parser)] @@ -105,6 +105,7 @@ async fn main() -> Result<(), Box> { )), dcutr: dcutr::Behaviour::new(keypair.public().to_peer_id()), })? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); swarm diff --git a/examples/distributed-key-value-store/src/main.rs b/examples/distributed-key-value-store/src/main.rs index 1843520838b..404333f3d20 100644 --- a/examples/distributed-key-value-store/src/main.rs +++ b/examples/distributed-key-value-store/src/main.rs @@ -31,6 +31,7 @@ use libp2p::{ tcp, yamux, }; use std::error::Error; +use std::time::Duration; use tracing_subscriber::EnvFilter; #[async_std::main] @@ -65,6 +66,7 @@ async fn main() -> Result<(), Box> { )?, }) })? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server)); diff --git a/examples/file-sharing/src/network.rs b/examples/file-sharing/src/network.rs index ad5418193a4..59625fc39ea 100644 --- a/examples/file-sharing/src/network.rs +++ b/examples/file-sharing/src/network.rs @@ -15,6 +15,7 @@ use libp2p::StreamProtocol; use serde::{Deserialize, Serialize}; use std::collections::{hash_map, HashMap, HashSet}; use std::error::Error; +use std::time::Duration; /// Creates the network components, namely: /// @@ -58,6 +59,7 @@ pub(crate) async fn new( request_response::Config::default(), ), })? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); swarm diff --git a/examples/identify/src/main.rs b/examples/identify/src/main.rs index 3c40addbcf8..916317a5a43 100644 --- a/examples/identify/src/main.rs +++ b/examples/identify/src/main.rs @@ -22,7 +22,7 @@ use futures::StreamExt; use libp2p::{core::multiaddr::Multiaddr, identify, noise, swarm::SwarmEvent, tcp, yamux}; -use std::error::Error; +use std::{error::Error, time::Duration}; use tracing_subscriber::EnvFilter; #[async_std::main] @@ -44,6 +44,7 @@ async fn main() -> Result<(), Box> { key.public(), )) })? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); // Tell the swarm to listen on all interfaces and a random, OS-assigned diff --git a/examples/ipfs-private/src/main.rs b/examples/ipfs-private/src/main.rs index 12bd985cdf0..a57bfd465e0 100644 --- a/examples/ipfs-private/src/main.rs +++ b/examples/ipfs-private/src/main.rs @@ -31,7 +31,7 @@ use libp2p::{ swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Multiaddr, Transport, }; -use std::{env, error::Error, fs, path::Path, str::FromStr}; +use std::{env, error::Error, fs, path::Path, str::FromStr, time::Duration}; use tokio::{io, io::AsyncBufReadExt, select}; use tracing_subscriber::EnvFilter; @@ -151,6 +151,7 @@ async fn main() -> Result<(), Box> { ping: ping::Behaviour::new(ping::Config::new()), }) })? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); println!("Subscribing to {gossipsub_topic:?}"); diff --git a/examples/relay-server/src/main.rs b/examples/relay-server/src/main.rs index bf5817454f8..a46d8459908 100644 --- a/examples/relay-server/src/main.rs +++ b/examples/relay-server/src/main.rs @@ -31,8 +31,8 @@ use libp2p::{ swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, }; -use std::error::Error; use std::net::{Ipv4Addr, Ipv6Addr}; +use std::{error::Error, time::Duration}; use tracing_subscriber::EnvFilter; fn main() -> Result<(), Box> { @@ -61,6 +61,7 @@ fn main() -> Result<(), Box> { key.public(), )), })? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); // Listen on all interfaces diff --git a/examples/upnp/src/main.rs b/examples/upnp/src/main.rs index fd0764990d1..4b02c320388 100644 --- a/examples/upnp/src/main.rs +++ b/examples/upnp/src/main.rs @@ -22,7 +22,7 @@ use futures::prelude::*; use libp2p::{noise, swarm::SwarmEvent, upnp, yamux, Multiaddr}; -use std::error::Error; +use std::{error::Error, time::Duration}; use tracing_subscriber::EnvFilter; #[tokio::main] @@ -39,6 +39,7 @@ async fn main() -> Result<(), Box> { yamux::Config::default, )? .with_behaviour(|_| upnp::tokio::Behaviour::default())? + .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); // Tell the swarm to listen on all interfaces and a random, OS-assigned From 0d18b1de4c9611e0bf562983faa056a550bc96ba Mon Sep 17 00:00:00 2001 From: Snowmead Date: Fri, 17 Nov 2023 13:32:49 -0500 Subject: [PATCH 2/4] trigger check From 23dd1be90f77a73cf4749e8c0b44f61f3202e329 Mon Sep 17 00:00:00 2001 From: Snowmead Date: Mon, 20 Nov 2023 09:47:55 -0500 Subject: [PATCH 3/4] remove unecessary connection timeout on upnp and relay server --- examples/relay-server/src/main.rs | 1 - examples/upnp/src/main.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/examples/relay-server/src/main.rs b/examples/relay-server/src/main.rs index a46d8459908..50c79f1d4b9 100644 --- a/examples/relay-server/src/main.rs +++ b/examples/relay-server/src/main.rs @@ -61,7 +61,6 @@ fn main() -> Result<(), Box> { key.public(), )), })? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); // Listen on all interfaces diff --git a/examples/upnp/src/main.rs b/examples/upnp/src/main.rs index 4b02c320388..44247e05622 100644 --- a/examples/upnp/src/main.rs +++ b/examples/upnp/src/main.rs @@ -39,7 +39,6 @@ async fn main() -> Result<(), Box> { yamux::Config::default, )? .with_behaviour(|_| upnp::tokio::Behaviour::default())? - .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) .build(); // Tell the swarm to listen on all interfaces and a random, OS-assigned From 4b2b226bd5b4dc20f727aa3873ae94ca38f24e44 Mon Sep 17 00:00:00 2001 From: Snowmead Date: Mon, 20 Nov 2023 09:48:57 -0500 Subject: [PATCH 4/4] remove unused imports --- examples/relay-server/src/main.rs | 2 +- examples/upnp/src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/relay-server/src/main.rs b/examples/relay-server/src/main.rs index 50c79f1d4b9..bf5817454f8 100644 --- a/examples/relay-server/src/main.rs +++ b/examples/relay-server/src/main.rs @@ -31,8 +31,8 @@ use libp2p::{ swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, }; +use std::error::Error; use std::net::{Ipv4Addr, Ipv6Addr}; -use std::{error::Error, time::Duration}; use tracing_subscriber::EnvFilter; fn main() -> Result<(), Box> { diff --git a/examples/upnp/src/main.rs b/examples/upnp/src/main.rs index 44247e05622..fd0764990d1 100644 --- a/examples/upnp/src/main.rs +++ b/examples/upnp/src/main.rs @@ -22,7 +22,7 @@ use futures::prelude::*; use libp2p::{noise, swarm::SwarmEvent, upnp, yamux, Multiaddr}; -use std::{error::Error, time::Duration}; +use std::error::Error; use tracing_subscriber::EnvFilter; #[tokio::main]