mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-27 11:25:08 +00:00
Merge branch 'dev' into feat/downloader-speed
Catch the branch up with dev (45 commits) so testers can run the perf work against current dev. Conflict resolutions: - downloaders/requests.py: keep the branch's racing-gated read1, adaptive controller speed reporting and byte-range slice guards; take dev's content-encoding checks (raw reads are skipped on an encoded body) and its rolling SpeedWindow for the no-controller speed fallback. The single-URL ranged-parallel gate now requires no speed limit and no caller Range. - service.py / session.py: keep the branch's TimeoutHTTPAdapter and retry config, raise POOL_MAX_SIZE to 64 so dev's pool-starvation fix survives (pool_block=True stalls threads on a pool smaller than the in-flight count). - docs: dev split configuration.md into per-area pages, so the branch's rnet network option rows moved to reference/configuration/network.md.
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import gzip
|
||||
import socket
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
import requests as rq
|
||||
|
||||
from unshackle.core.downloaders.requests import _is_content_encoded, download
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "expected"),
|
||||
[
|
||||
(None, False),
|
||||
("", False),
|
||||
(" ", False),
|
||||
("identity", False),
|
||||
("IDENTITY", False),
|
||||
("identity, identity", False),
|
||||
("gzip", True),
|
||||
("GZIP", True),
|
||||
(" gzip ", True),
|
||||
("x-gzip", True),
|
||||
("br", True),
|
||||
("zstd", True),
|
||||
("deflate", True),
|
||||
("compress", True),
|
||||
("dcb", True),
|
||||
("gzip, br", True),
|
||||
("identity, gzip", True),
|
||||
("gzip, identity", True),
|
||||
("gzip,,", True),
|
||||
("some-future-coding", True),
|
||||
],
|
||||
)
|
||||
def test_is_content_encoded(value, expected):
|
||||
assert _is_content_encoded(value) is expected
|
||||
|
||||
|
||||
PLAIN = b"WEBVTT\n\n" + b"".join(f"{i:05d} cue text\n".encode() for i in range(2000))
|
||||
|
||||
|
||||
class _EncodingServer:
|
||||
"""Serves one gzip body, honouring Range against the encoded representation."""
|
||||
|
||||
def __init__(self):
|
||||
self.encoded = gzip.compress(PLAIN)
|
||||
self.sock = socket.socket()
|
||||
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
self.sock.bind(("127.0.0.1", 0))
|
||||
self.sock.listen(8)
|
||||
self.url = f"http://127.0.0.1:{self.sock.getsockname()[1]}/sub.vtt"
|
||||
threading.Thread(target=self._serve, daemon=True).start()
|
||||
|
||||
def _serve(self):
|
||||
while True:
|
||||
try:
|
||||
conn, _ = self.sock.accept()
|
||||
except OSError:
|
||||
return
|
||||
try:
|
||||
request = conn.recv(65536).decode("latin1")
|
||||
start = next(
|
||||
(
|
||||
int(line.split("=", 1)[1].split("-")[0])
|
||||
for line in request.split("\r\n")
|
||||
if line.lower().startswith("range:")
|
||||
),
|
||||
None,
|
||||
)
|
||||
if start is None:
|
||||
body = self.encoded
|
||||
head = f"HTTP/1.1 200 OK\r\nContent-Length: {len(body)}\r\n"
|
||||
else:
|
||||
body = self.encoded[start:]
|
||||
head = (
|
||||
f"HTTP/1.1 206 Partial Content\r\nContent-Length: {len(body)}\r\n"
|
||||
f"Content-Range: bytes {start}-{len(self.encoded) - 1}/{len(self.encoded)}\r\n"
|
||||
)
|
||||
conn.sendall(
|
||||
(head + "Content-Encoding: gzip\r\nAccept-Ranges: bytes\r\nConnection: close\r\n\r\n").encode()
|
||||
+ body
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def close(self):
|
||||
self.sock.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def encoding_server():
|
||||
server = _EncodingServer()
|
||||
yield server
|
||||
server.close()
|
||||
|
||||
|
||||
def test_encoded_body_is_decoded_on_disk(tmp_path, encoding_server):
|
||||
save_path = tmp_path / "sub.vtt"
|
||||
list(download(url=encoding_server.url, save_path=save_path, session=rq.Session()))
|
||||
assert save_path.read_bytes() == PLAIN
|
||||
|
||||
|
||||
def test_encoded_body_discards_a_partial_instead_of_range_resuming(tmp_path, encoding_server):
|
||||
save_path = tmp_path / "sub.vtt"
|
||||
save_path.with_name("sub.vtt.!dev").write_bytes(PLAIN[:5000])
|
||||
list(download(url=encoding_server.url, save_path=save_path, session=rq.Session()))
|
||||
assert save_path.read_bytes() == PLAIN
|
||||
@@ -0,0 +1,118 @@
|
||||
import importlib
|
||||
import inspect
|
||||
import threading
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED
|
||||
from unshackle.core.downloaders.requests import TokenBucket, format_speed, parse_speed_limit, set_speed_limit
|
||||
|
||||
requests_downloader = importlib.import_module("unshackle.core.downloaders.requests")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "expected"),
|
||||
[
|
||||
("500k", 500_000),
|
||||
("5M", 5_000_000),
|
||||
("10MB", 10_000_000),
|
||||
("10mb/s", 10_000_000),
|
||||
("1.5G", 1_500_000_000),
|
||||
("1048576", 1_048_576),
|
||||
(5_000_000, 5_000_000.0),
|
||||
(None, None),
|
||||
("off", None),
|
||||
("none", None),
|
||||
("unlimited", None),
|
||||
("0", None),
|
||||
(0, None),
|
||||
],
|
||||
)
|
||||
def test_parse_speed_limit(value, expected):
|
||||
assert parse_speed_limit(value) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value",
|
||||
["fast", "10TB", "10MiB", "-5M", "M", "1..5M", ".", "1.2.3", float("inf"), float("nan"), -5, -1.5],
|
||||
)
|
||||
def test_parse_speed_limit_rejects(value):
|
||||
with pytest.raises(ValueError):
|
||||
parse_speed_limit(value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("rate", "expected"),
|
||||
[
|
||||
(500, "500 bytes/s"),
|
||||
(512_000, "512.0 kB/s"),
|
||||
(39 * 1024**2, "40.9 MB/s"),
|
||||
(1.5 * 1024**3, "1.6 GB/s"),
|
||||
],
|
||||
)
|
||||
def test_format_speed(rate, expected):
|
||||
assert format_speed(rate) == expected
|
||||
|
||||
|
||||
def test_result_accepts_a_missing_speed_limit():
|
||||
"""The API/serve path calls dl.result() by keyword without speed_limit."""
|
||||
from unshackle.commands.dl import dl
|
||||
|
||||
assert inspect.signature(dl.result).parameters["speed_limit"].default is None
|
||||
|
||||
|
||||
def test_bucket_holds_aggregate_rate_across_threads():
|
||||
rate = 10_000_000
|
||||
bucket = TokenBucket(rate)
|
||||
chunk = 100_000
|
||||
totals = [0, 0, 0, 0]
|
||||
start = time.monotonic()
|
||||
stop = start + 1.0
|
||||
|
||||
def worker(i: int) -> None:
|
||||
while time.monotonic() < stop:
|
||||
bucket.consume(chunk)
|
||||
totals[i] += chunk
|
||||
|
||||
threads = [threading.Thread(target=worker, args=(i,)) for i in range(4)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
achieved = sum(totals) / (time.monotonic() - start)
|
||||
# exceeding the limit is a real failure; undershoot is scheduler noise on a loaded box
|
||||
assert achieved <= rate * 1.1
|
||||
assert achieved >= rate * 0.5
|
||||
|
||||
|
||||
def test_bucket_chunk_larger_than_capacity_does_not_deadlock():
|
||||
bucket = TokenBucket(1_000_000)
|
||||
start = time.monotonic()
|
||||
bucket.consume(1_100_000)
|
||||
assert 1.0 < time.monotonic() - start < 2.0
|
||||
|
||||
|
||||
def test_locked_limit_ignores_unlocked_calls():
|
||||
"""serve's global_speed_limit must survive the per-job set in dl.result()."""
|
||||
try:
|
||||
set_speed_limit(5_000_000, lock=True)
|
||||
set_speed_limit(None)
|
||||
set_speed_limit(1_000_000)
|
||||
limiter = requests_downloader._speed_limiter
|
||||
assert limiter is not None and limiter.rate == 5_000_000
|
||||
finally:
|
||||
requests_downloader._speed_limit_locked = False
|
||||
requests_downloader._speed_limiter = None
|
||||
|
||||
|
||||
def test_bucket_wait_aborts_on_cancellation():
|
||||
"""Debt sleeps grow with thread count; cancelling must not wait them out."""
|
||||
bucket = TokenBucket(1_000)
|
||||
DOWNLOAD_CANCELLED.set()
|
||||
try:
|
||||
start = time.monotonic()
|
||||
bucket.consume(1_000_000)
|
||||
assert time.monotonic() - start < 1.0
|
||||
finally:
|
||||
DOWNLOAD_CANCELLED.clear()
|
||||
@@ -0,0 +1,43 @@
|
||||
import pytest
|
||||
|
||||
from unshackle.core.downloaders.requests import SPEED_ROLLING_WINDOW, SpeedWindow
|
||||
|
||||
|
||||
def test_rate_reflects_recent_window_not_full_history():
|
||||
"""After a slow start (shared bucket), the display must show the current rate."""
|
||||
win = SpeedWindow(0.0)
|
||||
total = 0
|
||||
for t in range(0, 60):
|
||||
total += 5_000_000
|
||||
win.rate(float(t), total)
|
||||
for t in range(60, 120):
|
||||
total += 10_000_000
|
||||
rate = win.rate(float(t), total)
|
||||
assert rate == pytest.approx(10_000_000, rel=0.01)
|
||||
|
||||
|
||||
def test_first_report_is_the_since_start_average():
|
||||
"""Bytes are credited in lumps; the first lump must not read as a whole window's worth."""
|
||||
win = SpeedWindow(0.0)
|
||||
assert win.rate(2.5, 25_000_000) == pytest.approx(10_000_000)
|
||||
|
||||
|
||||
def test_zero_span_returns_none():
|
||||
win = SpeedWindow(0.0)
|
||||
assert win.rate(0.0, 1_000_000) is None
|
||||
|
||||
|
||||
def test_stall_returns_none():
|
||||
"""Once the seed ages out, a total that stops growing has no delta to report."""
|
||||
win = SpeedWindow(0.0)
|
||||
for t in range(0, SPEED_ROLLING_WINDOW + 6):
|
||||
rate = win.rate(float(t), 1_000_000)
|
||||
assert rate is None
|
||||
|
||||
|
||||
def test_window_drops_old_samples():
|
||||
win = SpeedWindow(0.0)
|
||||
for t in range(0, 3 * SPEED_ROLLING_WINDOW):
|
||||
win.rate(float(t), t * 1_000_000)
|
||||
assert win.samples[0][0] >= 2 * SPEED_ROLLING_WINDOW - 1
|
||||
assert len(win.samples) == SPEED_ROLLING_WINDOW + 1
|
||||
Reference in New Issue
Block a user