Skip to content

Commit

Permalink
chore: Move event handler connection fields into a substruct
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Oct 31, 2023
1 parent e02ce80 commit 1d4079b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
26 changes: 17 additions & 9 deletions homestar-runtime/src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ pub(crate) struct EventHandler<DB: Database> {
sender: Arc<channel::AsyncBoundedChannelSender<Event>>,
receiver: channel::AsyncBoundedChannelReceiver<Event>,
query_senders: FnvHashMap<QueryId, (RequestResponseKey, Option<P2PSender>)>,
connected_peers: FnvHashMap<PeerId, ConnectedPoint>,
connected_peers_limit: u32,
connections: Connections,
request_response_senders: FnvHashMap<RequestId, (RequestResponseKey, P2PSender)>,
rendezvous: Rendezvous,
pubsub_enabled: bool,
Expand All @@ -85,8 +84,7 @@ pub(crate) struct EventHandler<DB: Database> {
sender: Arc<channel::AsyncBoundedChannelSender<Event>>,
receiver: channel::AsyncBoundedChannelReceiver<Event>,
query_senders: FnvHashMap<QueryId, (RequestResponseKey, Option<P2PSender>)>,
connected_peers: FnvHashMap<PeerId, ConnectedPoint>,
connected_peers_limit: u32,
connections: Connections,
request_response_senders: FnvHashMap<RequestId, (RequestResponseKey, P2PSender)>,
rendezvous: Rendezvous,
pubsub_enabled: bool,
Expand All @@ -95,13 +93,20 @@ pub(crate) struct EventHandler<DB: Database> {
external_address_limit: u32,
}

/// Rendezvous protocol configurations and state
struct Rendezvous {
registration_ttl: Duration,
discovery_interval: Duration,
discovered_peers: FnvHashMap<PeerId, PeerDiscoveryInfo>,
cookies: FnvHashMap<PeerId, Cookie>,
}

// Connected peers configuration and state
struct Connections {
peers: FnvHashMap<PeerId, ConnectedPoint>,
max_peers: u32,
}

impl<DB> EventHandler<DB>
where
DB: Database,
Expand Down Expand Up @@ -136,8 +141,10 @@ where
receiver,
query_senders: FnvHashMap::default(),
request_response_senders: FnvHashMap::default(),
connected_peers: FnvHashMap::default(),
connected_peers_limit: settings.network.max_connected_peers,
connections: Connections {
peers: FnvHashMap::default(),
max_peers: settings.network.max_connected_peers,
},
rendezvous: Rendezvous {
registration_ttl: settings.network.rendezvous_registration_ttl,
discovery_interval: settings.network.rendezvous_discovery_interval,
Expand Down Expand Up @@ -167,10 +174,11 @@ where
sender,
receiver,
query_senders: FnvHashMap::default(),
connected_peers: FnvHashMap::default(),
connected_peers_limit: settings.network.max_connected_peers,
discovered_peers: FnvHashMap::default(),
request_response_senders: FnvHashMap::default(),
connected: Connections {
peers: FnvHashMap::default(),
max_peers: settings.network.max_connected_peers,
},
rendezvous: Rendezvous {
registration_ttl: settings.network.rendezvous_registration_ttl,
discovery_interval: settings.network.rendezvous_discovery_interval,
Expand Down
17 changes: 9 additions & 8 deletions homestar-runtime/src/event_handler/swarm_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ async fn handle_swarm_event<THandlerErr: fmt::Debug + Send, DB: Database>(
.cookies
.insert(rendezvous_node, cookie);

let connected_peers_count = event_handler.connected_peers.len();
let connected_peers_count = event_handler.connections.peers.len();

// Skip dialing peers if at connected peers limit
if connected_peers_count >= event_handler.connected_peers_limit as usize {
if connected_peers_count >= event_handler.connections.max_peers as usize {
warn!("peers discovered through rendezvous not dialed because max connected peers limit reached");
return;
}
Expand All @@ -222,7 +222,8 @@ async fn handle_swarm_event<THandlerErr: fmt::Debug + Send, DB: Database>(
.iter()
.filter(|registration| {
!event_handler
.connected_peers
.connections
.peers
.contains_key(&registration.record.peer_id())
})
.collect();
Expand All @@ -235,7 +236,7 @@ async fn handle_swarm_event<THandlerErr: fmt::Debug + Send, DB: Database>(
// Dial discovered peer if not us and not at connected peers limit
if !self_registration
&& connected_peers_count + index
< event_handler.connected_peers_limit as usize
< event_handler.connections.max_peers as usize
{
let peer_id = registration.record.peer_id();
let opts = DialOpts::peer_id(peer_id)
Expand Down Expand Up @@ -672,8 +673,8 @@ async fn handle_swarm_event<THandlerErr: fmt::Debug + Send, DB: Database>(
"mDNS discovered a new peer"
);

if event_handler.connected_peers.len()
< event_handler.connected_peers_limit as usize
if event_handler.connections.peers.len()
< event_handler.connections.max_peers as usize
{
let _ = event_handler.swarm.dial(
DialOpts::peer_id(peer_id)
Expand Down Expand Up @@ -720,14 +721,14 @@ async fn handle_swarm_event<THandlerErr: fmt::Debug + Send, DB: Database>(
} => {
debug!(peer_id=peer_id.to_string(), endpoint=?endpoint, "peer connection established");
// add peer to connected peers list
event_handler.connected_peers.insert(peer_id, endpoint);
event_handler.connections.peers.insert(peer_id, endpoint);
}
SwarmEvent::ConnectionClosed { peer_id, cause, .. } => {
debug!(
peer_id = peer_id.to_string(),
"peer connection closed, cause: {cause:?}"
);
event_handler.connected_peers.remove_entry(&peer_id);
event_handler.connections.peers.remove_entry(&peer_id);

// Remove peer from DHT if not in configured peers
if event_handler.node_addresses.iter().all(|multiaddr| {
Expand Down

0 comments on commit 1d4079b

Please sign in to comment.