mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-12 09:31:20 +00:00
feat(swarm): allow configuration to idle connection timeout
Previously, a connection would be shut down immediately as soon as its `ConnectionHandler` reports `KeepAlive::No`. As we have gained experience with libp2p, it turned out that this isn't ideal. For one, tests often need to keep connections alive longer than the configured protocols require. Plus, some usecases require connections to be kept alive in general. Both of these needs are currently served by the `keep_alive::Behaviour`. That one does essentially nothing other than statically returning `KeepAlive::Yes` from its `ConnectionHandler`. It makes much more sense to deprecate `keep_alive::Behaviour` and instead allow users to globally configure an `idle_conncetion_timeout` on the `Swarm`. This timeout comes into effect once a `ConnectionHandler` reports `KeepAlive::No`. To start with, this timeout is 0. Together with https://github.com/libp2p/rust-libp2p/issues/3844, this will allow us to move towards a much more aggressive closing of idle connections, together with a more ergonomic way of opting out of this behaviour. Fixes #4121. Pull-Request: #4161.
This commit is contained in:
@ -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;
|
||||
@ -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();
|
||||
@ -127,5 +127,4 @@ async fn main() {
|
||||
struct MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
keep_alive: keep_alive::Behaviour,
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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());
|
||||
@ -133,5 +133,4 @@ struct MyBehaviour {
|
||||
identify: identify::Behaviour,
|
||||
rendezvous: rendezvous::client::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
keep_alive: keep_alive::Behaviour,
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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.
|
||||
@ -130,5 +130,4 @@ async fn main() {
|
||||
struct MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
keep_alive: keep_alive::Behaviour,
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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());
|
||||
@ -97,5 +97,4 @@ struct MyBehaviour {
|
||||
identify: identify::Behaviour,
|
||||
rendezvous: rendezvous::server::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
keep_alive: keep_alive::Behaviour,
|
||||
}
|
||||
|
Reference in New Issue
Block a user