From fd56763352bc2cc308234068ddd7bf0a2767c782 Mon Sep 17 00:00:00 2001 From: Divma <26765164+divagant-martian@users.noreply.github.com> Date: Mon, 2 Sep 2024 06:59:33 -0500 Subject: [PATCH] fix(iroh-net): reduce noise in swarm discovery due to republish (#2685) ## Description Since republish occurs more or less every second, for every peer in the local network, every second we insert it in the node_map (which includes locking), print a debug line, and for requesters, re-send a value that was previously sent (since every time a new sender is added we immediately send known values). This pr makes it so that we do all this only when new values are found. ## Breaking Changes - swarm discovery will no longer send consecutive repeated values ## Notes & open questions the typo is unrelated to the pr but was flagged by ci ## Change checklist - [x] Self-review. - [ ] ~~Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant.~~ - [ ] ~~Tests if relevant.~~ - [x] All breaking changes documented. --- iroh-blobs/src/downloader.rs | 2 +- .../src/discovery/local_swarm_discovery.rs | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/iroh-blobs/src/downloader.rs b/iroh-blobs/src/downloader.rs index 21644e8d93..2998f81ba7 100644 --- a/iroh-blobs/src/downloader.rs +++ b/iroh-blobs/src/downloader.rs @@ -411,7 +411,7 @@ impl Downloader { } } - /// Declare that certains nodes can be used to download a hash. + /// Declare that certain nodes can be used to download a hash. /// /// Note that this does not start a download, but only provides new nodes to already queued /// downloads. Use [`Self::queue`] to queue a download. diff --git a/iroh-net/src/discovery/local_swarm_discovery.rs b/iroh-net/src/discovery/local_swarm_discovery.rs index b952a06b08..aa369bdb8b 100644 --- a/iroh-net/src/discovery/local_swarm_discovery.rs +++ b/iroh-net/src/discovery/local_swarm_discovery.rs @@ -121,19 +121,28 @@ impl LocalSwarmDiscovery { continue; } - if let Some(senders) = senders.get(&discovered_node_id) { - for sender in senders.values() { - let item: DiscoveryItem = (&peer_info).into(); - trace!(?item, "sending DiscoveryItem"); - sender.send(Ok(item)).await.ok(); + let entry = node_addrs.entry(discovered_node_id); + if let std::collections::hash_map::Entry::Occupied(ref entry) = entry { + if entry.get() == &peer_info { + // this is a republish we already know about + continue; } } + debug!( ?discovered_node_id, ?peer_info, "adding node to LocalSwarmDiscovery address book" ); - node_addrs.insert(discovered_node_id, peer_info); + + if let Some(senders) = senders.get(&discovered_node_id) { + let item: DiscoveryItem = (&peer_info).into(); + trace!(?item, senders = senders.len(), "sending DiscoveryItem"); + for sender in senders.values() { + sender.send(Ok(item.clone())).await.ok(); + } + } + entry.or_insert(peer_info); } Message::SendAddrs(node_id, sender) => { let id = last_id + 1;