Skip to content

Commit f0a1830

Browse files
committed
feat: make ping config unconfigurable, use default ping config instead
1 parent a1ca920 commit f0a1830

3 files changed

Lines changed: 17 additions & 48 deletions

File tree

crates/charon-p2p/src/behaviours/pluto.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
//! Pluto behaviour.
22
3-
use std::time::Duration;
4-
53
use libp2p::{identify, identity::Keypair, ping, relay, swarm::NetworkBehaviour};
64

7-
use crate::gater::ConnGater;
5+
use crate::{config::default_ping_config, gater::ConnGater};
86

97
/// Pluto network behaviour.
108
#[derive(NetworkBehaviour)]
@@ -36,17 +34,13 @@ impl PlutoBehaviour {
3634
pub struct PlutoBehaviourBuilder {
3735
gater: Option<ConnGater>,
3836
identify_protocol: String,
39-
ping_interval: Duration,
40-
ping_timeout: Duration,
4137
}
4238

4339
impl Default for PlutoBehaviourBuilder {
4440
fn default() -> Self {
4541
Self {
4642
gater: None,
4743
identify_protocol: "/pluto/1.0.0-alpha".into(),
48-
ping_interval: Duration::from_secs(1),
49-
ping_timeout: Duration::from_secs(2),
5044
}
5145
}
5246
}
@@ -69,18 +63,6 @@ impl PlutoBehaviourBuilder {
6963
self
7064
}
7165

72-
/// Sets the ping interval.
73-
pub fn with_ping_interval(mut self, interval: Duration) -> Self {
74-
self.ping_interval = interval;
75-
self
76-
}
77-
78-
/// Sets the ping timeout.
79-
pub fn with_ping_timeout(mut self, timeout: Duration) -> Self {
80-
self.ping_timeout = timeout;
81-
self
82-
}
83-
8466
/// Builds the [`PlutoBehaviour`] with the provided keypair and relay
8567
/// client.
8668
pub fn build(self, key: &Keypair, relay_client: relay::client::Behaviour) -> PlutoBehaviour {
@@ -93,11 +75,7 @@ impl PlutoBehaviourBuilder {
9375
self.identify_protocol,
9476
key.public(),
9577
)),
96-
ping: ping::Behaviour::new(
97-
ping::Config::new()
98-
.with_interval(self.ping_interval)
99-
.with_timeout(self.ping_timeout),
100-
),
78+
ping: ping::Behaviour::new(default_ping_config()),
10179
}
10280
}
10381
}

crates/charon-p2p/src/config.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
use std::{
44
net::{IpAddr, SocketAddr},
55
str::FromStr,
6+
time::Duration,
67
};
78

8-
use libp2p::{Multiaddr, multiaddr};
9+
use libp2p::{Multiaddr, multiaddr, ping};
910

1011
/// P2P configuration error.
1112
#[derive(Debug, thiserror::Error)]
@@ -161,6 +162,18 @@ impl P2PConfigBuilder {
161162
}
162163
}
163164

165+
/// The default ping interval.
166+
pub const DEFAULT_PING_INTERVAL: Duration = Duration::from_secs(1);
167+
/// The default ping timeout.
168+
pub const DEFAULT_PING_TIMEOUT: Duration = Duration::from_secs(5);
169+
170+
/// Returns the default ping configuration.
171+
pub fn default_ping_config() -> ping::Config {
172+
ping::Config::new()
173+
.with_interval(DEFAULT_PING_INTERVAL)
174+
.with_timeout(DEFAULT_PING_TIMEOUT)
175+
}
176+
164177
/// Resolves a TCP address string to a SocketAddr, ensuring the IP is specified.
165178
fn resolve_listen_tcp_addr(addr: impl AsRef<str>) -> Result<SocketAddr> {
166179
let socket_addr: SocketAddr = addr

crates/relay-server/src/behaviour.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#![allow(missing_docs)] // we need to allow missing docs for the derive macro
22
//! Relay server behaviour.
33
4-
use std::time::Duration;
5-
64
use libp2p::{identify, identity::Keypair, ping, relay, swarm::NetworkBehaviour};
75

86
use charon_p2p::gater::ConnGater;
@@ -36,8 +34,6 @@ impl RelayServerBehaviour {
3634
pub struct RelayServerBehaviourBuilder {
3735
gater: Option<ConnGater>,
3836
identify_protocol: String,
39-
ping_interval: Duration,
40-
ping_timeout: Duration,
4137
relay_config: Option<relay::Config>,
4238
}
4339

@@ -46,8 +42,6 @@ impl Default for RelayServerBehaviourBuilder {
4642
Self {
4743
gater: None,
4844
identify_protocol: "/pluto/relay/1.0.0-alpha".into(),
49-
ping_interval: Duration::from_secs(1),
50-
ping_timeout: Duration::from_secs(2),
5145
relay_config: None,
5246
}
5347
}
@@ -71,18 +65,6 @@ impl RelayServerBehaviourBuilder {
7165
self
7266
}
7367

74-
/// Sets the ping interval.
75-
pub fn with_ping_interval(mut self, interval: Duration) -> Self {
76-
self.ping_interval = interval;
77-
self
78-
}
79-
80-
/// Sets the ping timeout.
81-
pub fn with_ping_timeout(mut self, timeout: Duration) -> Self {
82-
self.ping_timeout = timeout;
83-
self
84-
}
85-
8668
/// Sets the relay server configuration.
8769
pub fn with_relay_config(mut self, config: relay::Config) -> Self {
8870
self.relay_config = Some(config);
@@ -100,11 +82,7 @@ impl RelayServerBehaviourBuilder {
10082
self.identify_protocol,
10183
key.public(),
10284
)),
103-
ping: ping::Behaviour::new(
104-
ping::Config::new()
105-
.with_interval(self.ping_interval)
106-
.with_timeout(self.ping_timeout),
107-
),
85+
ping: ping::Behaviour::new(charon_p2p::config::default_ping_config()),
10886
gater: self
10987
.gater
11088
.unwrap_or_else(|| ConnGater::new_conn_gater(vec![], vec![])),

0 commit comments

Comments
 (0)