Skip to content

Commit ca85faa

Browse files
fix(iroh): ensure initial addresses are published via discovery (#3525)
## Description - Ensures that the addresses are published on startup - Published the last known `NodeData` when adding a new discovery service, to ensure all services are up to date
1 parent 3dc413c commit ca85faa

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

iroh/src/discovery.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,8 @@ impl From<DiscoveryItem> for NodeInfo {
432432
#[derive(Debug, Default, Clone)]
433433
pub struct ConcurrentDiscovery {
434434
services: Arc<RwLock<Vec<Box<dyn Discovery>>>>,
435+
/// The data last published, used to publish when adding a new service.
436+
last_data: Arc<RwLock<Option<NodeData>>>,
435437
}
436438

437439
impl ConcurrentDiscovery {
@@ -444,15 +446,28 @@ impl ConcurrentDiscovery {
444446
pub fn from_services(services: Vec<Box<dyn Discovery>>) -> Self {
445447
Self {
446448
services: Arc::new(RwLock::new(services)),
449+
last_data: Default::default(),
447450
}
448451
}
449452

450453
/// Adds a [`Discovery`] service.
454+
///
455+
/// If there is historical discovery data, it will be published immediately on this service.
451456
pub fn add(&self, service: impl Discovery + 'static) {
452-
self.services
453-
.write()
454-
.expect("poisoned")
455-
.push(Box::new(service));
457+
self.add_boxed(Box::new(service))
458+
}
459+
460+
/// Adds an already `Box`ed [`Discovery`] service.
461+
///
462+
/// If there is historical discovery data, it will be published immediately on this service.
463+
pub fn add_boxed(&self, service: Box<dyn Discovery>) {
464+
{
465+
let data = self.last_data.read().expect("poisoned");
466+
if let Some(data) = &*data {
467+
service.publish(data)
468+
}
469+
}
470+
self.services.write().expect("poisoned").push(service);
456471
}
457472

458473
/// Is there any services configured?
@@ -474,6 +489,7 @@ where
474489
let services = iter.into_iter().collect::<Vec<_>>();
475490
Self {
476491
services: Arc::new(RwLock::new(services)),
492+
last_data: Default::default(),
477493
}
478494
}
479495
}
@@ -484,6 +500,11 @@ impl Discovery for ConcurrentDiscovery {
484500
for service in &*services {
485501
service.publish(data);
486502
}
503+
504+
self.last_data
505+
.write()
506+
.expect("poisoned")
507+
.replace(data.clone());
487508
}
488509

489510
fn resolve(&self, node_id: NodeId) -> Option<BoxStream<Result<DiscoveryItem, DiscoveryError>>> {

iroh/src/magicsock.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,9 @@ impl Actor {
18781878

18791879
let mut net_report_watcher = self.msock.net_report.watch();
18801880

1881+
// ensure we are doing an initial publish of our addresses
1882+
self.msock.publish_my_addr();
1883+
18811884
loop {
18821885
self.msock.metrics.magicsock.actor_tick_main.inc();
18831886
#[cfg(not(wasm_browser))]

0 commit comments

Comments
 (0)