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): change mdns::Event to hold Vec #3621

Merged
merged 21 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7d5e606
feat: make mdns::Event clone-able
drHuangMHT Mar 13, 2023
05ce438
Merge branch 'libp2p:master' into master
Mar 13, 2023
a72e5e2
docs: filling CHANGELOG for PR 3606
drHuangMHT Mar 13, 2023
09bef20
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 13, 2023
926c7ca
Update protocols/mdns/CHANGELOG.md
thomaseizinger Mar 13, 2023
49d26c4
Update protocols/mdns/CHANGELOG.md
thomaseizinger Mar 13, 2023
4a35406
Merge branch 'master' into master
mergify[bot] Mar 13, 2023
a8ff1eb
feat: change 'mdns::Event' to hold 'SmallVec'
drHuangMHT Mar 16, 2023
4cec5fa
test: adapting test to 'mdns::Event' change
drHuangMHT Mar 16, 2023
a876f2f
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 16, 2023
18811d2
feat(mdns): change 'mdns::Event' to hold 'Vec'
drHuangMHT Mar 16, 2023
abdce7e
doc: add entry for #3621
drHuangMHT Mar 16, 2023
d5c246c
Update protocols/mdns/CHANGELOG.md
Mar 16, 2023
ed128a7
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 26, 2023
0d06f4c
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 26, 2023
9d45cf3
Remove conflict marker to resolve conflicts
drHuangMHT Mar 26, 2023
752002b
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Apr 11, 2023
05ed0ef
Merge branch 'master' of https://github.com/libp2p/rust-libp2p
drHuangMHT Apr 14, 2023
7cf5556
Update protocols/mdns/CHANGELOG.md
thomaseizinger Apr 14, 2023
3602c76
Merge remote-tracking branch 'upstream/master'
drHuangMHT May 2, 2023
cc9eb65
Merge branch 'master' into master
drHuangMHT May 2, 2023
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
6 changes: 6 additions & 0 deletions protocols/mdns/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.44.0 [unreleased]
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

- Change `mdns::Event` to hold `Vec`. See [PR 3621]
drHuangMHT marked this conversation as resolved.
Show resolved Hide resolved

[PR 3621]: https://github.com/libp2p/rust-libp2p/pull/3621

# 0.43.1 [unreleased]

