Skip to content

Commit 71af153

Browse files
committed
fix(autonat): close dial-back connections once probes complete
1 parent cb1450d commit 71af153

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

protocols/autonat/src/v1/behaviour.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use libp2p_request_response::{
3939
self as request_response, InboundRequestId, OutboundRequestId, ProtocolSupport, ResponseChannel,
4040
};
4141
use libp2p_swarm::{
42-
ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, THandler, THandlerInEvent,
43-
THandlerOutEvent, ToSwarm,
42+
CloseConnection, ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, THandler,
43+
THandlerInEvent, THandlerOutEvent, ToSwarm,
4444
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm},
4545
};
4646
use web_time::Instant;
@@ -349,6 +349,15 @@ impl Behaviour {
349349
if let Some(event) = self.as_server().on_outbound_connection(&peer, address) {
350350
self.pending_actions
351351
.push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event)));
352+
// This connection was dialed solely to probe the peer's reachability;
353+
// the probe is resolved and the response is sent over the connection
354+
// the peer dialed us on. Close the probe connection so it does not
355+
// linger alongside the peer's primary connection and hold on to
356+
// transport resources.
357+
self.pending_actions.push_back(ToSwarm::CloseConnection {
358+
peer_id: peer,
359+
connection: CloseConnection::One(conn),
360+
});
352361
}
353362
}
354363
ConnectedPoint::Dialer {

protocols/autonat/src/v2/server/behaviour.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use either::Either;
88
use libp2p_core::{Endpoint, Multiaddr, transport::PortUse};
99
use libp2p_identity::PeerId;
1010
use libp2p_swarm::{
11-
ConnectionDenied, ConnectionHandler, ConnectionId, DialFailure, FromSwarm, NetworkBehaviour,
12-
ToSwarm,
11+
CloseConnection, ConnectionDenied, ConnectionHandler, ConnectionId, DialFailure, FromSwarm,
12+
NetworkBehaviour, ToSwarm,
1313
dial_opts::{DialOpts, PeerCondition},
1414
dummy,
1515
};
@@ -102,13 +102,23 @@ where
102102
fn on_connection_handler_event(
103103
&mut self,
104104
peer_id: PeerId,
105-
_connection_id: ConnectionId,
105+
connection_id: ConnectionId,
106106
event: <Handler<R> as ConnectionHandler>::ToBehaviour,
107107
) {
108108
match event {
109-
Either::Left(Either::Left(Ok(_))) => {}
110-
Either::Left(Either::Left(Err(e))) => {
111-
tracing::debug!("dial back error: {e:?}");
109+
Either::Left(Either::Left(result)) => {
110+
if let Err(e) = result {
111+
tracing::debug!("dial back error: {e:?}");
112+
}
113+
// This connection was dialed solely to perform the dial-back, which
114+
// has now run to completion; the outcome reaches the client over the
115+
// connection it dialed us on. Close the dial-back connection so it
116+
// does not linger alongside the client's primary connection and hold
117+
// on to transport resources.
118+
self.pending_events.push_back(ToSwarm::CloseConnection {
119+
peer_id,
120+
connection: CloseConnection::One(connection_id),
121+
});
112122
}
113123
Either::Left(Either::Right(v)) => libp2p_core::util::unreachable(v),
114124
Either::Right(Either::Left(cmd)) => {

0 commit comments

Comments
 (0)