core: Add a total established connection limit (#2137)

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Age Manning 2021-07-31 03:22:53 +10:00 committed by GitHub
parent 99c7078bcf
commit 76f1fcbee3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -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]).
@ -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]

View File

@ -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),
@ -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 {
@ -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 {