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:
@@ -13,6 +13,12 @@ TRIAL_GC_DAYS = int(os.getenv("FETCHER_TRIAL_GC_DAYS", "7"))
|
||||
# over several hours. Oldest-fetched users go first; the rest wait a tick.
|
||||
REFRESH_CORE_RESERVE = int(os.getenv("FETCHER_REFRESH_CORE_RESERVE", "1500"))
|
||||
REFRESH_MAX_PER_TICK = int(os.getenv("FETCHER_REFRESH_MAX_PER_TICK", "60"))
|
||||
# GitHub's secondary (burst) limits trip when refreshes run back to back. The
|
||||
# tick pauses between users; on a rate limit it waits Retry-After (else the
|
||||
# default, capped) and retries that user once, then stops and defers the rest.
|
||||
REFRESH_USER_PAUSE = float(os.getenv("FETCHER_REFRESH_USER_PAUSE", "1.5"))
|
||||
REFRESH_RETRY_SLEEP = int(os.getenv("FETCHER_REFRESH_RETRY_SLEEP", "60"))
|
||||
REFRESH_RETRY_SLEEP_MAX = int(os.getenv("FETCHER_REFRESH_RETRY_SLEEP_MAX", "120"))
|
||||
API_TIMEOUT = int(os.getenv("API_TIMEOUT", "15"))
|
||||
|
||||
# Generator URL is used as a callback target when a /fetch-async background
|
||||
|
||||
+19
-2
@@ -23,6 +23,18 @@ def _core_remaining() -> int | None:
|
||||
return None
|
||||
|
||||
|
||||
def _fetch_retrying_once(username: str) -> dict:
|
||||
"""Fetch a user; on a transient GitHub error wait Retry-After (else the
|
||||
default, capped) and try once more. A second failure propagates."""
|
||||
try:
|
||||
return github.fetch_github_data(username, token=config.GITHUB_PAT)
|
||||
except github.GitHubTransientError as e:
|
||||
wait = min(e.retry_after or config.REFRESH_RETRY_SLEEP, config.REFRESH_RETRY_SLEEP_MAX)
|
||||
log.warning("refresh rate-limited for %s: %s; retrying once in %ss", username, e, wait)
|
||||
time.sleep(wait)
|
||||
return github.fetch_github_data(username, token=config.GITHUB_PAT)
|
||||
|
||||
|
||||
def tick(hours: int, active_within_days: int, gc_days: int) -> dict:
|
||||
"""Refresh due users, then GC abandoned ones. Returns counts."""
|
||||
refreshed = 0
|
||||
@@ -31,6 +43,8 @@ def tick(hours: int, active_within_days: int, gc_days: int) -> dict:
|
||||
deferred = max(len(due) - config.REFRESH_MAX_PER_TICK, 0)
|
||||
due = due[:config.REFRESH_MAX_PER_TICK]
|
||||
for i, username in enumerate(due):
|
||||
if i:
|
||||
time.sleep(config.REFRESH_USER_PAUSE)
|
||||
left = _core_remaining()
|
||||
if left is not None and left < config.REFRESH_CORE_RESERVE:
|
||||
log.info("core budget %s under reserve %s; deferring %s refreshes",
|
||||
@@ -38,7 +52,7 @@ def tick(hours: int, active_within_days: int, gc_days: int) -> dict:
|
||||
deferred += len(due) - i
|
||||
break
|
||||
try:
|
||||
data = github.fetch_github_data(username, token=config.GITHUB_PAT)
|
||||
data = _fetch_retrying_once(username)
|
||||
user = data.get("user")
|
||||
if user is None or (isinstance(user, dict) and user.get("message") == "Not Found"):
|
||||
data = {"error": "not_found"}
|
||||
@@ -49,8 +63,11 @@ def tick(hours: int, active_within_days: int, gc_days: int) -> dict:
|
||||
refreshed += 1
|
||||
except github.GitHubTransientError as e:
|
||||
db.bump_fetch_metric("rate_limited")
|
||||
log.warning("refresh rate-limited for %s (kept last-good): %s", username, e)
|
||||
log.warning("refresh rate-limited for %s (kept last-good): %s; deferring %s refreshes",
|
||||
username, e, len(due) - i - 1)
|
||||
failed += 1
|
||||
deferred += len(due) - i - 1
|
||||
break
|
||||
except Exception as e:
|
||||
db.bump_fetch_metric("error")
|
||||
log.warning("refresh failed for %s: %s", username, e)
|
||||
|
||||
+10
-3
@@ -47,19 +47,26 @@ class GitHubTransientError(Exception):
|
||||
next successful fetch. Propagate it instead so the caller aborts the fetch
|
||||
and keeps the previously-cached record."""
|
||||
|
||||
def __init__(self, msg="", retry_after=None):
|
||||
super().__init__(msg)
|
||||
# Seconds from GitHub's Retry-After header, when it sent one.
|
||||
self.retry_after = retry_after
|
||||
|
||||
|
||||
def _raise_if_transient(resp) -> None:
|
||||
"""Raise GitHubTransientError if `resp` looks like a rate limit or server
|
||||
error. A genuine NOT_FOUND (unknown user) is left alone — that is a real,
|
||||
persistable "this user has no data" answer, not a transient failure."""
|
||||
retry_after = resp.headers.get("Retry-After")
|
||||
retry_after = int(retry_after) if retry_after and retry_after.isdigit() else None
|
||||
if resp.status_code in (429, 500, 502, 503, 504):
|
||||
raise GitHubTransientError(f"HTTP {resp.status_code}")
|
||||
raise GitHubTransientError(f"HTTP {resp.status_code}", retry_after)
|
||||
# Primary/secondary REST rate limits come back as 403 with the remaining
|
||||
# budget at 0 (or a "secondary rate limit" message).
|
||||
if resp.status_code == 403 and resp.headers.get("X-RateLimit-Remaining") == "0":
|
||||
raise GitHubTransientError("REST rate limit exhausted")
|
||||
raise GitHubTransientError("REST rate limit exhausted", retry_after)
|
||||
if resp.status_code == 403 and "rate limit" in (resp.text or "").lower():
|
||||
raise GitHubTransientError("REST rate limit exhausted")
|
||||
raise GitHubTransientError("REST rate limit exhausted", retry_after)
|
||||
# GraphQL reports rate limits as HTTP 200 with an `errors` array — the same
|
||||
# 200 that a valid response uses — so the body has to be inspected.
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user