mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
fix(launch): back off build retries on GitHub limits, dedupe fetcher misses, no ad hoc render before first build
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
"""Flask app for the fetcher service. All endpoints require X-Internal-Token."""
|
||||
import fcntl
|
||||
import hmac
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import requests
|
||||
import tempfile
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from flask import Flask, jsonify, request, Response
|
||||
from functools import wraps
|
||||
@@ -31,6 +34,10 @@ _BG_POOL = ThreadPoolExecutor(
|
||||
# fetcher container; a multi-replica deploy would need a distributed lock.
|
||||
_INFLIGHT: set[str] = set()
|
||||
_INFLIGHT_LOCK = threading.Lock()
|
||||
# /data misses for the same user (worker build, ad hoc renders, cron) used to
|
||||
# each run a full GitHub fetch. gunicorn runs sync worker processes, so the
|
||||
# lock is a per-username file lock that all workers in the container share.
|
||||
_MISS_LOCK_DIR = tempfile.gettempdir()
|
||||
|
||||
# Must stay in sync with github.py's _USERNAME_RE. Trust boundary: the
|
||||
# fetcher is called only by the generator over the internal network, but
|
||||
@@ -81,6 +88,15 @@ def metrics():
|
||||
def get_data(username: str):
|
||||
if not _valid_username(username):
|
||||
return jsonify({"error": "invalid_username"}), 400
|
||||
if db.get_user(username) is None:
|
||||
path = os.path.join(_MISS_LOCK_DIR, f"ghstats-miss-{username.lower()}.lock")
|
||||
with open(path, "w") as lock:
|
||||
fcntl.flock(lock, fcntl.LOCK_EX)
|
||||
return _get_data(username)
|
||||
return _get_data(username)
|
||||
|
||||
|
||||
def _get_data(username: str):
|
||||
row = db.get_user(username)
|
||||
if row is None:
|
||||
# Auto-fetch path
|
||||
|
||||
@@ -215,3 +215,30 @@ def test_data_miss_rate_limited_returns_503(client, monkeypatch):
|
||||
assert r.status_code == 503
|
||||
assert r.get_json() == {"error": "github_rate_limited"}
|
||||
assert dbmod.get_user("alice") is None
|
||||
|
||||
|
||||
def test_concurrent_data_misses_fetch_once(client, monkeypatch):
|
||||
# Concurrent misses for one user used to each run a full GitHub fetch;
|
||||
# the per-user lock makes later callers read what the first stored.
|
||||
import threading, time as _time
|
||||
calls = []
|
||||
|
||||
def fake_fetch(username, **_kw):
|
||||
calls.append(username)
|
||||
_time.sleep(0.3)
|
||||
return {"user": {"login": username}, "repos": [], "events": []}
|
||||
monkeypatch.setattr(apimod.github, "fetch_github_data", fake_fetch)
|
||||
monkeypatch.setattr(apimod, "_is_github_not_found", lambda _d: False)
|
||||
app = apimod.app
|
||||
codes = []
|
||||
|
||||
def hit():
|
||||
with app.test_client() as c:
|
||||
codes.append(c.get("/data/alice", headers={"X-Internal-Token": "secret"}).status_code)
|
||||
threads = [threading.Thread(target=hit) for _ in range(3)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
assert codes == [200, 200, 200]
|
||||
assert calls == ["alice"]
|
||||
|
||||
Reference in New Issue
Block a user