From 1062589501c74fd2fe5b480373827cfaca6c59c9 Mon Sep 17 00:00:00 2001 From: Broque Thomas <26755000+Nezreka@users.noreply.github.com> Date: Mon, 4 May 2026 14:45:52 -0700 Subject: [PATCH] E2: Migrate YouTube to declared rate-limit policy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit YouTubeClient gains rate_limit_policy() that returns a RateLimitPolicy with the configured download_delay (3s default from `youtube.download_delay`). Engine reads this at register_plugin time + applies to engine.worker. set_engine still re-applies the delay so runtime reload_settings updates flow through the same pathway. Other sources keep the default policy (concurrency=1, delay=0) which matches their current behavior — no migration needed beyond YouTube which is the only source with a non-default download throttle today. New pinning test asserts the policy shape (delay=3.0, concurrency=1). Suite still green (2042 passed). --- core/youtube_client.py | 16 +++++++++++++++- tests/downloads/test_youtube_pinning.py | 11 +++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/core/youtube_client.py b/core/youtube_client.py index dd128f82..686c40f8 100644 --- a/core/youtube_client.py +++ b/core/youtube_client.py @@ -125,10 +125,24 @@ class YouTubeClient: # tests that bypass the orchestrator. self._engine = None + def rate_limit_policy(self): + """YouTube reads its download delay from user-tunable config + (``youtube.download_delay``, default 3s). Engine reads this + at ``register_plugin`` time, then ``set_engine`` runs and + re-applies if the config changed since instance construction.""" + from core.download_engine import RateLimitPolicy + return RateLimitPolicy( + download_concurrency=1, + download_delay_seconds=float(self._download_delay), + ) + def set_engine(self, engine): """Engine callback — gives the client access to the central thread worker + state store. Engine calls this during - ``register_plugin`` if the plugin defines it.""" + ``register_plugin`` if the plugin defines it. Worker delay + was already set from rate_limit_policy() — re-apply here so + runtime ``reload_settings`` updates take effect via the + same pathway.""" self._engine = engine engine.worker.set_delay('youtube', float(self._download_delay)) diff --git a/tests/downloads/test_youtube_pinning.py b/tests/downloads/test_youtube_pinning.py index 6c65e534..59cfb71b 100644 --- a/tests/downloads/test_youtube_pinning.py +++ b/tests/downloads/test_youtube_pinning.py @@ -149,6 +149,17 @@ def test_set_engine_configures_worker_delay(yt_client_with_engine): assert engine.worker._get_delay('youtube') == 3.0 +def test_rate_limit_policy_reflects_configured_delay(yt_client_with_engine): + """Pinning (Phase E): YouTube's rate_limit_policy() returns a + RateLimitPolicy with the configured download_delay (3s default + from `youtube.download_delay` config). Engine reads this at + register_plugin time.""" + client, _ = yt_client_with_engine + policy = client.rate_limit_policy() + assert policy.download_delay_seconds == 3.0 + assert policy.download_concurrency == 1 + + # --------------------------------------------------------------------------- # Query / cancel — engine-backed reads # ---------------------------------------------------------------------------