From b04e4aa29530a2c68a9c0e8145fc7a481e5f1dc9 Mon Sep 17 00:00:00 2001 From: wlynxg Date: Sat, 5 Oct 2024 23:41:04 +0800 Subject: [PATCH 1/4] feat(mdns): emit `ToSwarm::NewExternalAddrOfPeer` --- protocols/mdns/src/behaviour.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 6355fbf4943..38150a72ebb 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -41,6 +41,8 @@ use std::collections::hash_map::{Entry, HashMap}; use std::future::Future; use std::sync::{Arc, RwLock}; use std::{cmp, fmt, io, net::IpAddr, pin::Pin, task::Context, task::Poll, time::Instant}; +use std::collections::VecDeque; +use void::Void; /// An abstraction to allow for compatibility with various async runtimes. pub trait Provider: 'static { @@ -174,6 +176,9 @@ where listen_addresses: Arc>, local_peer_id: PeerId, + + /// Queued events to return when the behaviour is being polled. + pending_events: VecDeque>, } impl

Behaviour

@@ -193,6 +198,7 @@ where discovered_nodes: Default::default(), closest_expiration: Default::default(), listen_addresses: Default::default(), + pending_events: VecDeque::new(), local_peer_id, }) } @@ -290,6 +296,11 @@ where &mut self, cx: &mut Context<'_>, ) -> Poll>> { + // If there are pending addresses to be emitted we emit them. + if let Some(event) = self.pending_events.pop_front() { + return Poll::Ready(event); + } + // Poll ifwatch. while let Poll::Ready(Some(event)) = Pin::new(&mut self.if_watch).poll_next(cx) { match event { @@ -345,6 +356,11 @@ where } else { tracing::info!(%peer, address=%addr, "discovered peer on address"); self.discovered_nodes.push((peer, addr.clone(), expiration)); + self.pending_events + .push_back(ToSwarm::NewExternalAddrOfPeer { + peer_id:peer.clone(), + address: addr.clone(), + }); discovered.push((peer, addr)); } } From 9d1a02e217696248ab151b21b237061b139f99f9 Mon Sep 17 00:00:00 2001 From: wlynxg Date: Wed, 9 Oct 2024 10:18:14 +0800 Subject: [PATCH 2/4] style: optimize code format --- protocols/mdns/src/behaviour.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 38150a72ebb..0fdb66490fc 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -358,7 +358,7 @@ where self.discovered_nodes.push((peer, addr.clone(), expiration)); self.pending_events .push_back(ToSwarm::NewExternalAddrOfPeer { - peer_id:peer.clone(), + peer_id: peer, address: addr.clone(), }); discovered.push((peer, addr)); From 58f77a5234ce382d1d1ee40ecb6c862d3cac2f18 Mon Sep 17 00:00:00 2001 From: wlynxg Date: Wed, 9 Oct 2024 10:34:38 +0800 Subject: [PATCH 3/4] style: optimize code format --- protocols/mdns/src/behaviour.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 0fdb66490fc..5e8825c582b 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -38,10 +38,10 @@ use libp2p_swarm::{ }; use smallvec::SmallVec; use std::collections::hash_map::{Entry, HashMap}; +use std::collections::VecDeque; use std::future::Future; use std::sync::{Arc, RwLock}; use std::{cmp, fmt, io, net::IpAddr, pin::Pin, task::Context, task::Poll, time::Instant}; -use std::collections::VecDeque; use void::Void; /// An abstraction to allow for compatibility with various async runtimes. From e98e05dd6a1cdbb99becba213dc215a56d2035ec Mon Sep 17 00:00:00 2001 From: wlynxg Date: Wed, 9 Oct 2024 22:58:40 +0800 Subject: [PATCH 4/4] fix: change `ToSwarm::NewExternalAddrOfPeer` return timing --- protocols/mdns/src/behaviour.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 5e8825c582b..874d3ea3457 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -296,11 +296,6 @@ where &mut self, cx: &mut Context<'_>, ) -> Poll>> { - // If there are pending addresses to be emitted we emit them. - if let Some(event) = self.pending_events.pop_front() { - return Poll::Ready(event); - } - // Poll ifwatch. while let Poll::Ready(Some(event)) = Pin::new(&mut self.if_watch).poll_next(cx) { match event { @@ -369,6 +364,12 @@ where let event = Event::Discovered(discovered); return Poll::Ready(ToSwarm::GenerateEvent(event)); } + + // If there are pending addresses to be emitted we emit them. + if let Some(event) = self.pending_events.pop_front() { + return Poll::Ready(event); + } + // Emit expired event. let now = Instant::now(); let mut closest_expiration = None;