Proxy: fetch segments across 4 parallel byte-range lanes

The slow-segment logs show per-connection CDN throttling (15-40 KB/s
dribbles). yt-dlp's standard fix is -N 16 parallel fragment lanes; this
is the 4-lane version for the proxy: HEAD for content-length, then 4
concurrent Range GETs reassembled byte-exact (verified sha256-identical
to a direct CDN fetch). Small files (<512KB) still use a single GET.

ANY failure (no content-length, 416, lane error, timeout) falls back to
the existing single-stream path, so this is never worse than before.

Caveat (measured live): when the CDN caps SHARED egress rather than
per-connection, lanes just split the same pie — in that window both
1-lane and 4-lane time out. A different source is the only real fix
for those windows.

18/18 tests (new test pins lane stitch + fallback; also fixed a
pre-existing _get stub leak in the Jikan-shape test).
This commit is contained in:
jozamudi committed 2026-08-31 14:51:55 +00:00
1 parent 2576546b43
commit ee2a888aca
3 files changed
+120 -6

No files matched your search

+21 -6
View File
@@ -545,12 +545,27 @@ def stream(token: str):
return Response(hit[1], media_type=hit[0], headers=_STREAM_CACHE_HDRS)
if url.endswith(".m3u8"):
return _stream_playlist(url)
# segment miss: stream through (12s = total transfer cap), so the browser's
# first byte no longer waits for the CDN's whole upload
r = _upstream(upstream.get_stream, url, timeout=12)
ct = r.headers.get("content-type", "application/octet-stream")
return StreamingResponse(_seg_stream(r, url, ct), media_type=ct,
headers=_STREAM_CACHE_HDRS)
# segment miss: try parallel byte-range lanes first (4 concurrent fetches
# beat per-connection CDN throttling); ANY failure falls back to the
# single streaming path, so this can never be worse than before
t0 = time.time()
try:
body, ct = _upstream(upstream.fetch_parallel, url)
except HTTPException:
r = _upstream(upstream.get_stream, url, timeout=12)
ct = r.headers.get("content-type", "application/octet-stream")
if time.time() - t0 > 8:
name = url.rsplit("/", 1)[-1][:40]
print(f"[stream-slow] {name} parallel-fallback elapsed={time.time()-t0:.1f}s",
flush=True)
return StreamingResponse(_seg_stream(r, url, ct), media_type=ct,
headers=_STREAM_CACHE_HDRS)
if time.time() - t0 > 8:
name = url.rsplit("/", 1)[-1][:40]
print(f"[stream-slow] {name} parallel total={time.time()-t0:.1f}s "
f"bytes={len(body)}", flush=True)
_cache_put(url, ct, body)
return Response(body, media_type=ct, headers=_STREAM_CACHE_HDRS)
app.mount("/", StaticFiles(directory=ROOT / "static", html=True), name="static")