Skip to content

Commit

Permalink
feat(SwarmBuilder): add with_connection_timeout method
Browse files Browse the repository at this point in the history
Small PR to be able to change the `connection_timeout` value given to the `TransportTimeout` when building the `Swarm` with a `SwarmBuilder`.

Pull-Request: #5575.
  • Loading branch information
stormshield-frb authored Dec 14, 2024
1 parent d7b66a7 commit 89af870
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions libp2p/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
## 0.54.2

- Add `with_connection_timeout` on `SwarmBuilder` to allow configuration of the connection_timeout parameter.
See [PR 5575](https://github.com/libp2p/rust-libp2p/pull/5575).

- Deprecate `void` crate.
See [PR 5676](https://github.com/libp2p/rust-libp2p/pull/5676).

- Update default for idle-connection-timeout to 10s.
See [PR 4967](https://github.com/libp2p/rust-libp2p/pull/4967).

Expand Down
19 changes: 11 additions & 8 deletions libp2p/src/builder/phase/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use libp2p_core::Transport;
use std::time::Duration;

use libp2p_core::{transport::timeout::TransportTimeout, Transport};
use libp2p_swarm::Swarm;

#[allow(unused_imports)]
Expand All @@ -9,20 +11,21 @@ pub struct BuildPhase<T, B> {
pub(crate) behaviour: B,
pub(crate) transport: T,
pub(crate) swarm_config: libp2p_swarm::Config,
pub(crate) connection_timeout: Duration,
}

const CONNECTION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);

impl<Provider, T: AuthenticatedMultiplexedTransport, B: libp2p_swarm::NetworkBehaviour>
SwarmBuilder<Provider, BuildPhase<T, B>>
{
/// Timeout of the [`TransportTimeout`] wrapping the transport.
pub fn with_connection_timeout(mut self, connection_timeout: Duration) -> Self {
self.phase.connection_timeout = connection_timeout;
self
}

pub fn build(self) -> Swarm<B> {
Swarm::new(
libp2p_core::transport::timeout::TransportTimeout::new(
self.phase.transport,
CONNECTION_TIMEOUT,
)
.boxed(),
TransportTimeout::new(self.phase.transport, self.phase.connection_timeout).boxed(),
self.phase.behaviour,
self.keypair.public().to_peer_id(),
self.phase.swarm_config,
Expand Down
4 changes: 4 additions & 0 deletions libp2p/src/builder/phase/swarm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[allow(unused_imports)]
use super::*;

#[allow(unused)] // used below but due to feature flag combinations, clippy gives an unnecessary warning.
const DEFAULT_CONNECTION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);

#[allow(dead_code)]
pub struct SwarmPhase<T, B> {
pub(crate) behaviour: B,
Expand All @@ -20,6 +23,7 @@ macro_rules! impl_with_swarm_config {
behaviour: self.phase.behaviour,
transport: self.phase.transport,
swarm_config: constructor($config),
connection_timeout: DEFAULT_CONNECTION_TIMEOUT,
},
keypair: self.keypair,
phantom: std::marker::PhantomData,
Expand Down

0 comments on commit 89af870

Please sign in to comment.