mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-04 22:52:23 +08:00
refactor: migrate backoff crate to backon (#6718)
Replace backoff 0.4.0 with backon 1.6.0 for retry logic.
This commit is contained in:
@@ -2,6 +2,7 @@ use crate::constants::files::DNS_CONFIG;
|
||||
use crate::{config::Config, process::AsyncHandler, utils::dirs};
|
||||
use anyhow::Error;
|
||||
use arc_swap::{ArcSwap, ArcSwapOption};
|
||||
use backon::{ConstantBuilder, Retryable as _};
|
||||
use clash_verge_logging::{Type, logging};
|
||||
use once_cell::sync::OnceCell;
|
||||
use reqwest_dav::list_cmd::{ListEntity, ListFile};
|
||||
@@ -166,40 +167,25 @@ impl WebDavClient {
|
||||
let client = self.get_client(Operation::Upload).await?;
|
||||
let webdav_path: String = format!("{}/{}", dirs::BACKUP_DIR, file_name).into();
|
||||
|
||||
// 读取文件并上传,如果失败尝试一次重试
|
||||
let file_content = fs::read(&file_path).await?;
|
||||
|
||||
// 添加超时保护
|
||||
let upload_result = timeout(
|
||||
Duration::from_secs(TIMEOUT_UPLOAD),
|
||||
client.put(&webdav_path, file_content.clone()),
|
||||
)
|
||||
.await;
|
||||
let backoff = ConstantBuilder::default()
|
||||
.with_delay(Duration::from_millis(500))
|
||||
.with_max_times(1);
|
||||
|
||||
match upload_result {
|
||||
Err(_) => {
|
||||
logging!(warn, Type::Backup, "Warning: Upload timed out, retrying once");
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
timeout(
|
||||
Duration::from_secs(TIMEOUT_UPLOAD),
|
||||
client.put(&webdav_path, file_content),
|
||||
)
|
||||
.await??;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Ok(Err(e)) => {
|
||||
logging!(warn, Type::Backup, "Warning: Upload failed, retrying once: {e}");
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
timeout(
|
||||
Duration::from_secs(TIMEOUT_UPLOAD),
|
||||
client.put(&webdav_path, file_content),
|
||||
)
|
||||
.await??;
|
||||
Ok(())
|
||||
}
|
||||
Ok(Ok(_)) => Ok(()),
|
||||
}
|
||||
(|| async {
|
||||
timeout(
|
||||
Duration::from_secs(TIMEOUT_UPLOAD),
|
||||
client.put(&webdav_path, file_content.clone()),
|
||||
)
|
||||
.await??;
|
||||
Ok::<(), Error>(())
|
||||
})
|
||||
.retry(backoff)
|
||||
.notify(|err, dur| {
|
||||
logging!(warn, Type::Backup, "Upload failed: {err}, retrying in {dur:?}");
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn download(&self, filename: String, storage_path: PathBuf) -> Result<(), Error> {
|
||||
|
||||
Reference in New Issue
Block a user