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
+19 -2
View File
@@ -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)