mirror of
https://github.com/Yuyi-Oak/BlueArchiveToolkit.git
synced 2026-07-22 07:06:43 +08:00
feat(download): 多线程下载 + 重试指数退避(issue #17)
下载主循环由串行改为「并发下载 + 串行簿记」三段式: - Phase A:无网络前置校验(官方性/目标路径/建目录)+ 判定跳过/需下载, 非官方 URL 在任何下载前 fail-fast - Phase B:need-download 项经 scoped 线程池并发下载(默认并发 4, 可配 1..=256)。worker 只做只读 &self 的 pull_one(各 URL 独立 .part/目标文件),经 mpsc 把结果送回主线程;manifest/quarantine 簿记与进度回调全在主线程串行执行,无需加锁。首个失败或 should_cancel 置 cancel 标志,其余 worker 在任务边界停止 - Phase C:按 plan 顺序串行收尾——seed .hash 校验(顺序相关、可 fail-fast)+ 构建有序结果 fail-fast 与「不发布不完整资源」不变量保留;进度事件按 URL 配对但 顺序不再单调(并发下天然如此)。 curl 重试加指数退避(网络类 200ms→400ms→800ms…上限 5s;ETXTBSY 仍走 极短退避),并发下对官方 CDN 更礼貌;退避基值 cfg(test) 下为 0 不拖慢 单测。 并发度经 OfficialUpdateConfig.download_concurrency 贯通,CLI --download-concurrency 与 BAT_DOWNLOAD_CONCURRENCY 可配,.env 模板 与 USERGUIDE/CURRENT_STATUS/CHANGELOG 同步。 验证:新增并发正确性测试(并发 8:每 URL 恰一次 started+finished、 全部落盘)、并发度钳制、退避时长计算、CLI/env 解析单测;progress 排序测试改为顺序无关不变量;workspace 全测试(20 套件) + fmt + clippy --all-targets -D warnings 全绿。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -4957,6 +4957,8 @@ BAT_AUTO_DISCOVER=1
|
||||
# 正常检查间隔与失败重试间隔(秒)
|
||||
#BAT_INTERVAL_SECONDS=3600
|
||||
#BAT_ERROR_RETRY_SECONDS=60
|
||||
# 下载并发度(并行网络下载数,1~256;默认 4,对官方 CDN 礼貌)
|
||||
#BAT_DOWNLOAD_CONCURRENCY=4
|
||||
|
||||
# ---- 网络 ----
|
||||
# 显式代理 URL(支持 http/https/socks4/socks4a/socks5/socks5h)。
|
||||
@@ -5141,6 +5143,9 @@ fn apply_bat_env_overrides(
|
||||
if let Some(v) = value("BAT_UNZIP") {
|
||||
options.config.unzip_command = PathBuf::from(v);
|
||||
}
|
||||
if let Some(v) = value("BAT_DOWNLOAD_CONCURRENCY") {
|
||||
options.config.download_concurrency = parse_download_concurrency(&v)?;
|
||||
}
|
||||
if let Some(v) = value("BAT_PROXY") {
|
||||
options.config.curl_proxy = parse_proxy_config(&v)?;
|
||||
}
|
||||
@@ -5318,6 +5323,11 @@ fn parse_args_with_env(
|
||||
"--unzip" => {
|
||||
options.config.unzip_command = PathBuf::from(next_option_value(&mut args, &flag)?);
|
||||
}
|
||||
"--download-concurrency" => {
|
||||
let value = next_option_value(&mut args, &flag)?;
|
||||
options.config.download_concurrency = parse_download_concurrency(&value)?;
|
||||
options.sync_option_explicit = true;
|
||||
}
|
||||
"--dry-run" => {
|
||||
options.config.dry_run = true;
|
||||
options.sync_option_explicit = true;
|
||||
@@ -5656,6 +5666,7 @@ fn print_usage(binary: &str) {
|
||||
eprintln!(" --proxy <URL|auto|none> curl proxy override (default: auto from env)");
|
||||
eprintln!(" --no-proxy Force direct curl connections");
|
||||
eprintln!(" --unzip <PATH> unzip executable (default: unzip)");
|
||||
eprintln!(" --download-concurrency <N> Parallel downloads, 1..=256 (default: 4)");
|
||||
eprintln!(" --dry-run Do not write sync state");
|
||||
eprintln!(" --plan Include planned URLs in dry-run");
|
||||
eprintln!(" --force Force download/refresh");
|
||||
@@ -5730,6 +5741,17 @@ fn parse_platforms(value: &str) -> Result<Vec<PatchPlatform>, String> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// 解析下载并发度:正整数,钳制到 `1..=256`。
|
||||
fn parse_download_concurrency(value: &str) -> anyhow::Result<usize> {
|
||||
let parsed = value
|
||||
.parse::<usize>()
|
||||
.map_err(|error| anyhow::anyhow!("下载并发度无效:{error}"))?;
|
||||
if parsed == 0 {
|
||||
return Err(anyhow::anyhow!("下载并发度必须大于 0"));
|
||||
}
|
||||
Ok(parsed.min(256))
|
||||
}
|
||||
|
||||
fn parse_platform(value: &str) -> Result<PatchPlatform, String> {
|
||||
match value.to_ascii_lowercase().as_str() {
|
||||
"windows" | "win" => Ok(PatchPlatform::Windows),
|
||||
@@ -6012,6 +6034,51 @@ mod tests {
|
||||
assert_eq!(registry.get("task-1-2").unwrap().status, "succeeded");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_download_concurrency_flag_and_env() {
|
||||
// 默认值(与 OfficialUpdateConfig 默认一致)。
|
||||
assert_eq!(
|
||||
parse(&["bat"]).unwrap().config.download_concurrency,
|
||||
OfficialUpdateConfig::default().download_concurrency
|
||||
);
|
||||
// CLI 显式设置 + 上限钳制。
|
||||
assert_eq!(
|
||||
parse(&["bat", "--download-concurrency", "16"])
|
||||
.unwrap()
|
||||
.config
|
||||
.download_concurrency,
|
||||
16
|
||||
);
|
||||
assert_eq!(
|
||||
parse(&["bat", "--download-concurrency", "9999"])
|
||||
.unwrap()
|
||||
.config
|
||||
.download_concurrency,
|
||||
256
|
||||
);
|
||||
// 0 与非数字报错。
|
||||
assert!(parse(&["bat", "--download-concurrency", "0"]).is_err());
|
||||
assert!(parse(&["bat", "--download-concurrency", "abc"]).is_err());
|
||||
// 环境变量(含 .env)设置默认值;CLI 覆盖之。
|
||||
assert_eq!(
|
||||
parse_with_env(&["bat"], &[("BAT_DOWNLOAD_CONCURRENCY", "12")])
|
||||
.unwrap()
|
||||
.config
|
||||
.download_concurrency,
|
||||
12
|
||||
);
|
||||
assert_eq!(
|
||||
parse_with_env(
|
||||
&["bat", "--download-concurrency", "3"],
|
||||
&[("BAT_DOWNLOAD_CONCURRENCY", "12")]
|
||||
)
|
||||
.unwrap()
|
||||
.config
|
||||
.download_concurrency,
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_auto_discover_sync_args() {
|
||||
let options = parse(&[
|
||||
|
||||
Reference in New Issue
Block a user