Skip to content

Conversation

@whitecat346
Copy link
Member

Tip

这个是AI写的,AI真好用(

下载器功能重构PR说明

1. 主要修改内容

1.1 DownloadManager.cs

  • 修复了分段执行逻辑,确保所有初始分段都能被正确执行
  • 添加了完整的任务生命周期日志(开始/完成)
  • 优化了并行分段管理,提升下载效率
  • 完善了异常处理机制,增强系统稳定性

1.2 DownloadTask.cs

  • 修复了镜像轮换逻辑:将 & 运算符替换为 %,确保循环索引正确
  • 修正了拼写错误:SupprotsRangeSupportsRangeIMirrorSeletorIMirrorSelector
  • 添加了镜像切换和分段创建的详细日志
  • 优化了分段拆分算法,确保文件完整性

1.3 DownloadSegment.cs

  • 改进了文件流处理,添加 FileOptions.Asynchronous 提升异步IO性能
  • 完善了HTTP请求日志,包括范围请求和下载进度
  • 修复了缺少LINQ命名空间导致的编译错误
  • 添加了分段下载过程的详细跟踪日志

1.4 日志系统集成

  • 全面集成了 LogWrapper 日志系统,提供不同级别的日志输出
  • 覆盖了关键下载阶段:任务开始/结束、分段执行、镜像切换、错误信息
  • 支持通过日志快速定位和调试下载问题

2. 测试情况

重构后的下载器功能已通过基础测试验证,测试用例位于 PCL.Test\Net\Downloader\DownloadManagerTest.cs

[TestMethod]
public async Task DownloadTest()
{
	// 这里可以填入多个来源
    List<Uri> uris = [new Uri("https://disk.sample.cat/samples/zip/sample-1.zip")];
    var task = new DownloadTask(uris, @"G:\Tests\test.zip");

	// 现在只有一个镜像源选择器(((
    var manager = new DownloadManager(new FastMirrorSelector(new HttpClient()));
    manager.MaxParallelSegmentsPerTask = 8;

    await manager.DownloadAsync(task, CancellationToken.None);
}

后续建议

  1. 增加更多测试用例,包括:
    • 多镜像下载测试
    • 大文件下载测试
    • 网络异常情况下的恢复测试
  2. 考虑添加下载进度回调功能,便于UI层展示
  3. 优化分段算法,根据文件大小和网络条件动态调整分段数量

本次重构显著提升了下载器的稳定性、性能和可维护性,为用户提供了更加可靠的文件下载体验。

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the downloader functionality from a monolithic design into a modular architecture with cleaner separation of concerns. The refactor introduces mirror selection strategies, improved retry/resilience mechanisms using Polly, and comprehensive logging throughout the download lifecycle.

Key changes:

  • Replaced old Downloader, DownloadItem, and DownloadSegment classes with new DownloadManager, DownloadTask, and DownloadSegment implementations
  • Introduced IMirrorSelector interface with FastMirrorSelector implementation for intelligent mirror selection based on latency
  • Integrated Polly resilience pipeline for retry logic with exponential backoff and timeout handling

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 28 comments.

Show a summary per file
File Description
Net/Downloader/MirrorFailedException.cs New exception type for mirror-specific failures
Net/Downloader/IMirrorSelector.cs Interface defining mirror selection contract
Net/Downloader/FastMirrorSelector.cs Latency-based mirror selection implementation
Net/Downloader/DownloadTask.cs Manages download task state, mirrors, and segment splitting
Net/Downloader/DownloadSegmentStatus.cs Simplified enum for segment status tracking
Net/Downloader/DownloadSegment.cs Handles individual segment downloads with range request support
Net/Downloader/DownloadRangeNotSupportedException.cs Exception for servers not supporting range requests
Net/Downloader/DownloadManager.cs Orchestrates parallel segment downloads with resilience
Net/Downloader.cs Removed old implementation
Net/DownloadSegment.cs Removed old implementation
Net/DownloadItem.cs Removed old implementation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ruattd
Copy link
Contributor

ruattd commented Dec 28, 2025

建议直接写个新的,别改史山了....

@whitecat346
Copy link
Member Author

建议直接写个新的,别改史山了....

/cc @ruattd 其实你可以直接看文件更改,我这个就等于重写

@ruattd
Copy link
Contributor

ruattd commented Dec 28, 2025

其实你可以直接看文件更改,我这个就等于重写

我是指,你连架构也重写掉,把它改的符合 HTTP 规范一点(

@whitecat346
Copy link
Member Author

其实你可以直接看文件更改,我这个就等于重写

我是指,你连架构也重写掉,把它改的符合 HTTP 规范一点(

但是我懒欸(

@ruattd
Copy link
Contributor

ruattd commented Dec 28, 2025

但是我懒欸(

你都用 AI 了,以及我之前写那个完全没经过测试的,建议你测一下到底能不能用(

@whitecat346
Copy link
Member Author

但是我懒欸(

你都用 AI 了,以及我之前写那个完全没经过测试的,建议你测一下到底能不能用(

嗯?哪个?我不是很想(以及咱俩在GitHub这样聊天不太好啊)

Copy link
Contributor

@tangge233 tangge233 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

等待实战 :\

Copy link
Contributor

@wtommy932 wtommy932 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need it in my PR ( #131)
LGTM
wait for other's approval

@tangge233 tangge233 merged commit 66f27cc into PCL-Community:main Dec 31, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants