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

feat(swarm): allow configuration to idle connection timeout #4161

Merged
merged 33 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
073cee1
added idle_timeout and test for swarm builder
startup-dreamer Jul 8, 2023
459cb4d
Allow configuration of idle timeout via `SwarmBuilder`
thomaseizinger Jul 31, 2023
6134c56
Set `idle_connection_timeout` in `Swarm::new_ephemeral` and rewrite p…
thomaseizinger Jul 31, 2023
837a038
Fix deprecation message
thomaseizinger Jul 31, 2023
647e273
Move deprecation attribute to module declaration instead
thomaseizinger Jul 31, 2023
3dcec14
refactor: deprecated keep_alive in transports tls smoke test
startup-dreamer Aug 4, 2023
fdc555b
refactor: deprecated keep_alive in interop-test suite
startup-dreamer Aug 4, 2023
d4eea12
refactor: deprecated keep_alive in transport webrtc example
startup-dreamer Aug 4, 2023
42b9e3b
fix: idle timeout test case and added zero constant in comparison
startup-dreamer Aug 6, 2023
f91d242
refactor: deprecated keep_alive from swarm lib tests
startup-dreamer Aug 6, 2023
5cfd7f5
refactor: deprecated keep_alive from the rest of the tests
startup-dreamer Aug 7, 2023
c6e8211
Merge branch 'master' into feat/idle_connection_timeout
thomaseizinger Sep 12, 2023
73c90f8
Remove `dummy::Behaviour`
thomaseizinger Sep 12, 2023
45b2bd2
Simplify behaviours where possible
thomaseizinger Sep 12, 2023
40388df
Set `idle_connection_timeout` in tutorial
thomaseizinger Sep 12, 2023
7a79097
Update deprecation notice
thomaseizinger Sep 12, 2023
7f492b3
Increase to 1min
thomaseizinger Sep 12, 2023
9c7ce2c
Use via module import
thomaseizinger Sep 12, 2023
1a06749
Refactor usage of `new_test_swarm`
thomaseizinger Sep 12, 2023
d7898ad
Use match-arm guard to reduce indentation
thomaseizinger Sep 12, 2023
f86240e
Fix tests and bug in impl!
thomaseizinger Sep 12, 2023
0b73c5e
Fix missing import in tutorial
thomaseizinger Sep 12, 2023
e338178
Remove deprecated imports
thomaseizinger Sep 12, 2023
1ae9af9
Merge branch 'master' into feat/idle_connection_timeout
thomaseizinger Sep 12, 2023
88f1f1b
Add changelog entry
thomaseizinger Sep 12, 2023
23311ef
Merge branch 'master' into feat/idle_connection_timeout
thomaseizinger Sep 12, 2023
98aa6b0
Merge branch 'master' into feat/idle_connection_timeout
thomaseizinger Sep 13, 2023
3737483
Merge branch 'master' into feat/idle_connection_timeout
thomaseizinger Sep 13, 2023
f566a48
Shutdown `ConnectionHandler` after `max(KeepAlive::Until, idle_timeout)`
thomaseizinger Sep 17, 2023
91705f9
Merge branch 'master' into feat/idle_connection_timeout
thomaseizinger Sep 18, 2023
21d4d17
Update swarm/Cargo.toml
thomaseizinger Sep 18, 2023
4287c50
Update webrtc example to remove keep-alive
thomaseizinger Sep 19, 2023
ba4cb8d
Merge branch 'master' into feat/idle_connection_timeout
thomaseizinger Sep 19, 2023
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: 3 additions & 6 deletions examples/metrics/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ use futures::stream::StreamExt;
use libp2p::core::{upgrade::Version, Multiaddr, Transport};
use libp2p::identity::PeerId;
use libp2p::metrics::{Metrics, Recorder};
use libp2p::swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent};
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
use libp2p::{identify, identity, noise, ping, tcp, yamux};
use log::info;
use prometheus_client::registry::Registry;
use std::error::Error;
use std::thread;
use std::time::Duration;

mod http_service;

Expand All @@ -51,6 +52,7 @@ fn main() -> Result<(), Box<dyn Error>> {
Behaviour::new(local_pub_key),
local_peer_id,
)
.idle_connection_timeout(Duration::from_secs(60))
.build();

swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
Expand Down Expand Up @@ -87,13 +89,9 @@ fn main() -> Result<(), Box<dyn Error>> {
}

/// Our network behaviour.
///
/// For illustrative purposes, this includes the [`keep_alive::Behaviour`]) behaviour so the ping actually happen
/// and can be observed via the metrics.
#[derive(NetworkBehaviour)]
struct Behaviour {
identify: identify::Behaviour,
keep_alive: keep_alive::Behaviour,
ping: ping::Behaviour,
}

Expand All @@ -105,7 +103,6 @@ impl Behaviour {
"/ipfs/0.1.0".into(),
local_pub_key,
)),
keep_alive: keep_alive::Behaviour,
}
}
}
16 changes: 4 additions & 12 deletions examples/ping-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ use futures::prelude::*;
use libp2p::core::upgrade::Version;
use libp2p::{
identity, noise, ping,
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
swarm::{SwarmBuilder, SwarmEvent},
tcp, yamux, Multiaddr, PeerId, Transport,
};
use std::error::Error;
use std::time::Duration;

#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -42,7 +43,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
.boxed();

let mut swarm =
SwarmBuilder::with_async_std_executor(transport, Behaviour::default(), local_peer_id)
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
Expand All @@ -65,13 +67,3 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
}

