Skip to content

Commit fc8792d

Browse files
authored
transport/manager: Add connection limits for inbound and outbound established connections (#185)
This PR adds connection limits for: - maximum number of inbound established (negotiated) connections - maximum number of outbound established (negotiated) connections - any dial attempts are immediately rejected if we reach capacity for the maximum number of outbound established connections The public API exposes for the connection limits a builder, both on the Litep2pConfig, as well for the limits: ```rust let litep2p_config = Config::default() .with_connection_limits(ConnectionLimitsConfig::default() .max_incoming_connections(Some(3)) .max_outgoing_connections(Some(2))); ``` The connection limits increments and decrements connections identified by their `ConnectionId(usize)`. Regarding memory consumption, we use 2 hash-maps that require `sizeof(usize) * (num_of_inbound + num_of_outbound)` for elements. Where,`num_of_inbound` may be capped to `max_incoming_connections` and `num_of_outbound` to `max_outgoing_connections`. We do not need to introduce a limit on the total number of connections established with a single peer. Litep2p already assumes we can have a maximum of 2 established connections for each peer. We could also limit the number of inbound non negotiated connections, similar to how we handle outbound connections via dial methods. However, each protocol eagerly accepts and negotiates all inbound connections. This is not optimal, because we can save resources (CPU + mem) by rejecting non negotiated connections when we are at inbound capacity. To achieve this, a refactoring needs to move the accepting of inbound connections back into the ownership of the TransportManger, instead of the Transport itself. This PR introduces some changes, to also make it easier for review will look into it as a follow-up: - #186 Part of: - #17 --------- Signed-off-by: Alexandru Vasile <[email protected]>
1 parent a49afe1 commit fc8792d

File tree

10 files changed

+532
-22
lines changed

10 files changed

+532
-22
lines changed

src/config.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ use crate::{
2929
notification, request_response, UserProtocol,
3030
},
3131
transport::{
32-
quic::config::Config as QuicConfig, tcp::config::Config as TcpConfig,
33-
webrtc::config::Config as WebRtcConfig, websocket::config::Config as WebSocketConfig,
34-
MAX_PARALLEL_DIALS,
32+
manager::limits::ConnectionLimitsConfig, quic::config::Config as QuicConfig,
33+
tcp::config::Config as TcpConfig, webrtc::config::Config as WebRtcConfig,
34+
websocket::config::Config as WebSocketConfig, MAX_PARALLEL_DIALS,
3535
},
3636
types::protocol::ProtocolName,
3737
PeerId,
@@ -109,6 +109,9 @@ pub struct ConfigBuilder {
109109

110110
/// Maximum number of parallel dial attempts.
111111
max_parallel_dials: usize,
112+
113+
/// Connection limits config.
114+
connection_limits: ConnectionLimitsConfig,
112115
}
113116

114117
impl Default for ConfigBuilder {
@@ -137,6 +140,7 @@ impl ConfigBuilder {
137140
notification_protocols: HashMap::new(),
138141
request_response_protocols: HashMap::new(),
139142
known_addresses: Vec::new(),
143+
connection_limits: ConnectionLimitsConfig::default(),
140144
}
141145
}
142146

@@ -243,6 +247,12 @@ impl ConfigBuilder {
243247
self
244248
}
245249

250+
/// Set connection limits configuration.
251+
pub fn with_connection_limits(mut self, config: ConnectionLimitsConfig) -> Self {
252+
self.connection_limits = config;
253+
self
254+
}
255+
246256
/// Build [`Litep2pConfig`].
247257
pub fn build(mut self) -> Litep2pConfig {
248258
let keypair = match self.keypair {
@@ -267,6 +277,7 @@ impl ConfigBuilder {
267277
notification_protocols: self.notification_protocols,
268278
request_response_protocols: self.request_response_protocols,
269279
known_addresses: self.known_addresses,
280+
connection_limits: self.connection_limits,
270281
}
271282
}
272283
}
@@ -320,4 +331,7 @@ pub struct Litep2pConfig {
320331

321332
/// Known addresses.
322333
pub(crate) known_addresses: Vec<(PeerId, Vec<Multiaddr>)>,
334+
335+
/// Connection limits config.
336+
pub(crate) connection_limits: ConnectionLimitsConfig,
323337
}

src/error.rs

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
use crate::{
3030
protocol::Direction,
31+
transport::manager::limits::ConnectionLimitsError,
3132
types::{protocol::ProtocolName, ConnectionId, SubstreamId},
3233
PeerId,
3334
};
@@ -118,6 +119,8 @@ pub enum Error {
118119
ChannelClogged,
119120
#[error("Connection doesn't exist: `{0:?}`")]
120121
ConnectionDoesntExist(ConnectionId),
122+
#[error("Exceeded connection limits `{0:?}`")]
123+
ConnectionLimit(ConnectionLimitsError),
121124
}
122125

123126
#[derive(Debug, thiserror::Error)]
@@ -243,6 +246,12 @@ impl From<quinn::ConnectionError> for Error {
243246
}
244247
}
245248

