Skip to content

Commit

Permalink
Change established connection limits algorithm for DSN. (#1276)
Browse files Browse the repository at this point in the history
* Limit pending connections number.

* networking: Change connection limits algorithm.

(cherry picked from commit 20b0067)
  • Loading branch information
shamil-gadelshin authored and nazar-pc committed Mar 22, 2023
1 parent e1b3427 commit c0dbb17
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 59 deletions.
6 changes: 3 additions & 3 deletions crates/subspace-networking/src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ where
let connection_limits = ConnectionLimits::default()
.with_max_established_per_peer(SWARM_MAX_ESTABLISHED_CONNECTIONS_PER_PEER)
.with_max_pending_incoming(Some(max_pending_incoming_connections))
.with_max_pending_outgoing(Some(max_pending_outgoing_connections));
.with_max_pending_outgoing(Some(max_pending_outgoing_connections))
.with_max_established_incoming(Some(max_established_incoming_connections))
.with_max_established_outgoing(Some(max_established_outgoing_connections));

debug!(?connection_limits, "DSN connection limits set.");

Expand Down Expand Up @@ -446,8 +448,6 @@ where
next_random_query_interval: initial_random_query_interval,
networking_parameters_registry,
reserved_peers: convert_multiaddresses(reserved_peers).into_iter().collect(),
max_established_incoming_connections,
max_established_outgoing_connections,
target_connections,
temporary_bans,
metrics,
Expand Down
57 changes: 1 addition & 56 deletions crates/subspace-networking/src/node_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ where
networking_parameters_registry: Box<dyn NetworkingParametersRegistry>,
/// Defines set of peers with a permanent connection (and reconnection if necessary).
reserved_peers: HashMap<PeerId, Multiaddr>,
/// Incoming swarm connection limit.
max_established_incoming_connections: u32,
/// Outgoing swarm connection limit.
max_established_outgoing_connections: u32,
/// Defines target total (in and out) connection number that should be maintained.
target_connections: u32,
/// Temporarily banned peers.
Expand All @@ -125,8 +121,6 @@ where
pub(crate) next_random_query_interval: Duration,
pub(crate) networking_parameters_registry: Box<dyn NetworkingParametersRegistry>,
pub(crate) reserved_peers: HashMap<PeerId, Multiaddr>,
pub(crate) max_established_incoming_connections: u32,
pub(crate) max_established_outgoing_connections: u32,
pub(crate) target_connections: u32,
pub(crate) temporary_bans: Arc<Mutex<TemporaryBans>>,
pub(crate) metrics: Option<Metrics>,
Expand All @@ -145,8 +139,6 @@ where
next_random_query_interval,
networking_parameters_registry,
reserved_peers,
max_established_incoming_connections,
max_established_outgoing_connections,
target_connections,
temporary_bans,
metrics,
Expand All @@ -167,8 +159,6 @@ where
peer_dialing_timeout: Box::pin(tokio::time::sleep(Duration::from_secs(0)).fuse()),
networking_parameters_registry,
reserved_peers,
max_established_incoming_connections,
max_established_outgoing_connections,
target_connections,
temporary_bans,
metrics,
Expand Down Expand Up @@ -373,7 +363,7 @@ where

// TODO: Workaround for https://github.com/libp2p/rust-libp2p/discussions/3418
self.established_connections
.entry((peer_id, endpoint.clone()))
.entry((peer_id, endpoint))
.and_modify(|entry| {
*entry += 1;
})
Expand All @@ -395,51 +385,6 @@ where
warn!(%error, "Failed to expand regular concurrent tasks");
}
}

let (in_connections_number, out_connections_number) = {
let network_info = self.swarm.network_info();
let connections = network_info.connection_counters();

(
connections.num_established_incoming(),
connections.num_established_outgoing(),
)
};

match endpoint {
// In connections
ConnectedPoint::Listener { .. } => {
// check connections limit for non-reserved peers
if !is_reserved_peer
&& in_connections_number > self.max_established_incoming_connections
{
debug!(
%peer_id,
"Incoming connections limit exceeded. Disconnecting in-peer ..."
);
// Error here means: "peer was already disconnected"
let _ = self.swarm.disconnect_peer_id(peer_id);
}
}
// Out connections
ConnectedPoint::Dialer { address, .. } => {
self.networking_parameters_registry
.add_known_peer(peer_id, vec![address])
.await;

// check connections limit for non-reserved peers
if !is_reserved_peer
&& out_connections_number > self.max_established_outgoing_connections
{
debug!(
%peer_id,
"Outgoing connections limit exceeded. Disconnecting out-peer ..."
);
// Error here means: "peer was already disconnected"
let _ = self.swarm.disconnect_peer_id(peer_id);
}
}
}
}
SwarmEvent::ConnectionClosed {
peer_id,
Expand Down

0 comments on commit c0dbb17

Please sign in to comment.