aboutsummaryrefslogtreecommitdiffstats
path: root/src/transport
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport')
-rw-r--r--src/transport/libp2p/driver.rs4
-rw-r--r--src/transport/libp2p/relays.rs28
2 files changed, 23 insertions, 9 deletions
diff --git a/src/transport/libp2p/driver.rs b/src/transport/libp2p/driver.rs
index efcf109..37eae35 100644
--- a/src/transport/libp2p/driver.rs
+++ b/src/transport/libp2p/driver.rs
@@ -246,6 +246,10 @@ pub(super) async fn run(
}
find_on_dht(&mut swarm, peer);
}
+ // A dial that kept ours from going (`upgrade`) is over.
+ if let Some(peer) = peer_id {
+ upgrade(&mut swarm, &book, peer, relaying.local);
+ }
}
SwarmEvent::NewListenAddr { address, .. } => {
info!(address = %address, "Listening");
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`).