Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mdns): emit ToSwarm::NewExternalAddrOfPeer #5623

Closed
wants to merge 5 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
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 {
Expand Down Expand Up @@ -174,6 +176,9 @@
listen_addresses: Arc<RwLock<ListenAddresses>>,

local_peer_id: PeerId,

/// Queued events to return when the behaviour is being polled.
pending_events: VecDeque<ToSwarm<Event, Void>>,
}

impl<P> Behaviour<P>
Expand All @@ -193,6 +198,7 @@
discovered_nodes: Default::default(),
closest_expiration: Default::default(),
listen_addresses: Default::default(),
pending_events: VecDeque::new(),
local_peer_id,
})
}
Expand Down Expand Up @@ -290,6 +296,11 @@
&mut self,
cx: &mut Context<'_>,
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
// If there are pending addresses to be emitted we emit them.
if let Some(event) = self.pending_events.pop_front() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you prefer having this here instead of after returning discovered addresses?
I feel it's more correct to do this after returning the addresses and maintaining the previous flow of the pool loop.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comments.

However, my idea here is that Event::Discovered and ToSwarm::NewExternalAddrOfPeer events are related.

After Event::Discovered is returned in

return Poll::Ready(ToSwarm::GenerateEvent(event));
,
poll will return.
When poll is entered next time, the NewExternalAddrOfPeer event will be returned because there is a NewExternalAddrOfPeer event in the queue.

The result is similar to this:

receive mdns::Event::Discovered Event
mDNS discovered a new peer: 12D3KooWGkhG2wR1mtadqShqiVEBjLHo8xk48ULrbkdftMsBbttR
NewExternalAddrOfPeer: /ip4/192.168.175.1/udp/63808/quic-v1/p2p/12D3KooWGkhG2wR1mtadqShqiVEBjLHo8xk48ULrbkdftMsBbttR, 12D3KooWGkhG2wR1mtadqShqiVEBjLHo8xk48ULrbkdftMsBbttR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, my idea here is that Event::Discovered and ToSwarm::NewExternalAddrOfPeer events are related.

yeah exactly, that's why if we discover multiple addresses we shouldn't return all the NewExternalAddrOfPeer events before the second and subsequent Event::Discovered(discovered) right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your detailed explanation.
I have placed the return time of the NewExternalAddrOfPeer event after the Event::Discovered event.
Please check whether it meets your expectations.

return Poll::Ready(event);
}

// Poll ifwatch.
while let Poll::Ready(Some(event)) = Pin::new(&mut self.if_watch).poll_next(cx) {
match event {
Expand Down Expand Up @@ -345,6 +356,11 @@
} 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(),

Check failure on line 361 in protocols/mdns/src/behaviour.rs

View workflow job for this annotation

GitHub Actions / clippy (1.80.0)

using `clone` on type `PeerId` which implements the `Copy` trait

Check failure on line 361 in protocols/mdns/src/behaviour.rs

View workflow job for this annotation

GitHub Actions / clippy (beta)

using `clone` on type `PeerId` which implements the `Copy` trait
address: addr.clone(),
});
discovered.push((peer, addr));
}
}
Expand Down
Loading