智能下载
Turbo CDN 的智能下载模式自动选择最佳下载方式。
概述
智能下载是 Turbo CDN 的默认模式。它自动:
- 测试直接下载性能
- 测试 CDN 镜像性能
- 比较结果
- 选择最快的选项
- 使用最优方式下载
工作原理
决策流程
┌─────────────────────────────────────────────────────────────┐
│ 智能下载模式 │
├─────────────────────────────────────────────────────────────┤
│ 1. URL 分析 │
│ └─ 确定是否有可用的 CDN 优化 │
│ │
│ 2. 并行测试 │
│ ├─ 测试直接 URL(HEAD 请求) │
│ └─ 测试 CDN 镜像(HEAD 请求) │
│ │
│ 3. 性能比较 │
│ ├─ 比较延迟 │
│ └─ 考虑历史性能 │
│ │
│ 4. 方式选择 │
│ ├─ 如果 CDN 快 20% 以上 → 使用 CDN │
│ └─ 否则 → 使用直接下载 │
│ │
│ 5. 执行下载 │
│ └─ 使用选定的方式下载 │
└─────────────────────────────────────────────────────────────┘使用方法
CLI(默认)
bash
# 智能模式是默认的
turbo-cdn dl "https://github.com/user/repo/releases/download/v1.0/file.zip"
# 显式启用智能模式(与默认相同)
turbo-cdn dl "https://github.com/user/repo/releases/download/v1.0/file.zip" --smart
# 详细输出显示决策过程
turbo-cdn dl "https://github.com/user/repo/releases/download/v1.0/file.zip" --verbose库
rust
use turbo_cdn::*;
#[tokio::main]
async fn main() -> turbo_cdn::Result<()> {
let downloader = TurboCdn::new().await?;
// 智能下载(默认行为)
let result = downloader.download_smart("https://github.com/user/repo/releases/download/v1.0/file.zip").await?;
// 带详细输出
let result = downloader.download_smart_with_verbose(
"https://github.com/user/repo/releases/download/v1.0/file.zip",
true // verbose
).await?;
Ok(())
}下载模式
智能模式(默认)
自动选择最佳方式:
bash
turbo-cdn dl "https://example.com/file.zip"直接模式
绕过所有优化,直接从源下载:
bash
turbo-cdn dl "https://example.com/file.zip" --no-cdnrust
let result = downloader.download_direct_from_url("https://example.com/file.zip").await?;CDN 模式
强制 CDN 优化(跳过比较):
bash
turbo-cdn dl "https://example.com/file.zip" --force-cdnrust
let result = downloader.download_from_url("https://example.com/file.zip").await?;决策标准
何时选择 CDN
- CDN 延迟比直接下载快 20% 或更多
- 直接 URL 不可达或很慢
- 历史数据显示 CDN 性能更好
何时选择直接下载
- 直接 URL 很快(延迟 < 100ms)
- CDN 改善很小(< 20%)
- 该 URL 没有可用的 CDN 镜像
详细输出
使用 --verbose,您可以看到决策过程:
🧠 智能下载(自动选择最佳方式)- 默认模式
=========================================================
源 URL: https://github.com/user/repo/releases/download/v1.0/file.zip
模式: 智能模式(测试并选择最快方式)
✓ TurboCdn 以智能模式初始化(自动选择最佳方式)
测试下载方式...
直接 URL: 150ms 延迟
CDN (ghfast.top): 80ms 延迟
已选择: CDN (ghfast.top) - 快 47%
🎉 下载成功完成!
📁 ./file.zip
📊 25.50 MB (12.30 MB/s)性能优化
测试超时
智能模式使用短超时进行测试:
| 测试类型 | 超时 |
|---|---|
| HEAD 请求 | 3 秒 |
| 初始字节 | 5 秒 |
缓存
测试结果被缓存以避免重复测试:
- 缓存时长:5 分钟
- 缓存键:URL + 区域
- 失效:网络变化时
何时覆盖
使用 --no-cdn 当:
- 您知道直接源最快
- CDN 镜像已知过时
- 调试下载问题
使用 --force-cdn 当:
- 直接源被阻止或很慢
- 您想要一致的 CDN 行为
- 测试 CDN 性能
故障排除
智能模式选择了错误的方式
如果智能模式持续选择较慢的选项:
- 检查详细输出:查看实际延迟
- 清除缓存:结果可能过时
- 报告问题:可能表示有 bug
bash
# 查看详细决策过程
turbo-cdn dl "https://example.com/file.zip" --verbose
# 强制特定模式进行比较
turbo-cdn dl "https://example.com/file.zip" --no-cdn --verbose
turbo-cdn dl "https://example.com/file.zip" --force-cdn --verbose测试阶段慢
如果测试阶段耗时太长:
- 网络可能不稳定
- 考虑直接使用
--force-cdn或--no-cdn