From 400c4e845998101b94780b101a400c04d38897da Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 1 Nov 2026 13:29:34 +0100 Subject: libp2p: bump dupe grace period --- src/transport/libp2p/book.rs | 8 +++++--- src/transport/libp2p/driver.rs | 18 +++++++++++++++--- src/transport/libp2p/mod.rs | 11 ++++++----- 3 files changed, 26 insertions(+), 11 deletions(-) (limited to 'src/transport') diff --git a/src/transport/libp2p/book.rs b/src/transport/libp2p/book.rs index e502bf8..087cd17 100644 --- a/src/transport/libp2p/book.rs +++ b/src/transport/libp2p/book.rs @@ -81,7 +81,8 @@ pub(super) struct Book { /// Peers connected only through a relay, dialled directly (`upgrade`): when to again /// at the soonest, and how long to wait after that. pub(super) upgrades: HashMap, - /// The shortest round trip measured on each open connection (libp2p's ping). + /// The shortest round trip measured on each open connection (libp2p's ping), since + /// the last connection to the same peer opened (they are compared then). pub(super) rtts: HashMap, /// Peers whose hole punching failed: how often in a row, and when to try again /// (`PUNCH_RETRIES`). @@ -137,8 +138,9 @@ impl Book { } /// The connections to `peer`, the one to keep first: direct ones before relayed - /// ones, measured ones by their round trip (`rtts`: over the LAN before a VPN's hub, - /// whichever opened first), then the oldest. + /// ones, measured ones by their round trip (`rtts`, the shortest since the last of + /// them opened: over the LAN before a VPN's hub, whichever opened first), then the + /// oldest. pub(super) fn ranked(&self, peer: &PeerId) -> Vec { let mut connections: Vec<(bool, Duration, ConnectionId)> = self .connections diff --git a/src/transport/libp2p/driver.rs b/src/transport/libp2p/driver.rs index b8c8c37..c89a784 100644 --- a/src/transport/libp2p/driver.rs +++ b/src/transport/libp2p/driver.rs @@ -208,9 +208,21 @@ pub(super) async fn run( book.upgrades.remove(&peer_id); book.punching.remove(&peer_id); } - if relaying.local < peer_id - && book.connections.values().filter(|(connected, _, _)| *connected == peer_id).count() > 1 - { + let to_peer: Vec = book + .connections + .iter() + .filter(|(_, (connected, _, _))| *connected == peer_id) + .map(|(id, _)| *id) + .collect(); + if to_peer.len() > 1 { + // A new path to compare: each is judged by its round trips + // from now on, not an old one by its best over hours + // (`Book::ranked`). + for connection in &to_peer { + book.rtts.remove(connection); + } + } + if relaying.local < peer_id && to_peer.len() > 1 { // Only through relays: maybe a punch under way. let grace = match book.connection(&peer_id) { Some(Connection::Relayed) => PUNCH_GRACE, diff --git a/src/transport/libp2p/mod.rs b/src/transport/libp2p/mod.rs index 0d26625..6c57867 100644 --- a/src/transport/libp2p/mod.rs +++ b/src/transport/libp2p/mod.rs @@ -190,11 +190,12 @@ const EXPLORED_AT_ONCE: usize = 4; /// mentioned every few seconds (`swarm::topology`). Checked every `PRUNE_EVERY`. const STALE_AFTER: Duration = Duration::from_secs(600); const PRUNE_EVERY: Duration = Duration::from_secs(60); -/// Several connections to one peer (both dialled at once, or AutoNAT dialled back to -/// see whether it can) are cut to one, the fastest (`Book::duplicates`), this long after -/// the last one opened: long enough for what it was dialled for, and for each to be -/// pinged (at once, as it opens). Looked for every `DEDUPE_EVERY`. -const DUPLICATE_GRACE: Duration = Duration::from_secs(10); +/// Several connections to one peer (both dialled at once, AutoNAT dialling back to see +/// whether it can, or a better path explored) are cut to one, the fastest +/// (`Book::duplicates`), this long after the last one opened: long enough for what it +/// was dialled for, and for each to be pinged since (a new one at once, as it opens; +/// the others within libp2p's ping interval, 15 s). Looked for every `DEDUPE_EVERY`. +const DUPLICATE_GRACE: Duration = Duration::from_secs(20); /// The same while a peer is connected only through relays: a new relayed connection is /// a hole punch under way (`PUNCH_RETRIES`), which takes its tries (up to a QUIC /// handshake's 5 s each, three times) on that connection. -- cgit v1.3.1