diff --git a/rust/connlib/snownet/src/node.rs b/rust/connlib/snownet/src/node.rs index c0a691ab0..f4c9ffd77 100644 --- a/rust/connlib/snownet/src/node.rs +++ b/rust/connlib/snownet/src/node.rs @@ -157,17 +157,20 @@ where self.pending_events.clear(); - let connections = self.connections.iter_ids().collect::>(); - let num_connections = connections.len(); + let closed_connections = self + .connections + .iter_ids() + .map(Event::ConnectionClosed) + .collect::>(); + let num_connections = closed_connections.len(); - self.pending_events - .push_back(Event::ConnectionsCleared(connections)); + self.pending_events.extend(closed_connections); self.host_candidates.clear(); self.connections.clear(); self.buffered_transmits.clear(); - tracing::debug!("Cleared {num_connections} connections"); + tracing::debug!(%num_connections, "Closed all connections as part of reconnecting"); } pub fn public_key(&self) -> PublicKey { @@ -1270,8 +1273,8 @@ pub enum Event { /// All state associated with the connection has been cleared. ConnectionFailed(TId), - /// The referenced connections had their state cleared. - ConnectionsCleared(Vec), + /// We closed a connection (e.g. due to inactivity, roaming, etc). + ConnectionClosed(TId), } #[derive(Clone, PartialEq)] diff --git a/rust/connlib/snownet/tests/lib.rs b/rust/connlib/snownet/tests/lib.rs index 921daa3e9..a7b948388 100644 --- a/rust/connlib/snownet/tests/lib.rs +++ b/rust/connlib/snownet/tests/lib.rs @@ -634,7 +634,7 @@ impl TestNode { .in_scope(|| other.node.remove_remote_candidate(connection, candidate)), Event::ConnectionEstablished(_) | Event::ConnectionFailed(_) - | Event::ConnectionsCleared(_) => {} + | Event::ConnectionClosed(_) => {} }; } } diff --git a/rust/connlib/tunnel/src/client.rs b/rust/connlib/tunnel/src/client.rs index 3d7396535..a47f500e5 100644 --- a/rust/connlib/tunnel/src/client.rs +++ b/rust/connlib/tunnel/src/client.rs @@ -812,23 +812,13 @@ impl ClientState { self.node.handle_timeout(now); self.mangled_dns_queries.retain(|_, exp| now < *exp); + let mut resources_changed = false; // Track this separately to batch together `ResourcesChanged` events. + while let Some(event) = self.node.poll_event() { match event { - snownet::Event::ConnectionFailed(id) => { + snownet::Event::ConnectionFailed(id) | snownet::Event::ConnectionClosed(id) => { self.cleanup_connected_gateway(&id); - self.buffered_events - .push_back(ClientEvent::ResourcesChanged { - resources: self.resources(), - }); - } - snownet::Event::ConnectionsCleared(ids) => { - for id in ids { - self.cleanup_connected_gateway(&id); - } - self.buffered_events - .push_back(ClientEvent::ResourcesChanged { - resources: self.resources(), - }); + resources_changed = true; } snownet::Event::NewIceCandidate { connection, @@ -850,13 +840,17 @@ impl ClientState { }), snownet::Event::ConnectionEstablished(id) => { self.update_site_status_by_gateway(&id, Status::Online); - self.buffered_events - .push_back(ClientEvent::ResourcesChanged { - resources: self.resources(), - }); + resources_changed = true; } } } + + if resources_changed { + self.buffered_events + .push_back(ClientEvent::ResourcesChanged { + resources: self.resources(), + }); + } } fn update_site_status_by_gateway(&mut self, gateway_id: &GatewayId, status: Status) { diff --git a/rust/connlib/tunnel/src/gateway.rs b/rust/connlib/tunnel/src/gateway.rs index da6323227..2adb067d4 100644 --- a/rust/connlib/tunnel/src/gateway.rs +++ b/rust/connlib/tunnel/src/gateway.rs @@ -378,8 +378,7 @@ impl GatewayState { candidate, }); } - snownet::Event::ConnectionEstablished(_) - | snownet::Event::ConnectionsCleared(_) => {} + snownet::Event::ConnectionEstablished(_) | snownet::Event::ConnectionClosed(_) => {} } } } diff --git a/rust/snownet-tests/src/main.rs b/rust/snownet-tests/src/main.rs index 6ffb9029e..b8ecff5c0 100644 --- a/rust/snownet-tests/src/main.rs +++ b/rust/snownet-tests/src/main.rs @@ -400,7 +400,7 @@ impl Eventloop { } Some( snownet::Event::InvalidateIceCandidate { .. } - | snownet::Event::ConnectionsCleared { .. }, + | snownet::Event::ConnectionClosed { .. }, ) | None => {} }