simplify ping handling
This commit is contained in:
81
src/main.rs
81
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<tokio::sync::Mutex<StateMachine>>,
|
||||
last_failover: &Arc<tokio::sync::Mutex<Option<chrono::DateTime<Utc>>>>,
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user