/// Our network behaviour.
///
/// For illustrative purposes, this includes the [`KeepAlive`](keep_alive::Behaviour) behaviour so a continuous sequence of
/// pings can be observed.
#[derive(NetworkBehaviour, Default)]
struct Behaviour {
keep_alive: keep_alive::Behaviour,
ping: ping::Behaviour,
}
5 changes: 2 additions & 3 deletions examples/rendezvous/src/bin/rzv-discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use libp2p::{
identity,
multiaddr::Protocol,
noise, ping, rendezvous,
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, yamux, Multiaddr, PeerId, Transport,
};
use std::time::Duration;
Expand All @@ -50,10 +50,10 @@ async fn main() {
MyBehaviour {
rendezvous: rendezvous::client::Behaviour::new(key_pair.clone()),
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
keep_alive: keep_alive::Behaviour,
},
PeerId::from(key_pair.public()),
)
.idle_connection_timeout(Duration::from_secs(5))
.build();

swarm.dial(rendezvous_point_address.clone()).unwrap();
Expand Down Expand Up @@ -127,5 +127,4 @@ async fn main() {
struct MyBehaviour {
rendezvous: rendezvous::client::Behaviour,
ping: ping::Behaviour,
keep_alive: keep_alive::Behaviour,
}
5 changes: 2 additions & 3 deletions examples/rendezvous/src/bin/rzv-identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use futures::StreamExt;
use libp2p::{
core::transport::upgrade::Version,
identify, identity, noise, ping, rendezvous,
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, yamux, Multiaddr, PeerId, Transport,
};
use std::time::Duration;
Expand Down Expand Up @@ -50,10 +50,10 @@ async fn main() {
)),
rendezvous: rendezvous::client::Behaviour::new(key_pair.clone()),
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
keep_alive: keep_alive::Behaviour,
},
PeerId::from(key_pair.public()),
)
.idle_connection_timeout(Duration::from_secs(5))
.build();

let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap());
Expand Down Expand Up @@ -133,5 +133,4 @@ struct MyBehaviour {
identify: identify::Behaviour,
rendezvous: rendezvous::client::Behaviour,
ping: ping::Behaviour,
keep_alive: keep_alive::Behaviour,
}
5 changes: 2 additions & 3 deletions examples/rendezvous/src/bin/rzv-register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use futures::StreamExt;
use libp2p::{
core::transport::upgrade::Version,
identity, noise, ping, rendezvous,
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, yamux, Multiaddr, PeerId, Transport,
};
use std::time::Duration;
Expand All @@ -46,10 +46,10 @@ async fn main() {
MyBehaviour {
rendezvous: rendezvous::client::Behaviour::new(key_pair.clone()),
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
keep_alive: keep_alive::Behaviour,
},
PeerId::from(key_pair.public()),
)
.idle_connection_timeout(Duration::from_secs(5))
.build();

// In production the external address should be the publicly facing IP address of the rendezvous point.
Expand Down Expand Up @@ -130,5 +130,4 @@ async fn main() {
struct MyBehaviour {
rendezvous: rendezvous::client::Behaviour,
ping: ping::Behaviour,
keep_alive: keep_alive::Behaviour,
}
5 changes: 2 additions & 3 deletions examples/rendezvous/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use futures::StreamExt;
use libp2p::{
core::transport::upgrade::Version,
identify, identity, noise, ping, rendezvous,
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, yamux, PeerId, Transport,
};
use std::time::Duration;
Expand All @@ -48,10 +48,10 @@ async fn main() {
)),
rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()),
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
keep_alive: keep_alive::Behaviour,
},
PeerId::from(key_pair.public()),
)
.idle_connection_timeout(Duration::from_secs(5))
.build();

let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/62649".parse().unwrap());
Expand Down Expand Up @@ -97,5 +97,4 @@ struct MyBehaviour {
identify: identify::Behaviour,
rendezvous: rendezvous::server::Behaviour,
ping: ping::Behaviour,
keep_alive: keep_alive::Behaviour,
}
9 changes: 4 additions & 5 deletions interop-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::Duration;

use anyhow::{bail, Context, Result};
use futures::{FutureExt, StreamExt};
use libp2p::swarm::{keep_alive, NetworkBehaviour, SwarmEvent};
use libp2p::{identify, identity, ping, Multiaddr, PeerId};
use libp2p::swarm::SwarmEvent;
use libp2p::{identify, identity, ping, swarm::NetworkBehaviour, Multiaddr, PeerId};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -33,8 +33,7 @@ pub async fn run_test(
let mut swarm = swarm_builder(
boxed_transport,
Behaviour {
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
keep_alive: keep_alive::Behaviour,
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(10))),
// Need to include identify until https://github.com/status-im/nim-libp2p/issues/924 is resolved.
identify: identify::Behaviour::new(identify::Config::new(
"/interop-tests".to_owned(),
Expand All @@ -43,6 +42,7 @@ pub async fn run_test(
},
local_peer_id,
)
.idle_connection_timeout(Duration::from_secs(5))
.build();

log::info!("Running ping test: {}", swarm.local_peer_id());
Expand Down Expand Up @@ -242,7 +242,6 @@ impl FromStr for SecProtocol {
#[derive(NetworkBehaviour)]
struct Behaviour {
ping: ping::Behaviour,
keep_alive: keep_alive::Behaviour,
identify: identify::Behaviour,
}

Expand Down
Loading
Loading