支持断点续传#4558
Conversation
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request implements HTTP download resuming by introducing HttpResumeContext and updating FetchTask to handle range requests and 206 Partial Content responses. It refactors the Context architecture to include a reset() method, migrates file operations to FileChannel for better control, and introduces UrlResponseInfo to decouple caching logic from the HttpResponse class. Feedback was provided regarding the progress reporting logic, noting that using local request counters during a resumed download could cause the progress bar to reset incorrectly, and a specific fix was suggested to use the resume context for accurate progress tracking.
| if (resume != null) | ||
| resume.countUncompressed += len; |
There was a problem hiding this comment.
在断点续传的情况下,目前的进度更新逻辑(位于第 242 行,虽然不在本次 diff 中直接修改,但受本次修改影响)会导致进度条重置。counter.downloaded 仅代表当前 HTTP 请求已下载的字节数,而 contentLength 在 206 Partial Content 响应中仅代表剩余待下载的字节数。为了提供正确的进度反馈,应当使用 resume 上下文中的总长度和累计已下载字节数。建议修改第 242 行为:
updateProgress(resume != null ? resume.countUncompressed : counter.downloaded, resume != null ? resume.contentLength : contentLength);There was a problem hiding this comment.
Pull request overview
该 PR 在 FetchTask 的 HTTP 下载重试流程中引入断点续传能力:当下载在中途失败时,后续重试会尝试携带 Range 请求从已写入的位置继续下载(在满足服务器与响应头条件时),以减少重复流量与重下成本。
Changes:
- 在
FetchTask.downloadHttp中新增基于Accept-Ranges / Content-Range / Last-Modified等条件的续传判定与重试续传流程,并为可重试的Context增加reset()机制。 - 缓存相关接口从直接依赖
HttpResponse调整为传递UrlResponseInfo(URI + headers),降低耦合并适配新流程。 - 新增
FetchTaskTest,覆盖校验失败不覆盖目标文件、无效Content-Range触发全量回退、有效Content-Range成功续传等场景。
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| HMCLCore/src/test/java/org/jackhuang/hmcl/task/FetchTaskTest.java | 新增本地 HTTP 服务器测试,验证重试续传/回退与校验行为 |
| HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/UrlResponseInfo.java | 新增用于缓存层的响应元信息载体(URI + headers) |
| HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java | 增加从 HttpResponse 提取 ResponseInfo 的工具方法 |
| HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/ContentEncoding.java | 将 fromResponse 改为基于 HttpHeaders 的 fromHeaders |
| HMCLCore/src/main/java/org/jackhuang/hmcl/util/CacheRepository.java | 缓存写入接口改为接收 UrlResponseInfo,避免直接依赖 HttpResponse |
| HMCLCore/src/main/java/org/jackhuang/hmcl/task/GetTask.java | Context 支持 reset(),并适配缓存接口变更 |
| HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java | Context 支持 reset();写入改为 FileChannel 以支持多次重试/续写;适配缓存接口 |
| HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java | 核心:实现 HTTP 下载重试的断点续传逻辑,新增 Context.reset() 与续传上下文 |
| HMCLCore/src/main/java/org/jackhuang/hmcl/task/CacheFileTask.java | Context 支持 reset();适配缓存接口变更 |
| HMCLCore/src/main/java/org/jackhuang/hmcl/auth/offline/Skin.java | Context 支持 reset();适配缓存接口变更 |
| HMCL/src/main/java/org/jackhuang/hmcl/terracotta/TerracottaBundle.java | 包装下载 Context 时转发 reset() 并修正 withResult 行为 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request implements HTTP download resume (breakpoint transmission) support and refactors the download task architecture. Key changes include the introduction of HttpResumeContext to track progress across retries, the addition of a reset() method to the Context interface for clean task restarts, and a transition from OutputStream to FileChannel in file-based tasks. Additionally, it decouples caching logic from the HttpResponse object using a new UrlResponseInfo record and adds unit tests for download retry and resume behaviors. Feedback suggests improving the accuracy of progress reporting during resumed downloads and explicitly resetting the file channel position after truncation to ensure robustness.
| if (resume != null) | ||
| resume.countUncompressed += len; |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for resuming HTTP downloads by implementing partial content handling using Range and If-Range headers. It adds an HttpResumeContext to FetchTask to manage download state and introduces a reset() method to the Context class to handle download restarts. File-based tasks now utilize FileChannel for efficient data management and truncation. Additionally, the caching layer was refactored to use a new UrlResponseInfo record, and comprehensive unit tests were added to verify various resume and retry scenarios. I have no feedback to provide.
在下载失败重试时尝试从失败处继续下载,而不是从头开始下载。