Skip to content

Commit

Permalink
core: Add a total established connection limit (#2137)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Inden <[email protected]>
  • Loading branch information
AgeManning and mxinden authored Jul 30, 2021
1 parent 99c7078 commit 76f1fcb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 0.30.0 [unreleased]

- Add `ConnectionLimit::with_max_established` (see [PR 2137]).

- Add `Keypair::to_protobuf_encoding` (see [PR 2142]).

- Change `PublicKey::into_protobuf_encoding` to `PublicKey::to_protobuf_encoding` (see [PR 2145]).
Expand All @@ -12,6 +14,7 @@

[PR 2145]: https://github.com/libp2p/rust-libp2p/pull/2145
[PR 2142]: https://github.com/libp2p/rust-libp2p/pull/2142
[PR 2137]: https://github.com/libp2p/rust-libp2p/pull/2137/

# 0.29.0 [2021-07-12]

Expand Down
15 changes: 15 additions & 0 deletions core/src/connection/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,9 @@ impl ConnectionCounters {
fn check_max_established(&self, endpoint: &ConnectedPoint)
-> Result<(), ConnectionLimit>
{
// Check total connection limit.
Self::check(self.num_established(), self.limits.max_established_total)?;
// Check incoming/outgoing connection limits
match endpoint {
ConnectedPoint::Dialer { .. } =>
Self::check(self.established_outgoing, self.limits.max_established_outgoing),
Expand Down Expand Up @@ -1031,6 +1034,7 @@ pub struct ConnectionLimits {
max_established_incoming: Option<u32>,
max_established_outgoing: Option<u32>,
max_established_per_peer: Option<u32>,
max_established_total: Option<u32>,
}

impl ConnectionLimits {
Expand Down Expand Up @@ -1058,6 +1062,17 @@ impl ConnectionLimits {
self
}

/// Configures the maximum number of concurrent established connections (both
/// inbound and outbound).
///
/// Note: This should be used in conjunction with
/// [`ConnectionLimits::with_max_established_incoming`] to prevent possible
/// eclipse attacks (all connections being inbound).
pub fn with_max_established(mut self, limit: Option<u32>) -> Self {
self.max_established_total = limit;
self
}

/// Configures the maximum number of concurrent established connections per peer,
/// regardless of direction (incoming or outgoing).
pub fn with_max_established_per_peer(mut self, limit: Option<u32>) -> Self {
Expand Down

0 comments on commit 76f1fcb

Please sign in to comment.