fix(fetcher): back off on GitHub secondary limits in refresh cron

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shay
2026-12-01 11:43:40 -05:00
co-authored by Claude Opus 5.5
parent 0835c64a63
commit 1d32dee489
5 changed files with 89 additions and 5 deletions
+41
View File
@@ -15,6 +15,14 @@ def tmp_db(monkeypatch):
yield
@pytest.fixture(autouse=True)
def sleeps(monkeypatch):
# The tick pauses between users and before a retry; record, don't wait.
calls = []
monkeypatch.setattr(cronmod.time, "sleep", calls.append)
return calls
def test_refresh_tick_calls_github_for_due_users(tmp_db):
dbmod.upsert_user("alice", {"user": {"login": "alice"}})
# Backdate
@@ -82,3 +90,36 @@ def test_tick_stops_under_core_reserve(tmp_db, monkeypatch):
stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7)
assert [c.args[0] for c in gh.call_args_list] == ["a"]
assert stats["refreshed"] == 1 and stats["deferred"] == 2
def test_tick_retries_once_after_capped_retry_after(tmp_db, monkeypatch, sleeps):
# A secondary rate limit clears after a wait: honour Retry-After (capped)
# and retry the same user once; pause between users.
_due("a", "b")
monkeypatch.setattr(cronmod, "_core_remaining", lambda: None)
results = iter([ghmod.GitHubTransientError("rate limited", retry_after=500),
{"user": {"login": "a"}}, {"user": {"login": "b"}}])
def fake(u, **_k):
r = next(results)
if isinstance(r, Exception):
raise r
return r
with patch("src.cron.github.fetch_github_data", side_effect=fake) as gh:
stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7)
assert [c.args[0] for c in gh.call_args_list] == ["a", "a", "b"]
assert sleeps == [120, 1.5]
assert stats["refreshed"] == 2 and stats["failed"] == 0
def test_tick_stops_and_defers_after_second_rate_limit(tmp_db, monkeypatch, sleeps):
# Hammering on after a repeat rate limit only fails every remaining user;
# stop the tick and leave them due for the next one.
_due("a", "b", "c")
monkeypatch.setattr(cronmod, "_core_remaining", lambda: None)
with patch("src.cron.github.fetch_github_data",
side_effect=ghmod.GitHubTransientError("rate limited")) as gh:
stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7)
assert [c.args[0] for c in gh.call_args_list] == ["a", "a"]
assert sleeps == [60]
assert stats["failed"] == 1 and stats["refreshed"] == 0 and stats["deferred"] == 2