aboutsummaryrefslogtreecommitdiffstats
path: root/src/transport/libp2p/relays.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-10-31 13:39:52 +0100
committertv <tv@krebsco.de>2026-10-31 13:39:52 +0100
commit4ea5867047d869ea6a33efd6a47a2adc2340114e (patch)
treeeff2ab349a939309799298bad14187cb277bb5e0 /src/transport/libp2p/relays.rs
parent81be65604faadf207ff5359dfe95bc6a0865bc8d (diff)
libp2p: upgrade after a dial under way
Diffstat (limited to 'src/transport/libp2p/relays.rs')
-rw-r--r--src/transport/libp2p/relays.rs28
1 files changed, 19 insertions, 9 deletions
diff --git a/src/transport/libp2p/relays.rs b/src/transport/libp2p/relays.rs
index 3463408..dca031c 100644
--- a/src/transport/libp2p/relays.rs
+++ b/src/transport/libp2p/relays.rs
@@ -120,15 +120,17 @@ pub(super) fn pick_relays(
/// too (the member list, identify, mDNS), since the first connection, made through a
/// relay (a relayed seed), would otherwise be kept for good: a peer connected isn't
/// dialled again, and hole punching needs both sides to listen. The direct connection,
-/// once open, wins over the relayed one (`Book::duplicates`).
+/// once open, wins over the relayed one (`Book::duplicates`). A dial refused because
+/// another to the peer is under way (hole punching's, say) doesn't count as a try: the
+/// next comes when that one fails (`driver`), not `UPGRADE_RETRY` later.
pub(super) fn upgrade(
swarm: &mut Swarm<Behaviour>,
book: &Mutex<Book>,
peer: PeerId,
local: PeerId,
) {
- let direct: Vec<Multiaddr> = {
- let mut book = lock(book);
+ let (direct, now, wait) = {
+ let book = lock(book);
if book.connection(&peer) != Some(Connection::Relayed) {
return;
}
@@ -152,17 +154,25 @@ pub(super) fn upgrade(
if now < due {
return;
}
- book.upgrades
- .insert(peer, (now + wait, (wait * 2).min(RELAY_PAUSE)));
- direct
+ (direct, now, wait)
};
- debug!(peer = %Addressed(SCHEME, &peer), addresses = ?direct, "Connected only through a relay; dialling directly");
- let _ = swarm.dial(
+ let dialled = swarm.dial(
DialOpts::peer_id(peer)
- .addresses(direct)
+ .addresses(direct.clone())
.condition(PeerCondition::NotDialing)
.build(),
);
+ match dialled {
+ Ok(()) => {
+ debug!(peer = %Addressed(SCHEME, &peer), addresses = ?direct, "Connected only through a relay; dialling directly");
+ lock(book)
+ .upgrades
+ .insert(peer, (now + wait, (wait * 2).min(RELAY_PAUSE)));
+ }
+ Err(error) => {
+ debug!(peer = %Addressed(SCHEME, &peer), error = %error, "Connected only through a relay; can't dial directly yet");
+ }
+ }
}
/// Tries hole punching again with the peers it failed with that are due (`punching`).