Skip to content

Commit 54f4b19

Browse files
committed
Revert "refactor(iroh-net): remove random choice of direct addr (#2509)"
This reverts commit c1c3539.
1 parent 9052905 commit 54f4b19

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

iroh-net/src/magicsock.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,10 @@ impl MagicSock {
499499
let dest = QuicMappedAddr(dest);
500500

501501
let mut transmits_sent = 0;
502-
match self.node_map.get_send_addrs(dest) {
502+
match self
503+
.node_map
504+
.get_send_addrs(dest, self.ipv6_reported.load(Ordering::Relaxed))
505+
{
503506
Some((public_key, udp_addr, relay_url, mut msgs)) => {
504507
let mut pings_sent = false;
505508
// If we have pings to send, we *have* to send them out first.
@@ -3012,7 +3015,7 @@ mod tests {
30123015
#[tokio::test(flavor = "multi_thread")]
30133016
async fn test_two_devices_roundtrip_network_change() -> Result<()> {
30143017
time::timeout(
3015-
Duration::from_secs(90),
3018+
Duration::from_secs(50),
30163019
test_two_devices_roundtrip_network_change_impl(),
30173020
)
30183021
.await?

iroh-net/src/magicsock/node_map.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ impl NodeMap {
186186
pub(super) fn get_send_addrs(
187187
&self,
188188
addr: QuicMappedAddr,
189+
have_ipv6: bool,
189190
) -> Option<(
190191
PublicKey,
191192
Option<SocketAddr>,
@@ -195,7 +196,7 @@ impl NodeMap {
195196
let mut inner = self.inner.lock();
196197
let ep = inner.get_mut(NodeStateKey::QuicMappedAddr(addr))?;
197198
let public_key = *ep.public_key();
198-
let (udp_addr, relay_url, msgs) = ep.get_send_addrs();
199+
let (udp_addr, relay_url, msgs) = ep.get_send_addrs(have_ipv6);
199200
Some((public_key, udp_addr, relay_url, msgs))
200201
}
201202

iroh-net/src/magicsock/node_map/node_state.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66
};
77

88
use iroh_metrics::inc;
9+
use rand::seq::IteratorRandom;
910
use serde::{Deserialize, Serialize};
1011
use tokio::sync::mpsc;
1112
use tracing::{debug, event, info, instrument, trace, warn, Level};
@@ -260,8 +261,12 @@ impl NodeState {
260261

261262
/// Returns the address(es) that should be used for sending the next packet.
262263
///
263-
/// This may return to send on one, both or no paths.
264-
fn addr_for_send(&mut self, now: &Instant) -> (Option<SocketAddr>, Option<RelayUrl>) {
264+
/// Any or all of the UDP and relay addrs may be non-zero.
265+
fn addr_for_send(
266+
&mut self,
267+
now: &Instant,
268+
have_ipv6: bool,
269+
) -> (Option<SocketAddr>, Option<RelayUrl>) {
265270
if relay_only_mode() {
266271
debug!("in `DEV_relay_ONLY` mode, giving the relay address as the only viable address for this endpoint");
267272
return (None, self.relay_url());
@@ -285,8 +290,20 @@ impl NodeState {
285290
(Some(best_addr.addr), self.relay_url())
286291
}
287292
best_addr::State::Empty => {
288-
trace!("best_addr is unset, use relay");
289-
(None, self.relay_url())
293+
// No direct connection has been used before. If we know of any possible
294+
// candidate addresses, randomly try to use one while also sending via relay
295+
// at the same time.
296+
let addr = self
297+
.direct_addr_state
298+
.keys()
299+
.filter(|ipp| match ipp.ip() {
300+
IpAddr::V4(_) => true,
301+
IpAddr::V6(_) => have_ipv6,
302+
})
303+
.choose_stable(&mut rand::thread_rng())
304+
.map(|ipp| SocketAddr::from(*ipp));
305+
trace!(udp_addr = ?addr, "best_addr is unset, use candidate addr and relay");
306+
(addr, self.relay_url())
290307
}
291308
};
292309
let typ = match (best_addr, relay_url.clone()) {
@@ -1089,10 +1106,11 @@ impl NodeState {
10891106
#[instrument("get_send_addrs", skip_all, fields(node = %self.node_id.fmt_short()))]
10901107
pub(crate) fn get_send_addrs(
10911108
&mut self,
1109+
have_ipv6: bool,
10921110
) -> (Option<SocketAddr>, Option<RelayUrl>, Vec<PingAction>) {
10931111
let now = Instant::now();
10941112
self.last_used.replace(now);
1095-
let (udp_addr, relay_url) = self.addr_for_send(&now);
1113+
let (udp_addr, relay_url) = self.addr_for_send(&now, have_ipv6);
10961114
let mut ping_msgs = Vec::new();
10971115

10981116
if self.want_call_me_maybe(&now) {

iroh/tests/sync.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ async fn sync_gossip_bulk() -> Result<()> {
243243

244244
/// This tests basic sync and gossip with 3 peers.
245245
#[tokio::test]
246-
#[ignore = "flaky"]
247246
async fn sync_full_basic() -> Result<()> {
248247
let mut rng = test_rng(b"sync_full_basic");
249248
setup_logging();

0 commit comments

Comments
 (0)