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

@ -503,7 +503,11 @@ async def get_games_of_period(
example=True),
include_games: bool = Query(False,
description="Включить построчный список отдельных партий (только blitz/rapid/classical)",
example=False)
example=False),
priority: str = Query("background",
pattern="^(background|interactive)$",
description="\"interactive\" (запросы из /today и т.п.) обгоняет \"background\" (периодические проверки) в общей очереди shared-token запросов к Lichess",
example="background")
):
"""
## Статистика игр за период
@ -557,7 +561,7 @@ async def get_games_of_period(
# Конвертируем миллисекунды в секунды для внутренней логики
since_seconds = since // 1000
until_seconds = until // 1000
result = await stats_service.get_games_of_period(username, since_seconds, until_seconds, rated_only, include_games)
result = await stats_service.get_games_of_period(username, since_seconds, until_seconds, rated_only, include_games, priority=priority)
if not result.success:
# Реальная ошибка при обращении к Lichess — не маскируем её под "0 игр"
raise HTTPException(status_code=502, detail=result.message)