aboutsummaryrefslogtreecommitdiffstats
path: root/src/transport/libp2p/mod.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-01 13:20:08 +0100
committertv <tv@krebsco.de>2026-11-01 13:20:08 +0100
commit2ec7d7e0eedf504035417bebcace68f22f38ba10 (patch)
tree65b1b4c6273bc0af4472e9fd0a772e0a7db862ea /src/transport/libp2p/mod.rs
parent59833811cce25f03efbdc889f806ad75804942ab (diff)
libp2p: revise published address count/selection
Diffstat (limited to 'src/transport/libp2p/mod.rs')
-rw-r--r--src/transport/libp2p/mod.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/transport/libp2p/mod.rs b/src/transport/libp2p/mod.rs
index 6032891..2ad07e5 100644
--- a/src/transport/libp2p/mod.rs
+++ b/src/transport/libp2p/mod.rs
@@ -145,6 +145,11 @@ const MIN_RATE: u64 = 1 << 20;
const READS: usize = 32;
/// Addresses kept per peer, and peers with addresses at most.
const ADDRESSES_PER_PEER: usize = 8;
+/// Of this node's own, the direct ones (the best first) and those through relays it
+/// publishes at most (`local_addresses`): a node listening everywhere has many direct
+/// ones, and those through a relay are what reaches it from behind a NAT.
+const PUBLISHED_DIRECT: usize = 4;
+const PUBLISHED_RELAYED: usize = 2;
const MAX_PEERS: usize = 4096;
/// A relayed connection (circuit) through this node lasts at most this long and carries
@@ -363,20 +368,24 @@ impl Transport for Libp2pTransport {
}
/// Where we can be reached, each with our peer ID: the announced addresses, or else
- /// those we listen on (`published`), and those through relays. A relay adds each
- /// direct one again, ending in `/p2p-circuit`: where to reserve a slot.
+ /// those we listen on (`published`), and those through relays; at most
+ /// `PUBLISHED_DIRECT` and `PUBLISHED_RELAYED` of them, so the direct ones never
+ /// crowd out the relayed ones. A relay adds each direct one again, ending in
+ /// `/p2p-circuit`: where to reserve a slot.
fn local_addresses(&self) -> Vec<String> {
let book = lock(&self.book);
let direct: Vec<Multiaddr> = book
.direct()
.into_iter()
.filter_map(|address| address.with_p2p(self.peer_id).ok())
+ .take(PUBLISHED_DIRECT)
.collect();
let relayed = book
.listening
.iter()
.filter(|address| is_relayed(address))
- .filter_map(|address| address.clone().with_p2p(self.peer_id).ok());
+ .filter_map(|address| address.clone().with_p2p(self.peer_id).ok())
+ .take(PUBLISHED_RELAYED);
let offers: Vec<Multiaddr> = if book.serving {
direct
.iter()