From 1bf11190d56dee51ff4934251ff88eef7dc98ba1 Mon Sep 17 00:00:00 2001 From: Razvan Dimescu Date: Sun, 22 Mar 2026 19:36:03 +0200 Subject: [PATCH] reduce network change detection to 5s with tiered polling LAN IP checked every 5s (cheap UDP socket call). Full upstream re-detection runs every 30s as safety net, or immediately when LAN IP changes. Reduces worst-case network switch recovery from 30s to 5s. Co-Authored-By: Claude Opus 4.6 (1M context) --- Cargo.lock | 2 +- src/main.rs | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf97456..e50ae9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -932,7 +932,7 @@ dependencies = [ [[package]] name = "numa" -version = "0.3.0" +version = "0.3.1" dependencies = [ "axum", "env_logger", diff --git a/src/main.rs b/src/main.rs index 5de9a15..5c58e24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -279,14 +279,17 @@ async fn main() -> numa::Result<()> { } async fn network_watch_loop(ctx: Arc) { - let mut interval = tokio::time::interval(Duration::from_secs(30)); + let mut tick: u64 = 0; + + let mut interval = tokio::time::interval(Duration::from_secs(5)); interval.tick().await; // skip immediate tick loop { interval.tick().await; + tick += 1; let mut changed = false; - // Check LAN IP change + // Check LAN IP change (every 5s — cheap, one UDP socket call) if let Some(new_ip) = numa::lan::detect_lan_ip() { let mut current_ip = ctx.lan_ip.lock().unwrap(); if new_ip != *current_ip { @@ -296,10 +299,10 @@ async fn network_watch_loop(ctx: Arc) { } } - // Check upstream change (only for auto-detected upstream) - if ctx.upstream_auto { + // Check upstream change every 30s or immediately on LAN IP change + // (heavier — spawns scutil/ipconfig, only when auto-detected) + if ctx.upstream_auto && (changed || tick.is_multiple_of(6)) { let dns_info = numa::system_dns::discover_system_dns(); - // Use detected upstream, or try DHCP-provided DNS, or fall back to Quad9 let new_addr = dns_info .default_upstream .or_else(numa::system_dns::detect_dhcp_dns)