From 1d4079b19884b9b130ac9a6274b96ed04f54720f Mon Sep 17 00:00:00 2001 From: Brian Ginsburg Date: Tue, 31 Oct 2023 09:53:54 -0700 Subject: [PATCH] chore: Move event handler connection fields into a substruct --- homestar-runtime/src/event_handler.rs | 26 ++++++++++++------- .../src/event_handler/swarm_event.rs | 17 ++++++------ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/homestar-runtime/src/event_handler.rs b/homestar-runtime/src/event_handler.rs index c55d03d1f..86f374ec3 100644 --- a/homestar-runtime/src/event_handler.rs +++ b/homestar-runtime/src/event_handler.rs @@ -61,8 +61,7 @@ pub(crate) struct EventHandler { sender: Arc>, receiver: channel::AsyncBoundedChannelReceiver, query_senders: FnvHashMap)>, - connected_peers: FnvHashMap, - connected_peers_limit: u32, + connections: Connections, request_response_senders: FnvHashMap, rendezvous: Rendezvous, pubsub_enabled: bool, @@ -85,8 +84,7 @@ pub(crate) struct EventHandler { sender: Arc>, receiver: channel::AsyncBoundedChannelReceiver, query_senders: FnvHashMap)>, - connected_peers: FnvHashMap, - connected_peers_limit: u32, + connections: Connections, request_response_senders: FnvHashMap, rendezvous: Rendezvous, pubsub_enabled: bool, @@ -95,6 +93,7 @@ pub(crate) struct EventHandler { external_address_limit: u32, } +/// Rendezvous protocol configurations and state struct Rendezvous { registration_ttl: Duration, discovery_interval: Duration, @@ -102,6 +101,12 @@ struct Rendezvous { cookies: FnvHashMap, } +// Connected peers configuration and state +struct Connections { + peers: FnvHashMap, + max_peers: u32, +} + impl EventHandler where DB: Database, @@ -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, @@ -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, diff --git a/homestar-runtime/src/event_handler/swarm_event.rs b/homestar-runtime/src/event_handler/swarm_event.rs index b957243cc..011317122 100644 --- a/homestar-runtime/src/event_handler/swarm_event.rs +++ b/homestar-runtime/src/event_handler/swarm_event.rs @@ -209,10 +209,10 @@ async fn handle_swarm_event( .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; } @@ -222,7 +222,8 @@ async fn handle_swarm_event( .iter() .filter(|registration| { !event_handler - .connected_peers + .connections + .peers .contains_key(®istration.record.peer_id()) }) .collect(); @@ -235,7 +236,7 @@ async fn handle_swarm_event( // 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) @@ -672,8 +673,8 @@ async fn handle_swarm_event( "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) @@ -720,14 +721,14 @@ async fn handle_swarm_event( } => { 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| {