From 3acc5793c5e7405233df698f02eb02251c5cb366 Mon Sep 17 00:00:00 2001 From: HttpRafa <60099368+HttpRafa@users.noreply.github.com> Date: Mon, 5 May 2025 18:08:54 +0200 Subject: [PATCH 1/3] fix: Fix --- controller/src/application/group.rs | 69 ++++++++++++-------- plugins/cloudflare/src/plugin/dns/manager.rs | 5 ++ 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/controller/src/application/group.rs b/controller/src/application/group.rs index 21a76c73..83b1cfbb 100644 --- a/controller/src/application/group.rs +++ b/controller/src/application/group.rs @@ -77,40 +77,53 @@ impl Group { } true }), - Stage::Queueing => true, + Stage::Queueing | Stage::Stopping => true, }); - if self.scaling.stop_empty_servers && self.servers.len() as u32 > target_count { - let mut to_stop = self.servers.len() as u32 - target_count; + // Count all servers that are active + let current_count = self + .servers + .iter() + .filter(|server| matches!(server.1 .1, Stage::Active)) + .count(); + + if self.scaling.stop_empty_servers && current_count as u32 > target_count { + let mut to_stop = current_count as u32 - target_count; let mut requests = vec![]; - self.servers.retain(|id, server| match &server.1 { - Stage::Active => servers.get_server_mut(id.uuid()).is_some_and(|server| { - if server.connected_users() == &0 { - if server.flags().should_stop() && to_stop > 0 { - debug!( + self.servers + .retain(|id, group_server| match &group_server.1 { + Stage::Active => servers.get_server_mut(id.uuid()).is_some_and(|server| { + if server.connected_users() == &0 { + if server.flags().should_stop() && to_stop > 0 { + debug!( "Server {} is empty and reached the timeout, stopping it...", server.id() ); - requests.push(StopRequest::new(None, server.id().clone())); - to_stop -= 1; + requests.push(StopRequest::new(None, server.id().clone())); + to_stop -= 1; + server.flags_mut().clear_stop(); + // Mark server as stopping + group_server.1 = Stage::Stopping; + } else if !server.flags().is_stop_set() { + debug!( + "Server {} is empty, starting stop timer...", + server.id() + ); + server + .flags_mut() + .replace_stop(*config.empty_server_timeout()); + } + } else if server.flags().is_stop_set() { + debug!( + "Server {} is no longer empty, clearing stop timer...", + server.id() + ); server.flags_mut().clear_stop(); - } else if !server.flags().is_stop_set() { - debug!("Server {} is empty, starting stop timer...", server.id()); - server - .flags_mut() - .replace_stop(*config.empty_server_timeout()); } - } else if server.flags().is_stop_set() { - debug!( - "Server {} is no longer empty, clearing stop timer...", - server.id() - ); - server.flags_mut().clear_stop(); - } - true - }), - Stage::Queueing => true, - }); + true + }), + Stage::Queueing | Stage::Stopping => true, + }); servers.schedule_stops(requests); } } @@ -181,6 +194,7 @@ impl Group { servers.cancel_start(id.uuid()); false } + Stage::Stopping => false, }); self.status = LifecycleStatus::Inactive; @@ -196,7 +210,7 @@ impl Group { .iter() .find_map(|(id, server)| match &server.1 { Stage::Active => servers.get_server(id.uuid()), - Stage::Queueing => None, + Stage::Queueing | Stage::Stopping => None, }) } @@ -244,6 +258,7 @@ struct GroupServer(usize, Stage); enum Stage { Queueing, Active, + Stopping, } impl GroupServer { diff --git a/plugins/cloudflare/src/plugin/dns/manager.rs b/plugins/cloudflare/src/plugin/dns/manager.rs index fd84c230..0b45d89a 100644 --- a/plugins/cloudflare/src/plugin/dns/manager.rs +++ b/plugins/cloudflare/src/plugin/dns/manager.rs @@ -170,6 +170,11 @@ impl Records { } } + if batch.deletes.is_empty() { + // No request to cloudflare required + continue; + } + count += batch.deletes.len(); backend.send_batch(zone_id, &batch); } From 53e59a4107d991f2b2db0b9d6312ea6e32e81a2a Mon Sep 17 00:00:00 2001 From: HttpRafa <60099368+HttpRafa@users.noreply.github.com> Date: Mon, 5 May 2025 18:15:02 +0200 Subject: [PATCH 2/3] fix: Useless warning --- controller/src/application/subscriber.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/controller/src/application/subscriber.rs b/controller/src/application/subscriber.rs index a86dfc87..be31e564 100644 --- a/controller/src/application/subscriber.rs +++ b/controller/src/application/subscriber.rs @@ -1,5 +1,4 @@ use anyhow::Result; -use simplelog::error; use tokio::sync::mpsc::{channel, Receiver, Sender}; use tokio_stream::wrappers::ReceiverStream; use tonic::Status; @@ -33,8 +32,8 @@ impl Subscriber { pub async fn send_network(&self, data: Result) -> bool { match &self.0 { Dispatch::Network(sender) => { - if let Err(error) = sender.send(data).await { - error!("Failed to send network message: {}", error); + if let Err(_) = sender.send(data).await { + // Channel closed false } else { true @@ -47,16 +46,16 @@ impl Subscriber { pub async fn send_message(&self, message: T) -> bool { match &self.0 { Dispatch::Network(sender) => { - if let Err(error) = sender.send(Ok(message)).await { - error!("Failed to send network message: {}", error); + if let Err(_) = sender.send(Ok(message)).await { + // Channel closed false } else { true } } Dispatch::Plugin(sender) => { - if let Err(error) = sender.send(Ok(message)).await { - error!("Failed to send plugin message: {}", error); + if let Err(_) = sender.send(Ok(message)).await { + // Channel closed false } else { true From af4a9ec527235ec8753e5c5fd8b4b0b3ad147ee5 Mon Sep 17 00:00:00 2001 From: HttpRafa <60099368+HttpRafa@users.noreply.github.com> Date: Mon, 5 May 2025 18:15:54 +0200 Subject: [PATCH 3/3] fix: Useless warning --- controller/src/application/subscriber.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/controller/src/application/subscriber.rs b/controller/src/application/subscriber.rs index be31e564..d4beca5f 100644 --- a/controller/src/application/subscriber.rs +++ b/controller/src/application/subscriber.rs @@ -32,7 +32,7 @@ impl Subscriber { pub async fn send_network(&self, data: Result) -> bool { match &self.0 { Dispatch::Network(sender) => { - if let Err(_) = sender.send(data).await { + if sender.send(data).await.is_err() { // Channel closed false } else { @@ -46,7 +46,7 @@ impl Subscriber { pub async fn send_message(&self, message: T) -> bool { match &self.0 { Dispatch::Network(sender) => { - if let Err(_) = sender.send(Ok(message)).await { + if sender.send(Ok(message)).await.is_err() { // Channel closed false } else { @@ -54,7 +54,7 @@ impl Subscriber { } } Dispatch::Plugin(sender) => { - if let Err(_) = sender.send(Ok(message)).await { + if sender.send(Ok(message)).await.is_err() { // Channel closed false } else {