Revert "refactor: retry with backoff"

This reverts commit 0b63bebb6c.
This commit is contained in:
Slinetrac
2025-10-17 20:14:15 +08:00
parent 98a52c5c33
commit 4e31dc8728

View File

@@ -17,23 +17,12 @@ use crate::{
}, },
}; };
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use backoff::backoff::Backoff;
use backoff::{Error as BackoffError, ExponentialBackoff};
use compact_str::CompactString; use compact_str::CompactString;
use flexi_logger::DeferredNow; use flexi_logger::DeferredNow;
use log::Level; use log::Level;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::{ use std::{error::Error, fmt, path::PathBuf, sync::Arc, time::Duration};
error::Error,
fmt,
path::PathBuf,
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
time::{Duration, Instant},
};
use tauri_plugin_mihomo::Error as MihomoError; use tauri_plugin_mihomo::Error as MihomoError;
use tauri_plugin_shell::ShellExt; use tauri_plugin_shell::ShellExt;
use tokio::time::sleep; use tokio::time::sleep;
@@ -967,25 +956,10 @@ impl CoreManager {
return; return;
} }
let max_wait = Duration::from_millis(3000); const MAX_ATTEMPTS: usize = 15;
let mut backoff_strategy = ExponentialBackoff { const WAIT_INTERVAL_MS: u64 = 200;
initial_interval: Duration::from_millis(200),
max_interval: Duration::from_millis(200),
max_elapsed_time: Some(max_wait),
multiplier: 1.0,
randomization_factor: 0.0,
..Default::default()
};
backoff_strategy.reset();
let attempts = Arc::new(AtomicUsize::new(0)); for attempt in 0..MAX_ATTEMPTS {
let attempts_for_retry = Arc::clone(&attempts);
let operation = || {
let attempts = Arc::clone(&attempts_for_retry);
async move {
let attempt = attempts.fetch_add(1, Ordering::Relaxed);
let mut manager = SERVICE_MANAGER.lock().await; let mut manager = SERVICE_MANAGER.lock().await;
if matches!(manager.current(), ServiceStatus::Ready) { if matches!(manager.current(), ServiceStatus::Ready) {
@@ -994,10 +968,10 @@ impl CoreManager {
info, info,
Type::Core, Type::Core,
"Service became ready for TUN after {} attempt(s)", "Service became ready for TUN after {} attempt(s)",
attempt + 1 attempt
); );
} }
return Ok(()); return;
} }
if attempt == 0 { if attempt == 0 {
@@ -1020,7 +994,6 @@ impl CoreManager {
attempt + 1, attempt + 1,
err err
); );
return Err(BackoffError::transient(err));
} }
} }
@@ -1031,33 +1004,23 @@ impl CoreManager {
"Service became ready for TUN after {} attempt(s)", "Service became ready for TUN after {} attempt(s)",
attempt + 1 attempt + 1
); );
return Ok(()); return;
} }
logging!( drop(manager);
debug,
Type::Core,
"Service not ready after attempt {}; retrying with backoff",
attempt + 1
);
Err(BackoffError::transient(anyhow!("Service not ready yet"))) if attempt + 1 == MAX_ATTEMPTS {
} let total_wait_ms = (MAX_ATTEMPTS as u64) * WAIT_INTERVAL_MS;
};
let wait_started = Instant::now();
if let Err(err) = backoff::future::retry(backoff_strategy, operation).await {
let total_attempts = attempts.load(Ordering::Relaxed);
let waited_ms = wait_started.elapsed().as_millis();
logging!( logging!(
warn, warn,
Type::Core, Type::Core,
"Service still not ready after waiting approximately {} ms ({} attempt(s)); falling back to sidecar mode: {}", "Service still not ready after waiting approximately {} ms; falling back to sidecar mode",
waited_ms, total_wait_ms
total_attempts,
err
); );
break;
}
sleep(Duration::from_millis(WAIT_INTERVAL_MS)).await;
} }
} }