Back off a failed Jolpica ask; render posters off the event loop
Review round 1 (two findings): - While Jolpica failed, every schedule read asked it again, up to four times per poster request, and the poster, thumb and grand-prix routes were async, so the pacing, a 429's pause and a timeout stalled every request. A failed ask is now remembered for ten minutes (the kept schedule, or none, is answered meanwhile), a schedule in memory is answered without waiting for an ask under way, and those routes are plain defs in the threadpool. A rendered picture is stored whole (written aside, then renamed), as two may now render it side by side. - Bytecode was tracked: tests/__pycache__ (added with the tests) and models/__pycache__ that the cleanup missed. Untracked; .gitignore already names __pycache__/. From the review's notes: a poster whose map or photo failed is sent with a five-minute cache instead of a year's; uvicorn trusts the proxy's forwarded headers, so /examples redirects to https, not http; stale comments naming CARTO and the zoom lookup's key are corrected. Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
This commit is contained in:
+17
-3
@@ -34,6 +34,7 @@ class ErgastService:
|
||||
REQUEST_INTERVAL = 0.3 # seconds between two requests: under the 4 a second
|
||||
RETRIES = 3 # a throttled request (429) is asked again after a pause, at most this often
|
||||
OPEN_SEASON_TTL = 6 * 3600 # a season not yet over is asked again after six hours
|
||||
FAILED_BACKOFF = 600 # after a failed ask, a season is not asked again for ten minutes
|
||||
|
||||
# Country name normalization: Jolpica country → our country slug
|
||||
country_map = {
|
||||
@@ -49,6 +50,7 @@ class ErgastService:
|
||||
self._circuit_id_index = self._build_circuit_id_index()
|
||||
self.cache_dir = cache_dir
|
||||
self._schedules: dict[int, tuple[float, list[dict]]] = {} # season → (fetched at, races)
|
||||
self._failed_at: dict[int, float] = {} # season → when its last ask failed (monotonic)
|
||||
self._fetch_lock = threading.Lock()
|
||||
self._last_request = 0.0
|
||||
|
||||
@@ -73,21 +75,33 @@ class ErgastService:
|
||||
"""The race schedule of a season: from memory, then from disk, then from Jolpica.
|
||||
|
||||
A season that is over never changes, so it is kept on disk for good; a season not yet over is
|
||||
asked again after OPEN_SEASON_TTL. A failed fetch is never kept: the next call asks again, and
|
||||
until it succeeds the schedule kept last is answered, or none."""
|
||||
asked again after OPEN_SEASON_TTL. A failed fetch is never kept as a schedule, only its time: the
|
||||
season is not asked again for FAILED_BACKOFF, and meanwhile the schedule kept last is answered, or
|
||||
none. A schedule held in memory is answered without waiting for an ask under way."""
|
||||
kept = self._schedules.get(season)
|
||||
if kept and self._fresh(season, kept):
|
||||
return kept[1]
|
||||
with self._fetch_lock:
|
||||
kept = self._schedules.get(season) or self._read_kept(season)
|
||||
if kept and (season < date.today().year or time.time() - kept[0] < self.OPEN_SEASON_TTL):
|
||||
if kept and self._fresh(season, kept):
|
||||
self._schedules[season] = kept
|
||||
return kept[1]
|
||||
failed = self._failed_at.get(season)
|
||||
if failed is not None and time.monotonic() - failed < self.FAILED_BACKOFF:
|
||||
return kept[1] if kept else []
|
||||
races = self._ask_jolpica(season)
|
||||
if races is None:
|
||||
self._failed_at[season] = time.monotonic()
|
||||
return kept[1] if kept else []
|
||||
self._failed_at.pop(season, None)
|
||||
kept = (time.time(), races)
|
||||
self._schedules[season] = kept
|
||||
self._write_kept(season, kept)
|
||||
return races
|
||||
|
||||
def _fresh(self, season: int, kept: tuple[float, list[dict]]) -> bool:
|
||||
return season < date.today().year or time.time() - kept[0] < self.OPEN_SEASON_TTL
|
||||
|
||||
def _ask_jolpica(self, season: int) -> Optional[list[dict]]:
|
||||
"""Ask Jolpica for a season's races, paced under its rate limit; None when it does not answer."""
|
||||
url = f'{self.JOLPICA_BASE}/{season}.json?limit=100'
|
||||
|
||||
Reference in New Issue
Block a user