Add global yt-dlp metadata rate limiter (5s + jitter between calls)

All fetch_video_metadata / fetch_channel_metadata / fetch_channel_playlists
/ fetch_available_subs calls now go through _meta_run which enforces a
minimum 5s gap (+ 0.5-2.5s random jitter) across all concurrent tasks.
Per-task sleep loops removed since the global lock serializes everything.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 00:31:14 +02:00
parent 15e6b94cbf
commit c180c293b0
2 changed files with 29 additions and 17 deletions

View File

@@ -80,11 +80,7 @@ def _get_channel_or_404(db: Session, channel_id: int) -> Channel:
def _index_channels_batch(channel_ids: list[int], user_id: int, delay: float = 1.5):
"""Run channel syncs sequentially with a polite delay between requests."""
import time
for i, cid in enumerate(channel_ids):
if i > 0:
time.sleep(delay)
for cid in channel_ids:
_index_channel_task(cid, user_id)
@@ -208,9 +204,7 @@ def _enrich_missing_task(limit: int = 20):
"""),
{"limit": limit},
).mappings().all()
for i, row in enumerate(rows):
if i > 0:
time.sleep(2.0)
for row in rows:
try:
meta = ytdlp.fetch_video_metadata(row["youtube_video_id"])
if meta:
@@ -824,9 +818,7 @@ def _fetch_popular_task(channel_id: int, youtube_channel_id: str, channel_name:
_tasks[task_id]["done"] = 0
try:
for i, yt_id in enumerate(video_ids):
if i > 0:
time.sleep(2.0)
for yt_id in video_ids:
try:
meta = ytdlp.fetch_video_metadata(yt_id)
if meta: