Skip to content

Commit

Permalink
feat: network improvements (#141)
Browse files Browse the repository at this point in the history
Description
---
split max connections into outgoing and incoming
limit relay addresses to max 8
added some better logging
  • Loading branch information
SWvheerden authored Nov 8, 2024
1 parent 41aeb10 commit 8c1340e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 89 deletions.
80 changes: 0 additions & 80 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ libp2p = {version = "0.54.1", features = [
]}
log = {version = "0.4.21", features = ["kv"]}
log4rs = "1.3.0"
moka = {version = "0.12.7", features = ["future"]}
num = {version = "0.4.3", features = ["default", "num-bigint", "serde"]}
num-format = "*"
rand = "0.8.0"
serde = "1.0.203"
serde_cbor = "0.11.2"
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ pub async fn server(
config_builder.with_stable_peer(args.stable_peer);
config_builder.with_private_key_folder(args.private_key_folder.clone());
if let Some(max_connections) = args.max_connections {
config_builder.with_max_connections(max_connections);
config_builder.with_max_outgoing_connections(max_connections);
config_builder.with_max_incoming_connections(max_connections);
}
config_builder.with_randomx_enabled(!args.randomx_disabled);
config_builder.with_sha3x_enabled(!args.sha3x_disabled);
Expand Down
15 changes: 11 additions & 4 deletions src/server/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ pub struct Config {
pub p2p_service: p2p::Config,
pub mining_enabled: bool,
pub http_server: http::server::Config,
pub max_connections: Option<u32>,
pub max_incoming_connections: Option<u32>,
pub max_outgoing_connections: Option<u32>,
}

impl Default for Config {
Expand All @@ -36,7 +37,8 @@ impl Default for Config {
p2p_service: p2p::Config::default(),
mining_enabled: true,
http_server: http::server::Config::default(),
max_connections: Some(100),
max_incoming_connections: Some(100),
max_outgoing_connections: Some(100),
}
}
}
Expand Down Expand Up @@ -100,8 +102,13 @@ impl ConfigBuilder {
self
}

pub fn with_max_connections(&mut self, config: u32) -> &mut Self {
self.config.max_connections = Some(config);
pub fn with_max_incoming_connections(&mut self, config: u32) -> &mut Self {
self.config.max_incoming_connections = Some(config);
self
}

pub fn with_max_outgoing_connections(&mut self, config: u32) -> &mut Self {
self.config.max_outgoing_connections = Some(config);
self
}

Expand Down
8 changes: 6 additions & 2 deletions src/server/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ where S: ShareChain
relay_client,
dcutr: dcutr::Behaviour::new(key_pair.public().to_peer_id()),
autonat: autonat::Behaviour::new(key_pair.public().to_peer_id(), Default::default()),
connection_limits: connection_limits::Behaviour::new(ConnectionLimits::default().with_max_established(config.max_connections)),
connection_limits: connection_limits::Behaviour::new(ConnectionLimits::default().with_max_established_incoming(config.max_incoming_connections).with_max_established_outgoing(config.max_outgoing_connections)),
})
})
.map_err(|e| Error::LibP2P(LibP2PError::Behaviour(e.to_string())))?
Expand Down Expand Up @@ -1595,6 +1595,8 @@ where S: ShareChain
lock.select_random_relay();
if let Some(relay) = lock.selected_relay_mut() {
let addresses = relay.addresses.clone();
let mut addresses = relay.addresses.clone();
addresses.truncate(8);

if let Err(err) = self.swarm.dial(
DialOpts::peer_id(relay.peer_id)
Expand Down Expand Up @@ -1733,9 +1735,11 @@ where S: ShareChain
},
event = self.swarm.select_next_some() => {
let timer = Instant::now();
let mut event_string = format!("{:?}", event);
event_string.truncate(300);
self.handle_event(event).await;
if timer.elapsed() > MAX_ACCEPTABLE_NETWORK_EVENT_TIMEOUT {
warn!(target: LOG_TARGET, "Event handling took too long: {:?}", timer.elapsed());
warn!(target: LOG_TARGET, "Event handling took too long: {:?} ({})", timer.elapsed(),event_string);
}
},
_ = publish_peer_info_interval.tick() => {
Expand Down

0 comments on commit 8c1340e

Please sign in to comment.