Skip to content

Commit

Permalink
refactor: Improve dialing after rendezvous discovery
Browse files Browse the repository at this point in the history
Only dial when we are not at connected peer limit.
Only dial peers we are not already connected to.
  • Loading branch information
bgins committed Oct 25, 2023
1 parent e79ee5f commit 24a1512
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions homestar-runtime/src/event_handler/swarm_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use libp2p::{
},
mdns,
multiaddr::Protocol,
rendezvous::{self, Namespace},
rendezvous::{self, Namespace, Registration},
request_response,
swarm::{dial_opts::DialOpts, SwarmEvent},
PeerId, StreamProtocol,
Expand Down Expand Up @@ -190,17 +190,34 @@ async fn handle_swarm_event<THandlerErr: fmt::Debug + Send, DB: Database>(
registrations,
cookie,
} => {
// save cookie for later (when we are hungry for snacks again. yummy.)
if cookie.namespace() == Some(&Namespace::from_static(RENDEZVOUS_NAMESPACE)) {
// Store cookie
event_handler
.rendezvous_cookies
.insert(rendezvous_node, cookie);

// Dial discovered peers
for (index, registration) in registrations.iter().enumerate() {
let connected_peers_count = event_handler.connected_peers.len();

// Skip dialing peers if at connected peers limit
if connected_peers_count >= event_handler.connected_peers_limit as usize {
return;

This comment has been minimized.

Copy link
@zeeshanlakhani

zeeshanlakhani Oct 25, 2023

Contributor

may be worth a log, debug at least?

}

// Filter out already connected peers
let new_registrations: Vec<&Registration> = registrations

This comment has been minimized.

Copy link
@zeeshanlakhani

zeeshanlakhani Oct 25, 2023

Contributor

like the naming.

.iter()
.filter(|registration| {
!event_handler
.connected_peers
.contains_key(&registration.record.peer_id())
})
.collect();

// Dial newly discovered peers
for (index, registration) in new_registrations.iter().enumerate() {
// Dial discovered peer if not us and not at connected peers limit
if &registration.record.peer_id() != event_handler.swarm.local_peer_id()
&& event_handler.connected_peers.len() + index
&& connected_peers_count + index
< event_handler.connected_peers_limit as usize
{
// TODO: do anything with ttl here?
Expand All @@ -221,7 +238,7 @@ async fn handle_swarm_event<THandlerErr: fmt::Debug + Send, DB: Database>(
}
}
} else {
// don't add peers that aren't from our namespace
// Do not dial peers that are not using our namespace
warn!(peer_id=rendezvous_node.to_string(), namespace=?cookie.namespace(), "rendezvous peer gave records from an unexpected namespace");
}
}
Expand Down

0 comments on commit 24a1512

Please sign in to comment.