Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Next Next commit
Limit number of incoming connections
  • Loading branch information
sorpaas committed Jul 20, 2018
commit 6a8dae0c31fd17b7d24ce67d23d6f1c7f679d892
37 changes: 29 additions & 8 deletions substrate/network-libp2p/src/network_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub struct NetworkState {
min_peers: u32,
/// `max_peers` taken from the configuration.
max_peers: u32,
/// `max_incoming_peers` calculated as `max_peers / max_incoming_peers_factor` from the configuration.
max_incoming_peers: u32,

/// If true, only reserved peers can connect.
reserved_only: atomic::AtomicBool,
Expand Down Expand Up @@ -169,6 +171,7 @@ impl NetworkState {
peerstore,
min_peers: config.min_peers,
max_peers: config.max_peers,
max_incoming_peers: config.max_peers / config.max_incoming_peers_factor,
connections: RwLock::new(Connections {
peer_by_nodeid: FnvHashMap::with_capacity_and_hasher(expected_max_peers, Default::default()),
info_by_peer: FnvHashMap::with_capacity_and_hasher(expected_max_peers, Default::default()),
Expand Down Expand Up @@ -433,7 +436,7 @@ impl NetworkState {
/// Returns the number of open and pending connections with
/// custom protocols.
pub fn num_open_custom_connections(&self) -> u32 {
num_open_custom_connections(&self.connections.read())
num_open_custom_connections(&self.connections.read()).total
}

/// Returns the number of new outgoing custom connections to peers to
Expand Down Expand Up @@ -520,7 +523,7 @@ impl NetworkState {
/// You must pass an `UnboundedSender` which will be used by the `send`
/// method. Actually sending the data is not covered by this code.
///
/// The various methods of the `NetworkState` that close a connection do
/// The various methods of the `NetworkState` that close a connection do
/// so by dropping this sender.
pub fn custom_proto(
&self,
Expand Down Expand Up @@ -548,7 +551,8 @@ impl NetworkState {
let node_is_reserved = self.reserved_peers.read().contains(&infos.id);
if !node_is_reserved {
if self.reserved_only.load(atomic::Ordering::Relaxed) ||
num_open_connections >= self.max_peers
num_open_connections.total >= self.max_peers ||
num_open_connections.incoming >= self.max_incoming_peers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is also reached when we dial. You should use endpoint to determine whether we are dialing or listening.

{
debug!(target: "sub-libp2p", "Refusing node {:?} because we \
reached the max number of peers", node_id);
Expand Down Expand Up @@ -734,10 +738,17 @@ fn is_peer_disabled(
}
}

struct OpenCustomConnectionsNumbers {
/// Total number of open and pending connections.
pub total: u32,
/// Incoming number of open and pending connections.
pub incoming: u32,
}

/// Returns the number of open and pending connections with
/// custom protocols.
fn num_open_custom_connections(connections: &Connections) -> u32 {
connections
fn num_open_custom_connections(connections: &Connections) -> OpenCustomConnectionsNumbers {
let filtered = connections
.info_by_peer
.values()
.filter(|info|
Expand All @@ -747,8 +758,18 @@ fn num_open_custom_connections(connections: &Connections) -> u32 {
_ => false
}
)
)
.count() as u32
);

let mut total: u32 = 0;
let mut incoming: u32 = 0;
for info in filtered {
total += 1;
if !info.originated {
incoming += 1;
}
}

OpenCustomConnectionsNumbers { total, incoming }
}

/// Parses an address of the form `/ip4/x.x.x.x/tcp/x/p2p/xxxxxx`, and adds it
Expand All @@ -774,7 +795,7 @@ fn parse_and_add_to_peerstore(addr_str: &str, peerstore: &PeersStorage)
.peer_or_create(&peer_id)
.add_addr(addr, Duration::from_secs(100000 * 365 * 24 * 3600)),
}

Ok(peer_id)
}

Expand Down
3 changes: 3 additions & 0 deletions substrate/network-libp2p/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ pub struct NetworkConfiguration {
pub min_peers: u32,
/// Maximum allowed number of peers
pub max_peers: u32,
/// Maximum incoming peers factor. The maximum allowed number of incoming peers is calculated as `max_peers / max_incoming_peers_factor`.
pub max_incoming_peers_factor: u32,
/// Maximum handshakes
pub max_handshakes: u32,
/// Reserved protocols. Peers with <key> protocol get additional <value> connection slots.
Expand Down Expand Up @@ -188,6 +190,7 @@ impl NetworkConfiguration {
use_secret: None,
min_peers: 25,
max_peers: 50,
max_incoming_peers_factor: 3,
max_handshakes: 64,
reserved_protocols: HashMap::new(),
ip_filter: IpFilter::default(),
Expand Down