prioritize interactive requests over background checks in shared Lichess-token queue
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 15s

/today, /yesterday, /week were queuing behind the entire periodic-check
backlog on the same rate-limited LICHESS_APP_TOKEN (observed ~2min wait on
prod for a single command). Add a two-tier priority queue to SharedTokenGate:
interactive on-demand requests jump ahead of background periodic checks,
which still drain normally when nothing interactive is waiting. Verified
locally end-to-end (isolated queue unit tests, live request with priority
correctly reaching Lichess, and a real successful accuracy fetch rendered
through StatsFormatter).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-07-26 10:25:34 +00:00
parent 0dbd0c400b
commit d4eb61f994
6 changed files with 76 additions and 25 deletions

View file

@ -104,12 +104,19 @@ class LichessAPI:
logger.error(f"Error getting week stats: {e}")
return None
async def get_games_period(self, username: str, since: int, until: int, rated_only: Optional[bool] = None, include_games: bool = False) -> Optional[Dict[str, Any]]:
"""Get games for a specific period"""
async def get_games_period(self, username: str, since: int, until: int, rated_only: Optional[bool] = None, include_games: bool = False, priority: str = "background") -> Optional[Dict[str, Any]]:
"""
Get games for a specific period.
priority="interactive" (e.g. the accuracy fetch behind /today, /yesterday,
/week) jumps ahead of "background" (periodic_check) in the stats
service's shared Lichess-token queue, so an on-demand command doesn't
wait behind the whole periodic-check backlog.
"""
await self.rate_limiter.wait_if_needed()
try:
url = f"{self.stats_base_url}/games/{username}/period"
params = {"since": since, "until": until}
params = {"since": since, "until": until, "priority": priority}
if rated_only is not None:
params["rated_only"] = "true" if rated_only else "false"
if include_games: