mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-27 08:41:36 +00:00
core: Add a total established connection limit (#2137)
Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
@ -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 {
|
||||
|
Reference in New Issue
Block a user