aboutsummaryrefslogtreecommitdiffstats
path: root/src/transport/libp2p
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-01 13:29:34 +0100
committertv <tv@krebsco.de>2026-11-01 13:29:34 +0100
commitfec2900523cba5a70b7f0f67f2b132c7a655a926 (patch)
tree60f2a4e0ab6dbafe572154dc1cd1cd9ebfd1cc7a /src/transport/libp2p
parent497eab7136b9d35101f4a5ffa81797efb85da390 (diff)
libp2p: bump dupe grace period
Diffstat (limited to 'src/transport/libp2p')
-rw-r--r--src/transport/libp2p/book.rs8
-rw-r--r--src/transport/libp2p/driver.rs18
-rw-r--r--src/transport/libp2p/mod.rs11
3 files changed, 26 insertions, 11 deletions
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<PeerId, (tokio::time::Instant, Duration)>,
- /// 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<ConnectionId, Duration>,
/// 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<ConnectionId> {
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<ConnectionId> = 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.