mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 09:41:34 +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:
@ -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;
|
||||
|
||||
@ -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()?)?;
|
||||
@ -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,
|
||||
}
|
||||
|
||||
@ -105,7 +103,6 @@ impl Behaviour {
|
||||
"/ipfs/0.1.0".into(),
|
||||
local_pub_key,
|
||||
)),
|
||||
keep_alive: keep_alive::Behaviour,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user