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;
|
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(
|
async fn main_service(
|
||||||
config: Config,
|
config: Config,
|
||||||
primary_gateway: Ipv4Addr,
|
primary_gateway: Ipv4Addr,
|
||||||
@@ -204,32 +243,30 @@ async fn main_service(
|
|||||||
tokio::select! {
|
tokio::select! {
|
||||||
// Handle primary ping results
|
// Handle primary ping results
|
||||||
Some(result) = primary_rx.recv() => {
|
Some(result) = primary_rx.recv() => {
|
||||||
debug!("Primary ping result: {}", result);
|
handle_ping_result(
|
||||||
let mut sm = state_machine.lock().await;
|
result,
|
||||||
sm.add_primary_result(result);
|
"primary",
|
||||||
|
&state_machine,
|
||||||
if let Some((old_state, new_state)) = sm.update_state() {
|
&last_failover,
|
||||||
let mut last_failover_lock = last_failover.lock().await;
|
&mut route_manager,
|
||||||
if new_state == state_machine::State::Fallback && old_state != state_machine::State::Fallback {
|
&primary_gateway,
|
||||||
*last_failover_lock = Some(Utc::now());
|
&secondary_gateway,
|
||||||
}
|
&config,
|
||||||
state_machine::handle_state_change(new_state, old_state, &mut route_manager, &primary_gateway, &secondary_gateway, &config)?;
|
).await?;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle secondary ping results
|
// Handle secondary ping results
|
||||||
Some(result) = secondary_rx.recv() => {
|
Some(result) = secondary_rx.recv() => {
|
||||||
debug!("Secondary ping result: {}", result);
|
handle_ping_result(
|
||||||
let mut sm = state_machine.lock().await;
|
result,
|
||||||
sm.add_secondary_result(result);
|
"secondary",
|
||||||
|
&state_machine,
|
||||||
if let Some((old_state, new_state)) = sm.update_state() {
|
&last_failover,
|
||||||
let mut last_failover_lock = last_failover.lock().await;
|
&mut route_manager,
|
||||||
if new_state == state_machine::State::Fallback && old_state != state_machine::State::Fallback {
|
&primary_gateway,
|
||||||
*last_failover_lock = Some(Utc::now());
|
&secondary_gateway,
|
||||||
}
|
&config,
|
||||||
state_machine::handle_state_change(new_state, old_state, &mut route_manager, &primary_gateway, &secondary_gateway, &config)?;
|
).await?;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle shutdown signal
|
// Handle shutdown signal
|
||||||
|
|||||||
Reference in New Issue
Block a user