forked from kenzuya/unshackle
feat(downloader): adaptive worker scaling, tail boost, session parity, resume fixes
Opt-in --adaptive-workers (AIMD scaling + tail range-split), request/rnet session parity, and fixes for resume poisoning, truncated-segment acceptance, 416 handling, and byte-range-slice corruption. Tests + docs.
This commit is contained in:
@@ -3,6 +3,7 @@ import os
|
||||
import statistics
|
||||
import threading
|
||||
import time
|
||||
from collections import deque
|
||||
from concurrent.futures import FIRST_COMPLETED, wait
|
||||
from concurrent.futures.thread import ThreadPoolExecutor
|
||||
from http.cookiejar import CookieJar
|
||||
@@ -38,6 +39,13 @@ SPEED_ROLLING_WINDOW = 10 # seconds of history to keep for speed calculation
|
||||
RANGE_PARALLEL_MIN_SIZE = 64 * 1024 * 1024
|
||||
RANGE_PARALLEL_PART_SIZE = 16 * 1024 * 1024
|
||||
|
||||
# Tail-end boost (opt-in, adaptive only): when only a few segments remain and workers
|
||||
# would otherwise idle, split each remaining segment into intra-segment range parts so
|
||||
# aggregate throughput holds to the end instead of collapsing to (few)x(per-connection).
|
||||
TAIL_BOOST_MAX_PER_CYCLE = 4 # cap on segments probed/split per drain cycle so blocking probes can't stall the loop
|
||||
TAIL_BOOST_MIN_SEGMENT_SIZE = 8 * 1024 * 1024 # don't probe+split segments smaller than this
|
||||
TAIL_BOOST_PART_SIZE = 4 * 1024 * 1024 # target size per tail part (smaller than the whole-file part size)
|
||||
|
||||
|
||||
def _adaptive_chunk_size(content_length: int) -> int:
|
||||
"""Pick chunk size based on content length. Benchmarked sweet spot: 512KB-4MB."""
|
||||
@@ -46,6 +54,140 @@ def _adaptive_chunk_size(content_length: int) -> int:
|
||||
return min(MAX_CHUNK, max(MIN_CHUNK, content_length // 4))
|
||||
|
||||
|
||||
# Adaptive worker controller (opt-in): AIMD hill-climb over segment concurrency
|
||||
ADAPTIVE_TICK = 4.0 # seconds between target re-evaluations
|
||||
ADAPTIVE_START = 6 # initial worker target, clamped to cap
|
||||
ADAPTIVE_STEP = 2 # additive increase per tick
|
||||
ADAPTIVE_MIN = 2 # never drop below this many workers
|
||||
ADAPTIVE_ERROR_BURST = 3 # errors within a tick that trigger multiplicative decrease
|
||||
ADAPTIVE_PLATEAU_GAIN = 1.10 # min speed ratio to justify keeping an increase
|
||||
|
||||
|
||||
class AdaptiveWorkerController:
|
||||
"""CDN-aware segment concurrency governor.
|
||||
|
||||
Pure and synchronous (no threads or sockets), so the AIMD policy is unit-testable.
|
||||
``update`` is evaluated once per ``tick`` and only after a full ``window`` of
|
||||
throughput samples exists: it ramps the worker target up while the CDN keeps
|
||||
rewarding extra concurrency, reverts the last increase on a plateau, and halves
|
||||
(with a one-tick cooldown) on an error burst. Target is always clamped to
|
||||
``[ADAPTIVE_MIN, cap]``.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, cap: int, start: Optional[int] = None, tick: float = ADAPTIVE_TICK, window: float = SPEED_ROLLING_WINDOW
|
||||
) -> None:
|
||||
self.cap = max(ADAPTIVE_MIN, cap)
|
||||
base = ADAPTIVE_START if start is None else start
|
||||
self.target = max(ADAPTIVE_MIN, min(base, self.cap))
|
||||
self.tick = tick
|
||||
self.window = window
|
||||
self._samples: deque[tuple[float, int]] = deque()
|
||||
self._first_sample: Optional[float] = None # first-ever sample time (survives pruning)
|
||||
self._errors = 0 # errors observed since the last tick evaluation
|
||||
self._last_tick: Optional[float] = None
|
||||
self._last_action: Optional[str] = None # "increase" once we have probed upward
|
||||
self._speed_before_increase = 0.0
|
||||
self._cooldown = False
|
||||
|
||||
def _prune(self, now: float) -> None:
|
||||
cutoff = now - self.window
|
||||
while self._samples and self._samples[0][0] < cutoff:
|
||||
self._samples.popleft()
|
||||
|
||||
def record_bytes(self, n: int, now: float) -> None:
|
||||
if self._first_sample is None:
|
||||
self._first_sample = now
|
||||
self._samples.append((now, n))
|
||||
self._prune(now)
|
||||
|
||||
def record_error(self, now: float) -> None:
|
||||
self._errors += 1
|
||||
|
||||
def speed(self, now: float) -> float:
|
||||
"""Rolling bytes/sec over the window."""
|
||||
self._prune(now)
|
||||
if not self._samples:
|
||||
return 0.0
|
||||
span = now - self._samples[0][0]
|
||||
if span <= 0:
|
||||
return 0.0
|
||||
return sum(n for _, n in self._samples) / span
|
||||
|
||||
def _full_window(self, now: float) -> bool:
|
||||
# true once we have been observing for at least a full window; measured from the
|
||||
# first-ever sample so continuous pruning (which keeps span just under window) does
|
||||
# not perpetually starve the controller of a "full" window in real timing
|
||||
return self._first_sample is not None and (now - self._first_sample) >= self.window
|
||||
|
||||
def update(self, now: float, inflight_plus_remaining: Optional[int] = None) -> int:
|
||||
"""Evaluate the policy at most once per tick; return the current target.
|
||||
|
||||
``inflight_plus_remaining`` (when given) is the count of segments still in flight or
|
||||
not yet started. If it is below the target there is not enough work to saturate the
|
||||
target, so the low measured speed reflects starvation rather than a plateau, and the
|
||||
probe/revert step is skipped this tick. Error-burst decreases still apply. Default ``None`` keeps the
|
||||
original behaviour for existing callers.
|
||||
"""
|
||||
if self._last_tick is None:
|
||||
self._last_tick = now
|
||||
return self.target
|
||||
if now - self._last_tick < self.tick:
|
||||
return self.target
|
||||
self._last_tick = now
|
||||
|
||||
# hold until a full window of throughput history has accumulated
|
||||
if not self._full_window(now):
|
||||
self._errors = 0
|
||||
return self.target
|
||||
|
||||
errors = self._errors
|
||||
self._errors = 0
|
||||
speed_now = self.speed(now)
|
||||
old = self.target
|
||||
|
||||
if self._cooldown:
|
||||
# one tick of quiet after a decrease before probing again
|
||||
self._cooldown = False
|
||||
self._last_action = None
|
||||
return self.target
|
||||
|
||||
reason: Optional[str] = None
|
||||
if errors >= ADAPTIVE_ERROR_BURST:
|
||||
self.target = max(ADAPTIVE_MIN, self.target // 2)
|
||||
self._cooldown = True
|
||||
self._last_action = None
|
||||
reason = "error_burst"
|
||||
elif inflight_plus_remaining is not None and inflight_plus_remaining < self.target:
|
||||
# tail guard: too little work to saturate the target, so a low measured speed
|
||||
# here reflects starvation rather than a plateau, so hold and skip the probe/revert this tick
|
||||
pass
|
||||
elif self._last_action == "increase" and speed_now < ADAPTIVE_PLATEAU_GAIN * self._speed_before_increase:
|
||||
# last probe upward did not pay off, so revert it and hold
|
||||
self.target = max(ADAPTIVE_MIN, self.target - ADAPTIVE_STEP)
|
||||
self._last_action = None
|
||||
reason = "plateau"
|
||||
elif self.target < self.cap:
|
||||
self._speed_before_increase = speed_now
|
||||
self.target = min(self.cap, self.target + ADAPTIVE_STEP)
|
||||
self._last_action = "increase"
|
||||
reason = "increase"
|
||||
else:
|
||||
self._last_action = None
|
||||
|
||||
self.target = max(ADAPTIVE_MIN, min(self.cap, self.target))
|
||||
if self.target != old:
|
||||
debug_logger = get_debug_logger()
|
||||
if debug_logger:
|
||||
debug_logger.log(
|
||||
level="DEBUG",
|
||||
operation="adaptive_workers",
|
||||
message=f"worker target {old} -> {self.target}",
|
||||
context={"old": old, "new": self.target, "speed_bps": round(speed_now), "reason": reason},
|
||||
)
|
||||
return self.target
|
||||
|
||||
|
||||
def _is_requests_session(session: Any) -> bool:
|
||||
"""Check if the session is a standard requests.Session (supports resp.raw)."""
|
||||
return isinstance(session, Session)
|
||||
@@ -63,6 +205,8 @@ def _probe_ranged(url: str, session: Any, **kwargs: Any) -> tuple[int, bool]:
|
||||
rest = {k: v for k, v in kwargs.items() if k != "headers"}
|
||||
if _is_rnet_session(session):
|
||||
rest.setdefault("read_timeout", READ_TIMEOUT)
|
||||
# let download() own segment retries, with no nested session-level retry loop
|
||||
rest.setdefault("max_retries", 0)
|
||||
else:
|
||||
rest.setdefault("timeout", (CONNECT_TIMEOUT, READ_TIMEOUT))
|
||||
try:
|
||||
@@ -85,6 +229,48 @@ def _probe_ranged(url: str, session: Any, **kwargs: Any) -> tuple[int, bool]:
|
||||
pass
|
||||
|
||||
|
||||
def _has_range_header(item: dict[str, Any]) -> bool:
|
||||
"""True when a URL item's per-request headers already carry a Range.
|
||||
|
||||
Such an item is itself a byte-range slice of a larger resource (DASH SegmentBase,
|
||||
HLS EXT-X-BYTERANGE); probing or part-mode would clobber that Range and fetch the
|
||||
wrong bytes, so ranged-parallel strategies must skip these items entirely.
|
||||
"""
|
||||
return any(k.lower() == "range" for k in (item.get("headers") or {}))
|
||||
|
||||
|
||||
def _plan_tail_parts(size: int, spare: int) -> list[tuple[int, int]]:
|
||||
"""Plan intra-segment range parts for a tail segment.
|
||||
|
||||
Returns inclusive ``[start, end]`` byte ranges covering ``[0, size)`` with no gaps or
|
||||
overlaps, split into at most ``spare`` parts of ~``TAIL_BOOST_PART_SIZE``. Returns an
|
||||
empty list when the segment is smaller than ``TAIL_BOOST_MIN_SEGMENT_SIZE`` (not worth a
|
||||
probe+split) or there are fewer than two spare workers. Pure, with no I/O, so testable.
|
||||
"""
|
||||
if size < TAIL_BOOST_MIN_SEGMENT_SIZE or spare < 2:
|
||||
return []
|
||||
n_parts = max(1, min(spare, math.ceil(size / TAIL_BOOST_PART_SIZE)))
|
||||
part_size = math.ceil(size / n_parts)
|
||||
return [
|
||||
(s, e)
|
||||
for i in range(n_parts)
|
||||
for s, e in [(i * part_size, min(size - 1, (i + 1) * part_size - 1))]
|
||||
if s <= e
|
||||
]
|
||||
|
||||
|
||||
def _tail_boost_engages(remaining: int, pending: int, target: int) -> bool:
|
||||
"""True when idle workers outnumber the remaining whole segments.
|
||||
|
||||
That is the tail condition where running one worker per remaining segment would leave
|
||||
workers idle, so splitting those segments into range parts keeps throughput up. Gating on
|
||||
idle capacity (not a fixed segment count) is what lets the boost fire despite ``remaining``
|
||||
draining in strides of the worker target. Pure, so the gate is unit-testable.
|
||||
"""
|
||||
spare = target - pending
|
||||
return remaining > 0 and spare >= 2 and remaining <= spare
|
||||
|
||||
|
||||
def _dispatch_parts(
|
||||
url: str,
|
||||
save_path: Path,
|
||||
@@ -110,6 +296,9 @@ def _dispatch_parts(
|
||||
f.truncate(total_size)
|
||||
|
||||
events: Queue[dict[str, Any]] = Queue()
|
||||
# local to this ranged download: a failed part must not poison the process-global
|
||||
# DOWNLOAD_CANCELLED (which would kill the sequential fallback and sibling tracks)
|
||||
abort = threading.Event()
|
||||
|
||||
def _worker(start: int, end: int) -> None:
|
||||
for ev in download(
|
||||
@@ -118,6 +307,7 @@ def _dispatch_parts(
|
||||
session=session,
|
||||
part_offset=start,
|
||||
part_end=end,
|
||||
abort=abort,
|
||||
**kwargs,
|
||||
):
|
||||
events.put(ev)
|
||||
@@ -158,7 +348,7 @@ def _dispatch_parts(
|
||||
exc = fut.exception()
|
||||
if exc:
|
||||
worker_error = True
|
||||
DOWNLOAD_CANCELLED.set()
|
||||
abort.set()
|
||||
raise exc
|
||||
|
||||
advance = 0
|
||||
@@ -194,6 +384,8 @@ def download(
|
||||
part_offset: Optional[int] = None,
|
||||
part_end: Optional[int] = None,
|
||||
claimed: Optional[Callable[[], bool]] = None,
|
||||
abort: Optional[threading.Event] = None,
|
||||
on_retry: Optional[Callable[[Exception], None]] = None,
|
||||
**kwargs: Any,
|
||||
) -> Generator[dict[str, Any], None, None]:
|
||||
"""
|
||||
@@ -224,6 +416,12 @@ def download(
|
||||
part_end: Inclusive end byte of the part. Required when `part_offset` is set.
|
||||
claimed: Optional predicate checked before every attempt; when it returns True
|
||||
the download stops silently (another worker already delivered this file).
|
||||
abort: Optional local cancel signal. When set, the download stops like a cancel
|
||||
(keeps its partial, returns silently, does not raise). Used by ranged-parallel
|
||||
downloads to abort sibling parts without touching the process-global cancel.
|
||||
on_retry: Optional callback invoked with the raised exception before each retry
|
||||
wait (not on cancel or final exhaustion). Used to feed CDN error signals to
|
||||
the adaptive worker controller. Callback errors are swallowed.
|
||||
kwargs: Any extra keyword arguments to pass to the session.get() call. Use this
|
||||
for one-time request changes like a header, cookie, or proxy. For example,
|
||||
to request Byte-ranges use e.g., `headers={"Range": "bytes=0-128"}`.
|
||||
@@ -268,6 +466,8 @@ def download(
|
||||
request_kwargs = dict(kwargs)
|
||||
if use_rnet:
|
||||
request_kwargs.setdefault("read_timeout", READ_TIMEOUT)
|
||||
# own segment retries here, suppressing RnetSession's nested retry loop
|
||||
request_kwargs.setdefault("max_retries", 0)
|
||||
else:
|
||||
request_kwargs.setdefault("timeout", (CONNECT_TIMEOUT, READ_TIMEOUT))
|
||||
if part_mode:
|
||||
@@ -280,6 +480,19 @@ def download(
|
||||
request_kwargs["headers"] = req_headers
|
||||
|
||||
stream = session.get(url, stream=True, **request_kwargs)
|
||||
|
||||
if (not part_mode) and resume_offset > 0 and stream.status_code == 416:
|
||||
# our Range started past the end (a stale or oversized .!dev from a prior run).
|
||||
# discard it and restart clean rather than raise_for_status → burn retries.
|
||||
try:
|
||||
stream.close()
|
||||
except Exception:
|
||||
pass
|
||||
tmp_file.unlink(missing_ok=True)
|
||||
resume_offset = 0
|
||||
written = 0
|
||||
continue
|
||||
|
||||
stream.raise_for_status()
|
||||
|
||||
resumed = (not part_mode) and resume_offset > 0 and stream.status_code == 206
|
||||
@@ -314,12 +527,14 @@ def download(
|
||||
else:
|
||||
file_mode = "ab" if resumed else "wb"
|
||||
file_buffering = 1_048_576
|
||||
# non-part downloads write sequentially and must NOT pre-allocate: the resume
|
||||
# offset is derived from the tmp file's size, so preallocating to content_length
|
||||
# would make an interrupted write look fully downloaded and poison the resume
|
||||
# (unsatisfiable Range on retry, silent zero-padded corruption). part_mode still
|
||||
# pre-truncates in _dispatch_parts because its workers seek to fixed offsets.
|
||||
with open(save_path if part_mode else tmp_file, file_mode, buffering=file_buffering) as f:
|
||||
if part_mode:
|
||||
f.seek(part_offset + written)
|
||||
elif not resumed and content_length > 0:
|
||||
f.truncate(content_length)
|
||||
f.seek(0)
|
||||
|
||||
_write = f.write
|
||||
|
||||
@@ -334,7 +549,7 @@ def download(
|
||||
_bytes_since_yield = 0
|
||||
emit_progress = (not segmented) or part_mode
|
||||
for chunk in chunks:
|
||||
if DOWNLOAD_CANCELLED.is_set():
|
||||
if DOWNLOAD_CANCELLED.is_set() or (abort is not None and abort.is_set()):
|
||||
break
|
||||
_write(chunk)
|
||||
download_size = len(chunk)
|
||||
@@ -362,18 +577,21 @@ def download(
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not part_mode and not resumed and content_length > 0 and written != content_length:
|
||||
f.truncate(written)
|
||||
|
||||
if DOWNLOAD_CANCELLED.is_set() and (not content_length or written < content_length):
|
||||
# cancelled mid-stream: keep the partial .!dev for resume, never finalize
|
||||
aborted = abort is not None and abort.is_set()
|
||||
if (DOWNLOAD_CANCELLED.is_set() or aborted) and (not content_length or written < content_length):
|
||||
# cancelled/aborted mid-stream: keep the partial for resume, never finalize,
|
||||
# and skip the size checks below (a short read here is expected, not an error)
|
||||
return
|
||||
|
||||
if part_mode:
|
||||
expected = part_end - part_offset + 1
|
||||
if written < expected:
|
||||
raise IOError(f"Failed to read part {part_offset}-{part_end}: got {written}/{expected}")
|
||||
elif not segmented and content_length and written < content_length:
|
||||
elif content_length and written < content_length:
|
||||
# applies to segments too: a connection FIN'd mid-body is a clean EOF, so the
|
||||
# chunk loop ends without raising, so without this check a truncated segment would
|
||||
# finalize as complete and corrupt the merge. content_length==0 (chunked/gzip)
|
||||
# is unknown-length and correctly skips the check.
|
||||
raise IOError(f"Failed to read {content_length} bytes from the track URI.")
|
||||
|
||||
if not part_mode:
|
||||
@@ -382,15 +600,26 @@ def download(
|
||||
if segmented:
|
||||
yield dict(advance=1)
|
||||
break
|
||||
except Exception:
|
||||
except Exception as exc:
|
||||
try:
|
||||
stream.close()
|
||||
except Exception:
|
||||
pass
|
||||
if DOWNLOAD_CANCELLED.is_set() or attempts == MAX_ATTEMPTS:
|
||||
if part_mode and not DOWNLOAD_CANCELLED.is_set():
|
||||
cancelled = DOWNLOAD_CANCELLED.is_set() or (abort is not None and abort.is_set())
|
||||
if cancelled or attempts == MAX_ATTEMPTS:
|
||||
# a hedged loser whose file was already delivered must not kill the batch
|
||||
if claimed is not None and claimed():
|
||||
return
|
||||
# cancel/abort is not an error, but a genuine retry-exhaustion is, so surface it
|
||||
# so the caller fails fast instead of hitting a missing segment at merge time
|
||||
if not cancelled:
|
||||
raise
|
||||
return
|
||||
if on_retry is not None:
|
||||
try:
|
||||
on_retry(exc)
|
||||
except Exception:
|
||||
pass
|
||||
if not part_mode:
|
||||
resume_offset = tmp_file.stat().st_size if tmp_file.exists() else 0
|
||||
time.sleep(RETRY_WAIT)
|
||||
@@ -406,6 +635,7 @@ def requests(
|
||||
proxy: Optional[str] = None,
|
||||
max_workers: Optional[int] = None,
|
||||
session: Optional[Any] = None,
|
||||
adaptive: bool = False,
|
||||
) -> Generator[dict[str, Any], None, None]:
|
||||
"""
|
||||
Download files with optimized I/O and adaptive chunk sizing.
|
||||
@@ -441,6 +671,11 @@ def requests(
|
||||
session: An optional requests.Session or RnetSession to use. If provided,
|
||||
it will be used directly (preserving TLS fingerprinting). If None, a new
|
||||
requests.Session with HTTPAdapter connection pooling will be created.
|
||||
adaptive: Opt-in CDN-aware dynamic segment concurrency for segmented (multi-URL)
|
||||
downloads. When True the per-track worker count starts moderate and ramps up
|
||||
or backs off based on measured throughput and CDN errors, capped at
|
||||
max_workers. When False (default) all segments are submitted upfront using a
|
||||
fixed worker count, matching the non-adaptive behaviour exactly.
|
||||
"""
|
||||
if not urls:
|
||||
raise ValueError("urls must be provided and not empty")
|
||||
@@ -522,6 +757,7 @@ def requests(
|
||||
"max_workers": max_workers,
|
||||
"has_proxy": bool(proxy),
|
||||
"session_type": type(session).__name__,
|
||||
"adaptive": adaptive,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -531,7 +767,7 @@ def requests(
|
||||
url_item = urls[0]
|
||||
try:
|
||||
ranged_used = False
|
||||
if max_workers > 1:
|
||||
if max_workers > 1 and not _has_range_header(url_item):
|
||||
total_size, supports_ranges = _probe_ranged(url_item["url"], session)
|
||||
if supports_ranges and total_size >= RANGE_PARALLEL_MIN_SIZE:
|
||||
try:
|
||||
@@ -581,6 +817,10 @@ def requests(
|
||||
seg_durations: list[float] = []
|
||||
hedged: set[int] = set()
|
||||
|
||||
controller = AdaptiveWorkerController(cap=max_workers) if adaptive else None
|
||||
# feed CDN error signals (429/timeout/reset) from each retrying segment into the controller
|
||||
on_retry_cb = (lambda _exc: controller.record_error(time.time())) if controller else None
|
||||
|
||||
def _download_worker(index: int, url_item: dict[str, Any], hedge: bool = False) -> None:
|
||||
item = dict(url_item)
|
||||
save_path = item.pop("save_path")
|
||||
@@ -596,12 +836,16 @@ def requests(
|
||||
# the .!dev suffix keeps strays visible to the manifest parsers' cleanup
|
||||
target = save_path.with_name(f"{save_path.name}.{'h' if hedge else 'p'}.!dev")
|
||||
if not hedge:
|
||||
seg_start[index] = time.time()
|
||||
# under seg_lock: the hedge scan iterates seg_start.items() on the main
|
||||
# thread, and a first-time key insert during that iteration raises
|
||||
with seg_lock:
|
||||
seg_start[index] = time.time()
|
||||
for event in download(
|
||||
session=session,
|
||||
segmented=segmented_batch,
|
||||
save_path=target,
|
||||
claimed=lambda: index in seg_done,
|
||||
on_retry=on_retry_cb,
|
||||
**item,
|
||||
):
|
||||
if "file_downloaded" in event:
|
||||
@@ -616,13 +860,159 @@ def requests(
|
||||
event = dict(event, file_downloaded=save_path)
|
||||
event_queue.put(event)
|
||||
|
||||
futures = [pool.submit(_download_worker, i, url) for i, url in enumerate(urls)]
|
||||
pending = set(futures)
|
||||
# not-yet-submitted segments; adaptive mode meters submission against the controller
|
||||
# target, while fixed mode (adaptive=False) submits every segment upfront
|
||||
remaining: deque[tuple[int, dict[str, Any]]] = deque(enumerate(urls))
|
||||
pending: set = set()
|
||||
|
||||
# tail boost bookkeeping (adaptive only): indices whose parts are in flight vs
|
||||
# indices probed and left for the normal single-worker path, each decided once
|
||||
tail_boosted: set[int] = set()
|
||||
tail_skipped: set[int] = set()
|
||||
|
||||
def _submit(count: int, only: Optional[set[int]] = None) -> None:
|
||||
# only=<set> submits just those indices (used in the tail window to release
|
||||
# segments the boost declined while holding back boost candidates); with no
|
||||
# `only`, submits the leading `count` segments in FIFO order.
|
||||
if count <= 0 or not remaining:
|
||||
return
|
||||
picked: list[tuple[int, dict[str, Any]]] = []
|
||||
for item in remaining:
|
||||
if count <= 0:
|
||||
break
|
||||
if only is not None and item[0] not in only:
|
||||
continue
|
||||
pending.add(pool.submit(_download_worker, item[0], item[1]))
|
||||
picked.append(item)
|
||||
count -= 1
|
||||
for item in picked:
|
||||
remaining.remove(item)
|
||||
|
||||
def _tail_part_worker(
|
||||
index: int,
|
||||
url: str,
|
||||
part_target: Path,
|
||||
save_path: Path,
|
||||
start: int,
|
||||
end: int,
|
||||
size: int,
|
||||
abort: threading.Event,
|
||||
parts_left: list[int],
|
||||
part_lock: threading.Lock,
|
||||
req_kwargs: dict[str, Any],
|
||||
) -> None:
|
||||
try:
|
||||
# part_mode download writes [start, end] into the pre-truncated target and
|
||||
# emits only byte-`advance`; swallow those, since tail segments report at segment
|
||||
# granularity (advance=1) like every other segment, once on finalize below
|
||||
for _ev in download(
|
||||
url=url,
|
||||
save_path=part_target,
|
||||
session=session,
|
||||
part_offset=start,
|
||||
part_end=end,
|
||||
abort=abort,
|
||||
on_retry=on_retry_cb,
|
||||
**req_kwargs,
|
||||
):
|
||||
pass
|
||||
except BaseException:
|
||||
abort.set() # stop sibling parts of this segment; a real failure fails the batch
|
||||
raise
|
||||
with part_lock:
|
||||
parts_left[0] -= 1
|
||||
finalize = parts_left[0] == 0
|
||||
if not finalize:
|
||||
return
|
||||
if DOWNLOAD_CANCELLED.is_set() or abort.is_set():
|
||||
return # cancelled/aborted: keep the partial target for the stray sweep, don't finalize
|
||||
with seg_lock:
|
||||
if index in seg_done:
|
||||
part_target.unlink(missing_ok=True)
|
||||
return
|
||||
seg_done.add(index)
|
||||
seg_durations.append(time.time() - seg_start[index])
|
||||
os.replace(part_target, save_path)
|
||||
event_queue.put(dict(file_downloaded=save_path, written=size))
|
||||
event_queue.put(dict(advance=1))
|
||||
|
||||
def _maybe_tail_boost(target: int) -> None:
|
||||
# Tail boost: near the end, split the few not-yet-started segments across idle
|
||||
# workers with intra-segment range parts so aggregate throughput doesn't collapse
|
||||
# to (few segments)x(per-connection speed). Only not-yet-started segments in
|
||||
# `remaining` are boosted (never mid-flight ones) to avoid entangling the main
|
||||
# loop; gated behind adaptive, though it could be decoupled from the controller.
|
||||
# Engage on idle capacity so the boost fires despite `remaining` draining in strides
|
||||
# of the worker target; probe at most TAIL_BOOST_MAX_PER_CYCLE per cycle so a run of
|
||||
# blocking range probes can't stall the drain loop.
|
||||
if DOWNLOAD_CANCELLED.is_set() or not _tail_boost_engages(len(remaining), len(pending), target):
|
||||
return
|
||||
boosted = 0
|
||||
for index, url_item in list(remaining):
|
||||
if boosted >= TAIL_BOOST_MAX_PER_CYCLE:
|
||||
break
|
||||
spare = target - len(pending)
|
||||
if spare < 2:
|
||||
break
|
||||
if index in tail_boosted or index in tail_skipped:
|
||||
continue
|
||||
item = dict(url_item)
|
||||
save_path = item.pop("save_path")
|
||||
if save_path.exists(): # finished in a previous run; let the normal fast-path handle it
|
||||
tail_skipped.add(index)
|
||||
continue
|
||||
if _has_range_header(item): # byte-range slice; part-mode would clobber its Range
|
||||
tail_skipped.add(index)
|
||||
continue
|
||||
req_kwargs = {k: v for k, v in item.items() if k != "url"}
|
||||
size, supports_ranges = _probe_ranged(item["url"], session, **req_kwargs)
|
||||
parts = _plan_tail_parts(size, spare) if supports_ranges else []
|
||||
if not parts:
|
||||
tail_skipped.add(index) # no ranges / too small / no capacity -> normal path
|
||||
continue
|
||||
try:
|
||||
remaining.remove((index, url_item))
|
||||
except ValueError:
|
||||
tail_skipped.add(index)
|
||||
continue
|
||||
# distinct .tp.!dev target so an interrupted boost never collides with the
|
||||
# normal worker's .p.!dev; the .!dev suffix keeps the stray visible to cleanup
|
||||
part_target = save_path.with_name(f"{save_path.name}.tp.!dev")
|
||||
with open(part_target, "wb") as f:
|
||||
f.truncate(size)
|
||||
seg_start[index] = time.time()
|
||||
tail_boosted.add(index)
|
||||
abort = threading.Event()
|
||||
parts_left = [len(parts)]
|
||||
part_lock = threading.Lock()
|
||||
for start, end in parts:
|
||||
pending.add(
|
||||
pool.submit(
|
||||
_tail_part_worker,
|
||||
index,
|
||||
item["url"],
|
||||
part_target,
|
||||
save_path,
|
||||
start,
|
||||
end,
|
||||
size,
|
||||
abort,
|
||||
parts_left,
|
||||
part_lock,
|
||||
req_kwargs,
|
||||
)
|
||||
)
|
||||
boosted += 1
|
||||
|
||||
if controller:
|
||||
_submit(controller.update(time.time()))
|
||||
else:
|
||||
_submit(len(remaining))
|
||||
|
||||
pending_advance = 0
|
||||
|
||||
try:
|
||||
while pending:
|
||||
while pending or remaining:
|
||||
# Drain queued events — batch advances, track bytes for speed
|
||||
while True:
|
||||
try:
|
||||
@@ -638,6 +1028,8 @@ def requests(
|
||||
written = event.get("written")
|
||||
if written:
|
||||
total_bytes += written
|
||||
if controller:
|
||||
controller.record_bytes(written, time.time())
|
||||
# Pass through other events (file_downloaded, total, etc.)
|
||||
yield event
|
||||
|
||||
@@ -646,25 +1038,50 @@ def requests(
|
||||
yield dict(advance=pending_advance)
|
||||
pending_advance = 0
|
||||
|
||||
# Yield speed every 0.5s (throttled to avoid spamming Rich)
|
||||
now = time.time()
|
||||
if now - last_speed_report > 0.5 and total_bytes > 0:
|
||||
elapsed = now - start_time
|
||||
if elapsed > 0:
|
||||
download_speed = math.ceil(total_bytes / elapsed)
|
||||
yield dict(downloaded=f"{filesize.decimal(download_speed)}/s")
|
||||
last_speed_report = now
|
||||
|
||||
# hedge stuck segments once spare workers exist (pending < max_workers)
|
||||
if len(pending) < max_workers and seg_durations and not DOWNLOAD_CANCELLED.is_set():
|
||||
# Adaptive: re-evaluate the worker target and top up in-flight futures
|
||||
target = max_workers
|
||||
if controller:
|
||||
target = controller.update(now, len(pending) + len(remaining))
|
||||
_maybe_tail_boost(target) # spend spare workers on the tail before topping up
|
||||
if len(pending) < target:
|
||||
# In the final stride (unstarted segments <= one worker target), hold back
|
||||
# boost candidates so idle workers accumulate for a range-split; only
|
||||
# release segments the boost already declined (too small / no range) so
|
||||
# nothing stalls. Otherwise top up in FIFO order as usual.
|
||||
if remaining and len(remaining) <= target:
|
||||
_submit(target - len(pending), only=tail_skipped)
|
||||
else:
|
||||
_submit(target - len(pending))
|
||||
|
||||
# Yield speed every 0.5s (throttled to avoid spamming Rich)
|
||||
if now - last_speed_report > 0.5:
|
||||
if controller:
|
||||
rolling = controller.speed(now)
|
||||
if rolling > 0:
|
||||
yield dict(downloaded=f"{filesize.decimal(math.ceil(rolling))}/s")
|
||||
last_speed_report = now
|
||||
elif total_bytes > 0:
|
||||
elapsed = now - start_time
|
||||
if elapsed > 0:
|
||||
download_speed = math.ceil(total_bytes / elapsed)
|
||||
yield dict(downloaded=f"{filesize.decimal(download_speed)}/s")
|
||||
last_speed_report = now
|
||||
|
||||
# hedge stuck segments once spare workers exist within the active target
|
||||
if len(pending) < target and seg_durations and not DOWNLOAD_CANCELLED.is_set():
|
||||
threshold = max(HEDGE_FACTOR * statistics.median(seg_durations), HEDGE_MIN_WAIT)
|
||||
with seg_lock:
|
||||
stuck = [
|
||||
i
|
||||
for i, started in seg_start.items()
|
||||
if i not in seg_done and i not in hedged and now - started > threshold
|
||||
if i not in seg_done
|
||||
and i not in hedged
|
||||
and i not in tail_boosted # boosted segments are already range-split
|
||||
and now - started > threshold
|
||||
]
|
||||
for i in stuck[: max_workers - len(pending)]:
|
||||
for i in stuck[: target - len(pending)]:
|
||||
hedged.add(i)
|
||||
pending.add(pool.submit(_download_worker, i, urls[i], True))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user