Skip to content

Commit 76f1fcb

Browse files
AgeManningmxinden
andauthored
core: Add a total established connection limit (#2137)
Co-authored-by: Max Inden <[email protected]>
1 parent 99c7078 commit 76f1fcb

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

core/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 0.30.0 [unreleased]
22

3+
- Add `ConnectionLimit::with_max_established` (see [PR 2137]).
4+
35
- Add `Keypair::to_protobuf_encoding` (see [PR 2142]).
46

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

1315
[PR 2145]: https://github.com/libp2p/rust-libp2p/pull/2145
1416
[PR 2142]: https://github.com/libp2p/rust-libp2p/pull/2142
17+
[PR 2137]: https://github.com/libp2p/rust-libp2p/pull/2137/
1518

1619
# 0.29.0 [2021-07-12]
1720

core/src/connection/pool.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,9 @@ impl ConnectionCounters {
987987
fn check_max_established(&self, endpoint: &ConnectedPoint)
988988
-> Result<(), ConnectionLimit>
989989
{
990+
// Check total connection limit.
991+
Self::check(self.num_established(), self.limits.max_established_total)?;
992+
// Check incoming/outgoing connection limits
990993
match endpoint {
991994
ConnectedPoint::Dialer { .. } =>
992995
Self::check(self.established_outgoing, self.limits.max_established_outgoing),
@@ -1031,6 +1034,7 @@ pub struct ConnectionLimits {
10311034
max_established_incoming: Option<u32>,
10321035
max_established_outgoing: Option<u32>,
10331036
max_established_per_peer: Option<u32>,
1037+
max_established_total: Option<u32>,
10341038
}
10351039

10361040
impl ConnectionLimits {
@@ -1058,6 +1062,17 @@ impl ConnectionLimits {
10581062
self
10591063
}
10601064

1065+
/// Configures the maximum number of concurrent established connections (both
1066+
/// inbound and outbound).
1067+
///
1068+
/// Note: This should be used in conjunction with
1069+
/// [`ConnectionLimits::with_max_established_incoming`] to prevent possible
1070+
/// eclipse attacks (all connections being inbound).
1071+
pub fn with_max_established(mut self, limit: Option<u32>) -> Self {
1072+
self.max_established_total = limit;
1073+
self
1074+
}
1075+
10611076
/// Configures the maximum number of concurrent established connections per peer,
10621077
/// regardless of direction (incoming or outgoing).
10631078
pub fn with_max_established_per_peer(mut self, limit: Option<u32>) -> Self {

0 commit comments

Comments
 (0)