mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
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:
@@ -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
|
||||
|
||||
@@ -277,3 +277,16 @@ def test_repo_languages_capped_to_recent_owned_repos(monkeypatch):
|
||||
assert sorted(src.calls) == ["alice/mid", "alice/new"]
|
||||
assert repos[1]["language_bytes"] == {"Python": 1}
|
||||
assert "language_bytes" not in repos[0]
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_rate_limit_error_carries_retry_after():
|
||||
responses.add(
|
||||
responses.GET, "https://api.github.com/users/alice",
|
||||
json={"message": "You have exceeded a secondary rate limit."}, status=403,
|
||||
headers={"Retry-After": "42"},
|
||||
)
|
||||
src = github.DirectAPISource(token="t")
|
||||
with pytest.raises(github.GitHubTransientError) as exc:
|
||||
src.fetch_user_data("alice")
|
||||
assert exc.value.retry_after == 42
|
||||
|
||||
Reference in New Issue
Block a user