Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
use Toggle instead of Option for mdns (#14667)
Browse files Browse the repository at this point in the history
  • Loading branch information
melekes authored Jul 31, 2023
1 parent 115af33 commit 1d13511
Showing 1 changed file with 43 additions and 50 deletions.
93 changes: 43 additions & 50 deletions client/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ impl DiscoveryConfig {
discovery_only_if_under_num,
mdns: if enable_mdns {
match TokioMdns::new(mdns::Config::default(), local_peer_id) {
Ok(mdns) => Some(mdns),
Ok(mdns) => Toggle::from(Some(mdns)),
Err(err) => {
warn!(target: "sub-libp2p", "Failed to initialize mDNS: {:?}", err);
None
Toggle::from(None)
},
}
} else {
None
Toggle::from(None)
},
allow_non_globals_in_dht,
known_external_addresses: LruHashSet::new(
Expand All @@ -280,7 +280,7 @@ pub struct DiscoveryBehaviour {
/// it's always enabled in `NetworkWorker::new()`.
kademlia: Toggle<Kademlia<MemoryStore>>,
/// Discovers nodes on the local network.
mdns: Option<TokioMdns>,
mdns: Toggle<TokioMdns>,
/// Stream that fires when we need to perform the next random Kademlia query. `None` if
/// random walking is disabled.
next_kad_random_query: Option<Delay>,
Expand Down Expand Up @@ -568,14 +568,12 @@ impl NetworkBehaviour for DiscoveryBehaviour {
effective_role,
)?;

if let Some(ref mut mdns) = self.mdns {
list_to_filter.extend(mdns.handle_pending_outbound_connection(
connection_id,
maybe_peer,
addresses,
effective_role,
)?);
}
list_to_filter.extend(self.mdns.handle_pending_outbound_connection(
connection_id,
maybe_peer,
addresses,
effective_role,
)?);

if !self.allow_private_ip {
list_to_filter.retain(|addr| match addr.iter().next() {
Expand Down Expand Up @@ -650,9 +648,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
FromSwarm::NewListenAddr(e) => {
self.kademlia.on_swarm_event(FromSwarm::NewListenAddr(e));

if let Some(ref mut mdns) = self.mdns {
mdns.on_swarm_event(FromSwarm::NewListenAddr(e));
}
self.mdns.on_swarm_event(FromSwarm::NewListenAddr(e));
},
FromSwarm::ExternalAddrConfirmed(e @ ExternalAddrConfirmed { addr }) => {
let new_addr = addr.clone().with(Protocol::P2p(self.local_peer_id));
Expand Down Expand Up @@ -913,43 +909,40 @@ impl NetworkBehaviour for DiscoveryBehaviour {
}

// Poll mDNS.
if let Some(ref mut mdns) = self.mdns {
while let Poll::Ready(ev) = mdns.poll(cx, params) {
match ev {
ToSwarm::GenerateEvent(event) => match event {
mdns::Event::Discovered(list) => {
if self.num_connections >= self.discovery_only_if_under_num {
continue
}
while let Poll::Ready(ev) = self.mdns.poll(cx, params) {
match ev {
ToSwarm::GenerateEvent(event) => match event {
mdns::Event::Discovered(list) => {
if self.num_connections >= self.discovery_only_if_under_num {
continue
}

self.pending_events.extend(
list.into_iter()
.map(|(peer_id, _)| DiscoveryOut::Discovered(peer_id)),
);
if let Some(ev) = self.pending_events.pop_front() {
return Poll::Ready(ToSwarm::GenerateEvent(ev))
}
},
mdns::Event::Expired(_) => {},
},
ToSwarm::Dial { .. } => {
unreachable!("mDNS never dials!");
self.pending_events.extend(
list.into_iter().map(|(peer_id, _)| DiscoveryOut::Discovered(peer_id)),
);
if let Some(ev) = self.pending_events.pop_front() {
return Poll::Ready(ToSwarm::GenerateEvent(ev))
}
},
ToSwarm::NotifyHandler { event, .. } => match event {}, /* `event` is an */
// enum with no
// variant
ToSwarm::CloseConnection { peer_id, connection } =>
return Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }),
ToSwarm::NewExternalAddrCandidate(observed) =>
return Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)),
ToSwarm::ExternalAddrConfirmed(addr) =>
return Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)),
ToSwarm::ExternalAddrExpired(addr) =>
return Poll::Ready(ToSwarm::ExternalAddrExpired(addr)),
ToSwarm::ListenOn { opts } => return Poll::Ready(ToSwarm::ListenOn { opts }),
ToSwarm::RemoveListener { id } =>
return Poll::Ready(ToSwarm::RemoveListener { id }),
}
mdns::Event::Expired(_) => {},
},
ToSwarm::Dial { .. } => {
unreachable!("mDNS never dials!");
},
ToSwarm::NotifyHandler { event, .. } => match event {}, /* `event` is an */
// enum with no
// variant
ToSwarm::CloseConnection { peer_id, connection } =>
return Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }),
ToSwarm::NewExternalAddrCandidate(observed) =>
return Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)),
ToSwarm::ExternalAddrConfirmed(addr) =>
return Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)),
ToSwarm::ExternalAddrExpired(addr) =>
return Poll::Ready(ToSwarm::ExternalAddrExpired(addr)),
ToSwarm::ListenOn { opts } => return Poll::Ready(ToSwarm::ListenOn { opts }),
ToSwarm::RemoveListener { id } =>
return Poll::Ready(ToSwarm::RemoveListener { id }),
}
}

Expand Down

0 comments on commit 1d13511

Please sign in to comment.