249+
impl From<ConnectionLimitsError> for Error {
250+
fn from(error: ConnectionLimitsError) -> Self {
251+
Error::ConnectionLimit(error)
252+
}
253+
}
254+
246255
#[cfg(test)]
247256
mod tests {
248257
use super::*;

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ impl Litep2p {
143143
supported_transports,
144144
bandwidth_sink.clone(),
145145
litep2p_config.max_parallel_dials,
146+
litep2p_config.connection_limits,
146147
);
147148

148149
// add known addresses to `TransportManager`, if any exist

src/protocol/libp2p/kademlia/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,11 @@ mod tests {
897897

898898
use super::*;
899899
use crate::{
900-
codec::ProtocolCodec, crypto::ed25519::Keypair, transport::manager::TransportManager,
901-
types::protocol::ProtocolName, BandwidthSink,
900+
codec::ProtocolCodec,
901+
crypto::ed25519::Keypair,
902+
transport::manager::{limits::ConnectionLimitsConfig, TransportManager},
903+
types::protocol::ProtocolName,
904+
BandwidthSink,
902905
};
903906
use tokio::sync::mpsc::channel;
904907

@@ -914,6 +917,7 @@ mod tests {
914917
HashSet::new(),
915918
BandwidthSink::new(),
916919
8usize,
920+
ConnectionLimitsConfig::default(),
917921
);
918922

919923
let peer = PeerId::random();

src/protocol/mdns.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,11 @@ impl Mdns {
334334
#[cfg(test)]
335335
mod tests {
336336
use super::*;
337-
use crate::{crypto::ed25519::Keypair, transport::manager::TransportManager, BandwidthSink};
337+
use crate::{
338+
crypto::ed25519::Keypair,
339+
transport::manager::{limits::ConnectionLimitsConfig, TransportManager},
340+
BandwidthSink,
341+
};
338342
use futures::StreamExt;
339343
use multiaddr::Protocol;
340344

@@ -350,6 +354,7 @@ mod tests {
350354
HashSet::new(),
351355
BandwidthSink::new(),
352356
8usize,
357+
ConnectionLimitsConfig::default(),
353358
);
354359

355360
let mdns1 = Mdns::new(
@@ -372,6 +377,7 @@ mod tests {
372377
HashSet::new(),
373378
BandwidthSink::new(),
374379
8usize,
380+
ConnectionLimitsConfig::default(),
375381
);
376382

377383
let mdns2 = Mdns::new(

src/protocol/notification/tests/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
},
3030
InnerTransportEvent, ProtocolCommand, TransportService,
3131
},
32-
transport::manager::TransportManager,
32+
transport::manager::{limits::ConnectionLimitsConfig, TransportManager},
3333
types::protocol::ProtocolName,
3434
BandwidthSink, PeerId,
3535
};
@@ -53,6 +53,7 @@ fn make_notification_protocol() -> (
5353
HashSet::new(),
5454
BandwidthSink::new(),
5555
8usize,
56+
ConnectionLimitsConfig::default(),
5657
);
5758

5859
let peer = PeerId::random();

src/protocol/request_response/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
InnerTransportEvent, TransportService,
3030
},
3131
substream::Substream,
32-
transport::manager::TransportManager,
32+
transport::manager::{limits::ConnectionLimitsConfig, TransportManager},
3333
types::{RequestId, SubstreamId},
3434
BandwidthSink, Error, PeerId, ProtocolName,
3535
};
@@ -51,6 +51,7 @@ fn protocol() -> (
5151
HashSet::new(),
5252
BandwidthSink::new(),
5353
8usize,
54+
ConnectionLimitsConfig::default(),
5455
);
5556

5657
let peer = PeerId::random();

src/transport/manager/limits.rs

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
// Copyright 2024 litep2p developers
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the "Software"),
5+
// to deal in the Software without restriction, including without limitation
6+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
// and/or sell copies of the Software, and to permit persons to whom the
8+
// Software is furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
// DEALINGS IN THE SOFTWARE.
20+
21+
//! Limits for the transport manager.
22+
23+
use crate::types::ConnectionId;
24+
25+
use std::collections::HashSet;
26+
27+
/// Configuration for the connection limits.
28+
#[derive(Debug, Clone, Default)]
29+
pub struct ConnectionLimitsConfig {
30+
/// Maximum number of incoming connections that can be established.
31+
max_incoming_connections: Option<usize>,
32+
/// Maximum number of outgoing connections that can be established.
33+
max_outgoing_connections: Option<usize>,
34+
}
35+
36+
impl ConnectionLimitsConfig {
37+
/// Configures the maximum number of incoming connections that can be established.
38+
pub fn max_incoming_connections(mut self, limit: Option<usize>) -> Self {
39+
self.max_incoming_connections = limit;
40+
self
41+
}
42+
43+
/// Configures the maximum number of outgoing connections that can be established.
44+
pub fn max_outgoing_connections(mut self, limit: Option<usize>) -> Self {
45+
self.max_outgoing_connections = limit;
46+
self
47+
}
48+
}
49+
50+
/// Error type for connection limits.
51+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52+
pub enum ConnectionLimitsError {
53+
/// Maximum number of incoming connections exceeded.
54+
MaxIncomingConnectionsExceeded,
55+
/// Maximum number of outgoing connections exceeded.
56+
MaxOutgoingConnectionsExceeded,
57+
}
58+
59+
/// Connection limits.
60+
#[derive(Debug, Clone)]
61+
pub struct ConnectionLimits {
62+
/// Configuration for the connection limits.
63+
config: ConnectionLimitsConfig,
64+
65+
/// Established incoming connections.
66+
incoming_connections: HashSet<ConnectionId>,
67+
/// Established outgoing connections.
68+
outgoing_connections: HashSet<ConnectionId>,
69+
}
70+
71+
impl ConnectionLimits {
72+
/// Creates a new connection limits instance.
73+
pub fn new(config: ConnectionLimitsConfig) -> Self {
74+
let max_incoming_connections = config.max_incoming_connections.unwrap_or(0);
75+
let max_outgoing_connections = config.max_outgoing_connections.unwrap_or(0);
76+
77+
Self {
78+
config,
79+
incoming_connections: HashSet::with_capacity(max_incoming_connections),
80+
outgoing_connections: HashSet::with_capacity(max_outgoing_connections),
81+
}
82+
}
83+
84+
/// Called when dialing an address.
85+
///
86+
/// Returns the number of outgoing connections permitted to be established.
87+
/// It is guaranteed that at least one connection can be established if the method returns `Ok`.
88+
/// The number of available outgoing connections can influence the maximum parallel dials to a
89+
/// single address.
90+
///
91+
/// If the maximum number of outgoing connections is not set, `Ok(usize::MAX)` is returned.
92+
pub fn on_dial_address(&mut self) -> Result<usize, ConnectionLimitsError> {
93+
if let Some(max_outgoing_connections) = self.config.max_outgoing_connections {
94+
if self.outgoing_connections.len() >= max_outgoing_connections {
95+
return Err(ConnectionLimitsError::MaxOutgoingConnectionsExceeded);
96+
}
97+
98+
return Ok(max_outgoing_connections - self.outgoing_connections.len());
99+
}
100+
101+
Ok(usize::MAX)
102+
}
103+
104+
/// Called when a new connection is established.
105+
pub fn on_connection_established(
106+
&mut self,
107+
connection_id: ConnectionId,
108+
is_listener: bool,
109+
) -> Result<(), ConnectionLimitsError> {
110+
// Check connection limits.
111+
if is_listener {
112+
if let Some(max_incoming_connections) = self.config.max_incoming_connections {
113+
if self.incoming_connections.len() >= max_incoming_connections {
114+
return Err(ConnectionLimitsError::MaxIncomingConnectionsExceeded);
115+
}
116+
}
117+
} else {
118+
if let Some(max_outgoing_connections) = self.config.max_outgoing_connections {
119+
if self.outgoing_connections.len() >= max_outgoing_connections {
120+
return Err(ConnectionLimitsError::MaxOutgoingConnectionsExceeded);
121+
}
122+
}
123+
}
124+
125+
// Keep track of the connection.
126+
if is_listener {
127+
if self.config.max_incoming_connections.is_some() {
128+
self.incoming_connections.insert(connection_id);
129+
}
130+
} else {
131+
if self.config.max_outgoing_connections.is_some() {
132+
self.outgoing_connections.insert(connection_id);
133+
}
134+
}
135+
136+
Ok(())
137+
}
138+
139+
/// Called when a connection is closed.
140+
pub fn on_connection_closed(&mut self, connection_id: ConnectionId) {
141+
self.incoming_connections.remove(&connection_id);
142+
self.outgoing_connections.remove(&connection_id);
143+
}
144+
}
145+
146+
#[cfg(test)]
147+
mod tests {
148+
use super::*;
149+
use crate::types::ConnectionId;
150+
151+
#[test]
152+
fn connection_limits() {
153+
let config = ConnectionLimitsConfig::default()
154+
.max_incoming_connections(Some(3))
155+
.max_outgoing_connections(Some(2));
156+
let mut limits = ConnectionLimits::new(config);
157+
158+
let connection_id_in_1 = ConnectionId::random();
159+
let connection_id_in_2 = ConnectionId::random();
160+
let connection_id_out_1 = ConnectionId::random();
161+
let connection_id_out_2 = ConnectionId::random();
162+
let connection_id_in_3 = ConnectionId::random();
163+
let connection_id_out_3 = ConnectionId::random();
164+
165+
// Establish incoming connection.
166+
assert!(limits.on_connection_established(connection_id_in_1, true).is_ok());
167+
assert_eq!(limits.incoming_connections.len(), 1);
168+
169+
assert!(limits.on_connection_established(connection_id_in_2, true).is_ok());
170+
assert_eq!(limits.incoming_connections.len(), 2);
171+
172+
assert!(limits.on_connection_established(connection_id_in_3, true).is_ok());
173+
assert_eq!(limits.incoming_connections.len(), 3);
174+
175+
assert_eq!(
176+
limits.on_connection_established(ConnectionId::random(), true).unwrap_err(),
177+
ConnectionLimitsError::MaxIncomingConnectionsExceeded
178+
);
179+
assert_eq!(limits.incoming_connections.len(), 3);
180+
181+
// Establish outgoing connection.
182+
assert!(limits.on_connection_established(connection_id_out_1, false).is_ok());
183+
assert_eq!(limits.incoming_connections.len(), 3);
184+
assert_eq!(limits.outgoing_connections.len(), 1);
185+
186+
assert!(limits.on_connection_established(connection_id_out_2, false).is_ok());
187+
assert_eq!(limits.incoming_connections.len(), 3);
188+
assert_eq!(limits.outgoing_connections.len(), 2);
189+
190+
assert_eq!(
191+
limits.on_connection_established(connection_id_out_3, false).unwrap_err(),
192+
ConnectionLimitsError::MaxOutgoingConnectionsExceeded
193+
);
194+
195+
// Close connections with peer a.
196+
limits.on_connection_closed(connection_id_in_1);
197+
assert_eq!(limits.incoming_connections.len(), 2);
198+
assert_eq!(limits.outgoing_connections.len(), 2);
199+
200+
limits.on_connection_closed(connection_id_out_1);
201+
assert_eq!(limits.incoming_connections.len(), 2);
202+
assert_eq!(limits.outgoing_connections.len(), 1);
203+
}
204+
}

0 commit comments

Comments
 (0)