forked from kenzuya/unshackle
feat(dl): add --speed-limit and serve global_speed_limit
Shared token bucket across all download threads; decimal suffixes (5M = 5.0 MB/s) or plain bytes/sec
This commit is contained in:
@@ -45,6 +45,7 @@ from unshackle.core.config import config, resolve_cdm_name, resolve_decryption
|
||||
from unshackle.core.console import GradientPulseBarColumn, SyncLive, console
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED, DOWNLOAD_LICENCE_ONLY, AnyTrack, context_settings
|
||||
from unshackle.core.credential import Credential
|
||||
from unshackle.core.downloaders import format_speed, parse_speed_limit, set_speed_limit
|
||||
from unshackle.core.drm import DRM_T, ClearKeyCENC, MonaLisa, PlayReady, Widevine
|
||||
from unshackle.core.events import events
|
||||
from unshackle.core.music import (
|
||||
@@ -691,6 +692,16 @@ class dl:
|
||||
help="Max workers/threads to download with per-track. Default depends on the downloader.",
|
||||
)
|
||||
@click.option("--downloads", type=int, default=1, help="Amount of tracks to download concurrently.")
|
||||
@click.option(
|
||||
"--speed-limit",
|
||||
"speed_limit",
|
||||
type=str,
|
||||
default=None,
|
||||
metavar="SPEED",
|
||||
help="Cap total download speed across all downloads combined, e.g. 500k, 5M, 1.5G or plain "
|
||||
"bytes/sec (5M = 5.0 MB/s; bytes, not bits). "
|
||||
"'off' disables a limit configured in the dl: config block.",
|
||||
)
|
||||
@click.option(
|
||||
"-o",
|
||||
"--output",
|
||||
@@ -1258,6 +1269,7 @@ class dl:
|
||||
downloads: int,
|
||||
worst: bool,
|
||||
best_available: bool,
|
||||
speed_limit: Optional[str] = None,
|
||||
split_audio: Optional[bool] = None,
|
||||
merge_video: Optional[bool] = None,
|
||||
real_video_bitrate: bool = False,
|
||||
@@ -1275,6 +1287,15 @@ class dl:
|
||||
if skip_dl:
|
||||
DOWNLOAD_LICENCE_ONLY.set()
|
||||
|
||||
try:
|
||||
speed_limit_bps = parse_speed_limit(speed_limit)
|
||||
except ValueError as e:
|
||||
self.log.error(str(e))
|
||||
sys.exit(1)
|
||||
set_speed_limit(speed_limit_bps)
|
||||
if speed_limit_bps:
|
||||
self.log.info(f"Speed limit: {format_speed(speed_limit_bps)}")
|
||||
|
||||
if export:
|
||||
config.directories.exports.mkdir(parents=True, exist_ok=True)
|
||||
export_path = config.directories.exports / f"export_{self.service}_{int(time.time())}.json"
|
||||
|
||||
@@ -10,6 +10,7 @@ from unshackle.core.api import cors_middleware, setup_routes, setup_swagger
|
||||
from unshackle.core.api.compression import compression_middleware
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.constants import context_settings
|
||||
from unshackle.core.downloaders import format_speed, parse_speed_limit, set_speed_limit
|
||||
|
||||
|
||||
@click.command(
|
||||
@@ -146,6 +147,14 @@ def serve(
|
||||
if remote_only:
|
||||
api_only = True
|
||||
|
||||
try:
|
||||
global_speed_limit = parse_speed_limit(config.serve.get("global_speed_limit"))
|
||||
except ValueError as e:
|
||||
raise click.ClickException(f"serve.global_speed_limit: {e}")
|
||||
if global_speed_limit:
|
||||
set_speed_limit(global_speed_limit, lock=True)
|
||||
log.info(f"Global speed limit: {format_speed(global_speed_limit)} (shared by all jobs)")
|
||||
|
||||
global_services = config.serve.get("services")
|
||||
if global_services:
|
||||
log.info(f"Global service allowlist: {', '.join(global_services)}")
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
from .requests import requests
|
||||
from .requests import format_speed, parse_speed_limit, requests, set_speed_limit
|
||||
|
||||
__all__ = ("requests",)
|
||||
__all__ = ("requests", "format_speed", "parse_speed_limit", "set_speed_limit")
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
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
|
||||
@@ -42,6 +44,104 @@ RANGE_PARALLEL_MIN_SIZE = 64 * 1024 * 1024
|
||||
RANGE_PARALLEL_PART_SIZE = 16 * 1024 * 1024
|
||||
|
||||
|
||||
class SpeedWindow:
|
||||
"""Rolling-window download rate over the last SPEED_ROLLING_WINDOW seconds."""
|
||||
|
||||
def __init__(self, start: float) -> None:
|
||||
# zero-byte seed keeps early readings from exceeding the true rate
|
||||
self.samples: deque[tuple[float, int]] = deque([(start, 0)])
|
||||
|
||||
def rate(self, now: float, total: int) -> Optional[float]:
|
||||
self.samples.append((now, total))
|
||||
while now - self.samples[0][0] > SPEED_ROLLING_WINDOW:
|
||||
self.samples.popleft()
|
||||
span = now - self.samples[0][0]
|
||||
delta = total - self.samples[0][1]
|
||||
if span <= 0 or delta <= 0:
|
||||
return None
|
||||
return delta / span
|
||||
|
||||
|
||||
class TokenBucket:
|
||||
"""Aggregate byte-rate limiter shared by every download thread."""
|
||||
|
||||
def __init__(self, rate: float) -> None:
|
||||
self.rate = rate
|
||||
self.capacity = rate
|
||||
self.tokens = 0.0
|
||||
self.ts = time.monotonic()
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def consume(self, n: int) -> None:
|
||||
# tokens may go negative; waiting for n > capacity would deadlock
|
||||
with self.lock:
|
||||
now = time.monotonic()
|
||||
self.tokens = min(self.capacity, self.tokens + (now - self.ts) * self.rate)
|
||||
self.ts = now
|
||||
self.tokens -= n
|
||||
wait = -self.tokens / self.rate if self.tokens < 0 else 0.0
|
||||
if wait > 0:
|
||||
DOWNLOAD_CANCELLED.wait(wait)
|
||||
|
||||
|
||||
_speed_limiter: Optional[TokenBucket] = None
|
||||
_speed_limit_locked = False
|
||||
|
||||
SPEED_UNITS = {
|
||||
"": 1,
|
||||
"b": 1,
|
||||
"k": 1_000,
|
||||
"kb": 1_000,
|
||||
"m": 1_000_000,
|
||||
"mb": 1_000_000,
|
||||
"g": 1_000_000_000,
|
||||
"gb": 1_000_000_000,
|
||||
}
|
||||
|
||||
|
||||
def parse_speed_limit(value: Union[str, int, float, None]) -> Optional[float]:
|
||||
"""
|
||||
Parse a human-readable speed limit into bytes/sec, or None for unlimited.
|
||||
|
||||
Accepts plain numbers (exact bytes/sec) or a decimal suffix with optional
|
||||
/s, case-insensitive: "500k", "5M", "10MB/s", "1.5G", 5000000. Suffixes
|
||||
match the displayed speeds (5M = 5.0 MB/s); values are bytes, not bits.
|
||||
"", 0, "off", "none" and "unlimited" mean no limit.
|
||||
"""
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, (int, float)):
|
||||
if not math.isfinite(value) or value < 0:
|
||||
raise ValueError(f"Invalid speed limit {value!r}: must be a positive number of bytes/sec.")
|
||||
return float(value) or None
|
||||
text = value.strip().lower().removesuffix("/s")
|
||||
if text in ("", "0", "off", "none", "unlimited"):
|
||||
return None
|
||||
match = re.fullmatch(r"(\d*\.?\d+)\s*([kmg]b?|b)?", text)
|
||||
if not match:
|
||||
raise ValueError(f"Invalid speed limit {value!r}: use e.g. 500k, 5M, 1.5G, plain bytes/sec, or 'off'.")
|
||||
return float(match.group(1)) * SPEED_UNITS[match.group(2) or ""]
|
||||
|
||||
|
||||
def format_speed(bytes_per_sec: float) -> str:
|
||||
"""Format a byte rate in the same decimal units the progress bars show."""
|
||||
return f"{filesize.decimal(int(bytes_per_sec))}/s"
|
||||
|
||||
|
||||
def set_speed_limit(bytes_per_sec: Optional[float], lock: bool = False) -> None:
|
||||
"""
|
||||
Set (or clear) the global download speed limit shared by all threads.
|
||||
|
||||
A locked limit (serve's global_speed_limit) wins: later unlocked calls,
|
||||
like the per-job one in dl.result(), become no-ops for the process.
|
||||
"""
|
||||
global _speed_limiter, _speed_limit_locked
|
||||
if _speed_limit_locked and not lock:
|
||||
return
|
||||
_speed_limit_locked = lock
|
||||
_speed_limiter = TokenBucket(bytes_per_sec) if bytes_per_sec else None
|
||||
|
||||
|
||||
def _adaptive_chunk_size(content_length: int) -> int:
|
||||
"""Pick chunk size based on content length. Benchmarked sweet spot: 512KB-4MB."""
|
||||
if content_length <= 0:
|
||||
@@ -132,7 +232,8 @@ def _dispatch_parts(
|
||||
yield {"total": total_size}
|
||||
|
||||
total_bytes = 0
|
||||
start_time = last_report = time.time()
|
||||
last_report = time.time()
|
||||
speed_window = SpeedWindow(last_report)
|
||||
completed = False
|
||||
worker_error = False
|
||||
|
||||
@@ -153,7 +254,9 @@ def _dispatch_parts(
|
||||
|
||||
now = time.time()
|
||||
if now - last_report > 0.5 and total_bytes > 0:
|
||||
yield {"downloaded": f"{filesize.decimal(math.ceil(total_bytes / (now - start_time)))}/s"}
|
||||
rate = speed_window.rate(now, total_bytes)
|
||||
if rate:
|
||||
yield {"downloaded": f"{filesize.decimal(math.ceil(rate))}/s"}
|
||||
last_report = now
|
||||
|
||||
done, pending = wait(pending, timeout=0.1, return_when=FIRST_COMPLETED)
|
||||
@@ -300,7 +403,10 @@ def download(
|
||||
except ValueError:
|
||||
content_length = 0
|
||||
|
||||
limiter = _speed_limiter
|
||||
chunk_size = _adaptive_chunk_size(content_length)
|
||||
if limiter:
|
||||
chunk_size = min(chunk_size, max(8192, int(limiter.rate / 4)))
|
||||
total_size = (resume_offset + content_length) if resumed and content_length > 0 else content_length
|
||||
|
||||
if not segmented and not part_mode:
|
||||
@@ -350,6 +456,8 @@ def download(
|
||||
except Exception:
|
||||
pass
|
||||
return
|
||||
if limiter is not None:
|
||||
limiter.consume(len(chunk))
|
||||
_write(chunk)
|
||||
download_size = len(chunk)
|
||||
written += download_size
|
||||
@@ -543,7 +651,7 @@ def requests(
|
||||
url_item = urls[0]
|
||||
try:
|
||||
ranged_used = False
|
||||
if max_workers > 1:
|
||||
if max_workers > 1 and _speed_limiter is None:
|
||||
total_size, supports_ranges = _probe_ranged(url_item["url"], session)
|
||||
if supports_ranges and total_size >= RANGE_PARALLEL_MIN_SIZE:
|
||||
try:
|
||||
@@ -581,6 +689,7 @@ def requests(
|
||||
total_bytes = 0
|
||||
start_time = time.time()
|
||||
last_speed_report = start_time
|
||||
speed_window = SpeedWindow(start_time)
|
||||
last_hedge_check = 0.0
|
||||
hedge_median = 0.0 # cached; recomputed only when seg_durations grows
|
||||
hedge_median_len = 0
|
||||
@@ -664,10 +773,9 @@ def requests(
|
||||
# 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")
|
||||
rate = speed_window.rate(now, total_bytes)
|
||||
if rate:
|
||||
yield dict(downloaded=f"{filesize.decimal(math.ceil(rate))}/s")
|
||||
last_speed_report = now
|
||||
|
||||
# hedge stuck segments once spare workers exist (pending < max_workers);
|
||||
@@ -675,6 +783,7 @@ def requests(
|
||||
if (
|
||||
len(pending) < max_workers
|
||||
and seg_durations
|
||||
and _speed_limiter is None
|
||||
and now - last_hedge_check > 0.5
|
||||
and not DOWNLOAD_CANCELLED.is_set()
|
||||
):
|
||||
@@ -768,4 +877,4 @@ def requests(
|
||||
)
|
||||
|
||||
|
||||
__all__ = ("requests",)
|
||||
__all__ = ("requests", "format_speed", "parse_speed_limit", "set_speed_limit")
|
||||
|
||||
Reference in New Issue
Block a user