mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-18 09:52:13 +08:00
refactor(utils): unified retry and strategy-list helpers
This commit is contained in:
@@ -101,7 +101,7 @@ async fn should_update_profile(uid: &String, ignore_auto_update: bool) -> Result
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all, level = "info", fields(uid = %uid, strategy = tracing::field::Empty))]
|
||||
#[tracing::instrument(skip_all, level = "info", fields(uid = %uid, strategy = tracing::field::Empty, configured_elapsed = tracing::field::Empty, clash_elapsed = tracing::field::Empty, system_elapsed = tracing::field::Empty))]
|
||||
async fn perform_profile_update(
|
||||
uid: &String,
|
||||
url: &String,
|
||||
@@ -109,7 +109,7 @@ async fn perform_profile_update(
|
||||
option: Option<&PrfOption>,
|
||||
is_mannual_trigger: bool,
|
||||
) -> Result<()> {
|
||||
let mut merged_opt = PrfOption::merge(opt, option);
|
||||
let merged_opt = PrfOption::merge(opt, option);
|
||||
let profiles = Config::profiles().await;
|
||||
let profiles_arc = profiles.latest_arc();
|
||||
let profile_name = profiles_arc
|
||||
@@ -118,61 +118,74 @@ async fn perform_profile_update(
|
||||
.and_then(|item| item.name.clone())
|
||||
.unwrap_or_else(|| String::from("UnKnown Profile"));
|
||||
|
||||
match PrfItem::from_url(url, None, None, merged_opt.as_ref()).await {
|
||||
Ok(mut item) => {
|
||||
logging!(info, Type::Config, "[订阅更新] 更新订阅配置成功");
|
||||
profiles_update_item_safe(uid, &mut item).await?;
|
||||
return Ok(());
|
||||
}
|
||||
Err(err) => {
|
||||
logging!(
|
||||
warn,
|
||||
Type::Config,
|
||||
"[订阅更新] 直接更新失败: {},尝试使用Clash代理更新",
|
||||
mask_err(&format!("{err:#}"))
|
||||
);
|
||||
}
|
||||
#[derive(Clone, Copy)]
|
||||
enum Strategy {
|
||||
Configured,
|
||||
Clash,
|
||||
System,
|
||||
}
|
||||
|
||||
merged_opt.get_or_insert_with(PrfOption::default).self_proxy = Some(true);
|
||||
merged_opt.get_or_insert_with(PrfOption::default).with_proxy = Some(false);
|
||||
|
||||
match PrfItem::from_url(url, None, None, merged_opt.as_ref()).await {
|
||||
Ok(mut item) => {
|
||||
logging!(info, Type::Config, "[订阅更新] 使用 Clash代理 更新订阅配置成功");
|
||||
let result =
|
||||
crate::utils::retry::try_strategies(Strategy::Configured, [Strategy::Clash, Strategy::System], |strategy| {
|
||||
let mut options = merged_opt.clone();
|
||||
async move {
|
||||
let (name, success, failure) = match strategy {
|
||||
Strategy::Configured => (
|
||||
"configured",
|
||||
"[订阅更新] 更新订阅配置成功",
|
||||
"[订阅更新] 直接更新失败: {},尝试使用Clash代理更新",
|
||||
),
|
||||
Strategy::Clash => (
|
||||
"clash",
|
||||
"[订阅更新] 使用 Clash代理 更新订阅配置成功",
|
||||
"[订阅更新] Clash代理更新失败: {},尝试使用系统代理更新",
|
||||
),
|
||||
Strategy::System => (
|
||||
"system",
|
||||
"[订阅更新] 使用 系统代理 更新订阅配置成功",
|
||||
"[订阅更新] 系统代理更新失败: {},所有重试均已失败",
|
||||
),
|
||||
};
|
||||
match strategy {
|
||||
Strategy::Configured => {}
|
||||
Strategy::Clash | Strategy::System => {
|
||||
let option = options.get_or_insert_with(PrfOption::default);
|
||||
option.self_proxy = Some(matches!(strategy, Strategy::Clash));
|
||||
option.with_proxy = Some(matches!(strategy, Strategy::System));
|
||||
}
|
||||
}
|
||||
tracing::Span::current().record("strategy", name);
|
||||
let started = std::time::Instant::now();
|
||||
let result = PrfItem::from_url(url, None, None, options.as_ref()).await;
|
||||
let elapsed_field = match strategy {
|
||||
Strategy::Configured => "configured_elapsed",
|
||||
Strategy::Clash => "clash_elapsed",
|
||||
Strategy::System => "system_elapsed",
|
||||
};
|
||||
tracing::Span::current().record(elapsed_field, tracing::field::debug(started.elapsed()));
|
||||
match &result {
|
||||
Ok(_) => logging!(info, Type::Config, "{success}"),
|
||||
Err(error) => logging!(
|
||||
warn,
|
||||
Type::Config,
|
||||
"{}",
|
||||
failure.replace("{}", &mask_err(&format!("{error:#}")))
|
||||
),
|
||||
}
|
||||
result
|
||||
}
|
||||
})
|
||||
.await;
|
||||
let last_err = match result {
|
||||
Ok((strategy, mut item)) => {
|
||||
// A persistence error must not replay a successful download with another transport.
|
||||
profiles_update_item_safe(uid, &mut item).await?;
|
||||
handle::Handle::notice(NoticeStatus::UpdateWithClashProxy, profile_name.as_str());
|
||||
if !matches!(strategy, Strategy::Configured) {
|
||||
handle::Handle::notice(NoticeStatus::UpdateWithClashProxy, profile_name.as_str());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
Err(err) => {
|
||||
logging!(
|
||||
warn,
|
||||
Type::Config,
|
||||
"[订阅更新] Clash代理更新失败: {},尝试使用系统代理更新",
|
||||
mask_err(&format!("{err:#}"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
merged_opt.get_or_insert_with(PrfOption::default).self_proxy = Some(false);
|
||||
merged_opt.get_or_insert_with(PrfOption::default).with_proxy = Some(true);
|
||||
|
||||
let last_err = match PrfItem::from_url(url, None, None, merged_opt.as_ref()).await {
|
||||
Ok(mut item) => {
|
||||
logging!(info, Type::Config, "[订阅更新] 使用 系统代理 更新订阅配置成功");
|
||||
profiles_update_item_safe(uid, &mut item).await?;
|
||||
handle::Handle::notice(NoticeStatus::UpdateWithClashProxy, profile_name.as_str());
|
||||
return Ok(());
|
||||
}
|
||||
Err(err) => {
|
||||
logging!(
|
||||
warn,
|
||||
Type::Config,
|
||||
"[订阅更新] 系统代理更新失败: {},所有重试均已失败",
|
||||
mask_err(&format!("{err:#}"))
|
||||
);
|
||||
err
|
||||
}
|
||||
Err(error) => error,
|
||||
};
|
||||
|
||||
let last_err = mask_err(&last_err.to_string());
|
||||
|
||||
Reference in New Issue
Block a user