mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-13 14:22:08 +08:00
refactor(utils): migrate remaining bounded retry loops
This commit is contained in:
@@ -54,22 +54,34 @@ pub async fn check_singleton() -> Result<SingletonDisposition> {
|
||||
.context("failed to initialize singleton lock")?;
|
||||
if !try_lock_instance(&lock).context("failed to acquire singleton lock")? {
|
||||
let deadline = std::time::Instant::now() + Duration::from_secs(20);
|
||||
loop {
|
||||
if std::time::Instant::now() >= deadline {
|
||||
bail!("another app instance is starting");
|
||||
}
|
||||
// A restarting instance releases this lock only after spawning this
|
||||
// process, and its embedded server is already down, so waiting on
|
||||
// the notify probe alone can never promote this instance.
|
||||
if try_lock_instance(&lock).context("failed to acquire singleton lock")? {
|
||||
break;
|
||||
}
|
||||
if let Ok(record) = read_instance_record(&record_path)
|
||||
&& notify_existing_instance(&record).await
|
||||
{
|
||||
return Ok(SingletonDisposition::Secondary);
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
use crate::utils::retry::{RetryError, RetryPolicy, retry};
|
||||
let secondary = retry(
|
||||
RetryPolicy::fixed(
|
||||
std::num::NonZeroUsize::MIN.saturating_add(400),
|
||||
Duration::from_millis(50),
|
||||
),
|
||||
|_| async {
|
||||
if std::time::Instant::now() >= deadline {
|
||||
return Err(RetryError::Stop(anyhow::anyhow!("another app instance is starting")));
|
||||
}
|
||||
// A restarting primary releases the lock after spawning us, with its server down.
|
||||
if try_lock_instance(&lock)
|
||||
.context("failed to acquire singleton lock")
|
||||
.map_err(RetryError::Stop)?
|
||||
{
|
||||
return Ok(false);
|
||||
}
|
||||
if let Ok(record) = read_instance_record(&record_path)
|
||||
&& notify_existing_instance(&record).await
|
||||
{
|
||||
return Ok(true);
|
||||
}
|
||||
Err(RetryError::Retry(anyhow::anyhow!("another app instance is starting")))
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
if secondary {
|
||||
return Ok(SingletonDisposition::Secondary);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user