The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
fix(http): 分片重试换新连接 #665
XiaoYouChR07f30290ef50
diff --git a/features/http_pack/task.py b/features/http_pack/task.pyindex bdba177f..ced0bedd 100644--- a/features/http_pack/task.py+++ b/features/http_pack/task.py@@ -261,15 +261,9 @@ class HttpTaskStep(TaskStep):async def _runSubworker(self, subworker: HttpSubworker, fd: int, delay: float = 0) -> None:await asyncio.sleep(delay)- client = buildClient(emulation=self._emulation, userAgent=self.userAgent or None, readTimeout=STREAM_READ_TIMEOUT)- try:- await self._runSubworkerWith(subworker, fd, client)- finally:- client.close()-- async def _runSubworkerWith(self, subworker: HttpSubworker, fd: int, client) -> None:if subworker.end == SpecialFileSize.UNKNOWN:while True:+ client = buildClient(emulation=self._emulation, userAgent=self.userAgent or None, readTimeout=STREAM_READ_TIMEOUT)try:httpPos = self.httpByteOffset + subworker.positionheaders = {**self._effectiveHeaders, "range": f"bytes={httpPos}-", "accept-encoding": "identity"}@@ -301,9 +295,12 @@ class HttpTaskStep(TaskStep):raiselogger.opt(exception=e).error("下载分片失败,将在 5 秒后重试 {}", self.outputPath)await asyncio.sleep(5)+ finally:+ client.close()elif subworker.end == SpecialFileSize.NOT_SUPPORTED:while True:+ client = buildClient(emulation=self._emulation, userAgent=self.userAgent or None, readTimeout=STREAM_READ_TIMEOUT)try:ftruncate(fd, 0)subworker.receivedBytes = 0@@ -334,9 +331,12 @@ class HttpTaskStep(TaskStep):raiselogger.opt(exception=e).error("下载分片失败,将在 5 秒后重试 {}", self.outputPath)await asyncio.sleep(5)+ finally:+ client.close()else:while subworker.position <= subworker.end:+ client = buildClient(emulation=self._emulation, userAgent=self.userAgent or None, readTimeout=STREAM_READ_TIMEOUT)try:httpPos = self.httpByteOffset + subworker.positionhttpEnd = self.httpByteOffset + subworker.end@@ -381,6 +381,8 @@ class HttpTaskStep(TaskStep):raiselogger.opt(exception=e).error("下载分片失败,将在 5 秒后重试 {}", self.outputPath)await asyncio.sleep(5)+ finally:+ client.close()self._reassignSubworker()diff --git a/tests/test_http_subworker.py b/tests/test_http_subworker.pyindex 9dddf4eb..e1b7384a 100644--- a/tests/test_http_subworker.py+++ b/tests/test_http_subworker.py@@ -15,6 +15,7 @@ from pathlib import Pathimport pytestfrom aiohttp import web+from app.client import buildClient as realBuildClientfrom features.http_pack.task import HttpTask, HttpTaskStep, HttpSubworker, PermanentDownloadError, RangeNotSupportedErrorfrom app.models.task import TaskStatus, TaskErrorfrom tests.helpers import buildFileContent, buildRangeHandler, runStep@@ -639,6 +640,47 @@ class TestStallRecovery:assert hasStalledreleaseHang.set()+ async def test_dead_client_retries_on_a_new_one(self, server, tmp_path, monkeypatch):+ """A client that cannot fetch is closed, and the next attempt finishes the file on a new one."""+ content = buildFileContent(500)+ firstAttempt = True++ class DeadClient:+ isClosed = False++ async def get(self, *args, **kwargs):+ raise ConnectionError("dead connection")++ def close(self):+ self.isClosed = True++ deadClient = DeadClient()++ def buildClient(**kwargs):+ nonlocal firstAttempt+ if kwargs.get("readTimeout") is not None and firstAttempt:+ firstAttempt = False+ return deadClient+ return realBuildClient(**kwargs)++ monkeypatch.setattr("features.http_pack.task.buildClient", buildClient)++ _real_sleep = asyncio.sleep++ async def fast_sleep(n):+ await _real_sleep(0 if n >= 5 else min(n, 0.01))++ monkeypatch.setattr(asyncio, "sleep", fast_sleep)++ url = await server(buildRangeHandler(content))+ task, step = makeStep(url, tmp_path, fileSize=500, subworkerCount=1)+ task.setStatus(TaskStatus.RUNNING)++ await asyncio.wait_for(runStep(step), timeout=10)++ assert (tmp_path / "test.bin").read_bytes() == content+ assert deadClient.isClosed+class TestInitialStart: