Skip to content

Commit

Permalink
fix: set idle_connection_timeout in examples
Browse files Browse the repository at this point in the history
Within examples, we often set up a very specific network configuration which may leave connections idle whilst we wait for user input. To ensure that the examples can still showcase something, we need to set an `idle_connection_timeout` on the `Swarm`.

Related: #4877.

Pull-Request: #4887.
  • Loading branch information
snowmead authored Nov 21, 2023
1 parent 5a4a462 commit 3e30c20
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/autonat/src/bin/autonat_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
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(
Expand Down
2 changes: 2 additions & 0 deletions examples/autonat/src/bin/autonat_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -52,6 +53,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
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(
Expand Down
3 changes: 2 additions & 1 deletion examples/dcutr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -105,6 +105,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
)),
dcutr: dcutr::Behaviour::new(keypair.public().to_peer_id()),
})?
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
.build();

swarm
Expand Down
2 changes: 2 additions & 0 deletions examples/distributed-key-value-store/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use libp2p::{
tcp, yamux,
};
use std::error::Error;
use std::time::Duration;
use tracing_subscriber::EnvFilter;

#[async_std::main]
Expand Down Expand Up @@ -65,6 +66,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
)?,
})
})?
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
.build();

swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server));
Expand Down
2 changes: 2 additions & 0 deletions examples/file-sharing/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
///
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/identify/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -44,6 +44,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
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
Expand Down
3 changes: 2 additions & 1 deletion examples/ipfs-private/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -151,6 +151,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
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:?}");
Expand Down

0 comments on commit 3e30c20

Please sign in to comment.