- Derive `Clone` for `mdns::Event`. See [PR 3606].
Expand Down
72 changes: 6 additions & 66 deletions protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ where
}
}
// Emit discovered event.
let mut discovered = SmallVec::<[(PeerId, Multiaddr); 4]>::new();
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
let mut discovered = Vec::new();
for iface_state in self.iface_states.values_mut() {
while let Poll::Ready((peer, addr, expiration)) =
iface_state.poll(cx, &self.listen_addresses)
Expand All @@ -304,15 +304,13 @@ where
}
}
if !discovered.is_empty() {
let event = Event::Discovered(DiscoveredAddrsIter {
inner: discovered.into_iter(),
});
let event = Event::Discovered(discovered);
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
// Emit expired event.
let now = Instant::now();
let mut closest_expiration = None;
let mut expired = SmallVec::<[(PeerId, Multiaddr); 4]>::new();
let mut expired = Vec::new();
self.discovered_nodes.retain(|(peer, addr, expiration)| {
if *expiration <= now {
log::info!("expired: {} {}", peer, addr);
Expand All @@ -323,9 +321,7 @@ where
true
});
if !expired.is_empty() {
let event = Event::Expired(ExpiredAddrsIter {
inner: expired.into_iter(),
});
let event = Event::Expired(expired);
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
}
if let Some(closest_expiration) = closest_expiration {
Expand All @@ -342,67 +338,11 @@ where
#[derive(Debug, Clone)]
pub enum Event {
/// Discovered nodes through mDNS.
Discovered(DiscoveredAddrsIter),
Discovered(Vec<(PeerId, Multiaddr)>),

/// The given combinations of `PeerId` and `Multiaddr` have expired.
///
/// Each discovered record has a time-to-live. When this TTL expires and the address hasn't
/// been refreshed, we remove it from the list and emit it as an `Expired` event.
Expired(ExpiredAddrsIter),
}

/// Iterator that produces the list of addresses that have been discovered.
#[derive(Clone)]
pub struct DiscoveredAddrsIter {
inner: smallvec::IntoIter<[(PeerId, Multiaddr); 4]>,
}

impl Iterator for DiscoveredAddrsIter {
type Item = (PeerId, Multiaddr);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}

impl ExactSizeIterator for DiscoveredAddrsIter {}

impl fmt::Debug for DiscoveredAddrsIter {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("DiscoveredAddrsIter").finish()
}
}

/// Iterator that produces the list of addresses that have expired.
#[derive(Clone)]
pub struct ExpiredAddrsIter {
inner: smallvec::IntoIter<[(PeerId, Multiaddr); 4]>,
}

impl Iterator for ExpiredAddrsIter {
type Item = (PeerId, Multiaddr);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}

impl ExactSizeIterator for ExpiredAddrsIter {}

impl fmt::Debug for ExpiredAddrsIter {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("ExpiredAddrsIter").finish()
}
Expired(Vec<(PeerId, Multiaddr)>),
}
20 changes: 10 additions & 10 deletions protocols/mdns/tests/use-async-std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ async fn test_expired_async_std() {

loop {
match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await {
Either::Left((Event::Expired(mut peers), _)) => {
if peers.any(|(p, _)| p == b_peer_id) {
Either::Left((Event::Expired(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == b_peer_id) {
return;
}
}
Either::Right((Event::Expired(mut peers), _)) => {
if peers.any(|(p, _)| p == a_peer_id) {
Either::Right((Event::Expired(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == a_peer_id) {
return;
}
}
Expand All @@ -93,8 +93,8 @@ async fn test_no_expiration_on_close_async_std() {

// 1. Connect via address from mDNS event
loop {
if let Event::Discovered(mut peers) = a.next_behaviour_event().await {
if let Some((_, addr)) = peers.find(|(p, _)| p == &b_peer_id) {
if let Event::Discovered(peers) = a.next_behaviour_event().await {
if let Some((_, addr)) = peers.into_iter().find(|(p, _)| p == &b_peer_id) {
a.dial_and_wait(addr).await;
break;
}
Expand Down Expand Up @@ -130,13 +130,13 @@ async fn run_discovery_test(config: Config) {

while !discovered_a && !discovered_b {
match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await {
Either::Left((Event::Discovered(mut peers), _)) => {
if peers.any(|(p, _)| p == b_peer_id) {
Either::Left((Event::Discovered(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == b_peer_id) {
discovered_b = true;
}
}
Either::Right((Event::Discovered(mut peers), _)) => {
if peers.any(|(p, _)| p == a_peer_id) {
Either::Right((Event::Discovered(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == a_peer_id) {
discovered_a = true;
}
}
Expand Down
16 changes: 8 additions & 8 deletions protocols/mdns/tests/use-tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ async fn test_expired_tokio() {

loop {
match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await {
Either::Left((Event::Expired(mut peers), _)) => {
if peers.any(|(p, _)| p == b_peer_id) {
Either::Left((Event::Expired(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == b_peer_id) {
return;
}
}
Either::Right((Event::Expired(mut peers), _)) => {
if peers.any(|(p, _)| p == a_peer_id) {
Either::Right((Event::Expired(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == a_peer_id) {
return;
}
}
Expand All @@ -86,13 +86,13 @@ async fn run_discovery_test(config: Config) {

while !discovered_a && !discovered_b {
match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await {
Either::Left((Event::Discovered(mut peers), _)) => {
if peers.any(|(p, _)| p == b_peer_id) {
Either::Left((Event::Discovered(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == b_peer_id) {
discovered_b = true;
}
}
Either::Right((Event::Discovered(mut peers), _)) => {
if peers.any(|(p, _)| p == a_peer_id) {
Either::Right((Event::Discovered(peers), _)) => {
if peers.into_iter().any(|(p, _)| p == a_peer_id) {
discovered_a = true;
}
}
Expand Down