From 3a289ecff2df8e571543df0990f0418791445914 Mon Sep 17 00:00:00 2001 From: Michal Humpula Date: Sun, 1 Mar 2026 08:17:26 +0100 Subject: [PATCH] simplify ping handling --- src/main.rs | 81 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 86b6371..73fb319 100644 --- a/src/main.rs +++ b/src/main.rs @@ -127,6 +127,45 @@ async fn main() -> Result<()> { use state_machine::StateMachine; +async fn handle_ping_result( + result: pinger::PingResult, + interface_name: &str, + state_machine: &Arc>, + last_failover: &Arc>>>, + route_manager: &mut routing::RouteManager, + primary_gateway: &Ipv4Addr, + secondary_gateway: &Ipv4Addr, + config: &Config, +) -> Result<()> { + debug!("{} ping result: {}", interface_name, result); + let mut sm = state_machine.lock().await; + + // Add result to appropriate history based on interface + if interface_name == "primary" { + sm.add_primary_result(result); + } else { + sm.add_secondary_result(result); + } + + if let Some((old_state, new_state)) = sm.update_state() { + let mut last_failover_lock = last_failover.lock().await; + if new_state == state_machine::State::Fallback + && old_state != state_machine::State::Fallback + { + *last_failover_lock = Some(Utc::now()); + } + state_machine::handle_state_change( + new_state, + old_state, + route_manager, + primary_gateway, + secondary_gateway, + config, + )?; + } + Ok(()) +} + async fn main_service( config: Config, primary_gateway: Ipv4Addr, @@ -204,32 +243,30 @@ async fn main_service( tokio::select! { // Handle primary ping results Some(result) = primary_rx.recv() => { - debug!("Primary ping result: {}", result); - let mut sm = state_machine.lock().await; - sm.add_primary_result(result); - - if let Some((old_state, new_state)) = sm.update_state() { - let mut last_failover_lock = last_failover.lock().await; - if new_state == state_machine::State::Fallback && old_state != state_machine::State::Fallback { - *last_failover_lock = Some(Utc::now()); - } - state_machine::handle_state_change(new_state, old_state, &mut route_manager, &primary_gateway, &secondary_gateway, &config)?; - } + handle_ping_result( + result, + "primary", + &state_machine, + &last_failover, + &mut route_manager, + &primary_gateway, + &secondary_gateway, + &config, + ).await?; } // Handle secondary ping results Some(result) = secondary_rx.recv() => { - debug!("Secondary ping result: {}", result); - let mut sm = state_machine.lock().await; - sm.add_secondary_result(result); - - if let Some((old_state, new_state)) = sm.update_state() { - let mut last_failover_lock = last_failover.lock().await; - if new_state == state_machine::State::Fallback && old_state != state_machine::State::Fallback { - *last_failover_lock = Some(Utc::now()); - } - state_machine::handle_state_change(new_state, old_state, &mut route_manager, &primary_gateway, &secondary_gateway, &config)?; - } + handle_ping_result( + result, + "secondary", + &state_machine, + &last_failover, + &mut route_manager, + &primary_gateway, + &secondary_gateway, + &config, + ).await?; } // Handle shutdown signal