stop infinite retry loop on rejected Lichess tokens
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 12s

A gamer's periodic check would get permanently stuck if their stored token
was revoked/expired: our stats API collapsed both "Lichess rejected the
token" (401/403, permanent) and genuine transient errors into the same 502
response, so the bot treated an invalid token exactly like a network blip —
retrying the same window forever at a capped 300s backoff, never advancing
the checkpoint (observed in prod: Dor1zz stuck for 100+ consecutive errors
over 8+ hours, admin alerts firing every 25 failures).

Preserve the distinction that already existed one layer down (lichess_client.py
already tells 401/403 apart from other failures) instead of collapsing it in
stats_service.py: add PuzzleOfPeriodResponse.auth_failed, have main.py return
401 specifically for that case, and have the bot raise a distinct
InvalidTokenError instead of returning None. On InvalidTokenError, the bot now
clears the token for that pair, notifies the user to reconnect via /addtoken,
and continues tracking games normally instead of stalling forever.
This commit is contained in:
vrubelroman 2026-07-05 07:37:14 +00:00
parent 8080921141
commit 619c00aa06
7 changed files with 70 additions and 16 deletions

View file

@ -704,7 +704,10 @@ async def get_puzzle_of_period(
try:
result = await stats_service.get_puzzle_of_period(token, since, until, max)
if not result.success:
# Реальная ошибка при обращении к Lichess — не маскируем её под "0 пазлов"
if result.auth_failed:
# Токен отклонён Lichess (401/403) — permanent, ретраить бессмысленно
raise HTTPException(status_code=401, detail=result.message)
# Прочая (транзитная) ошибка при обращении к Lichess — не маскируем её под "0 пазлов"
raise HTTPException(status_code=502, detail=result.message)
return result
except HTTPException:

View file

@ -253,6 +253,7 @@ class PuzzleOfPeriodResponse(BaseModel):
"""
message: str = Field(..., description="Сообщение о результате запроса", example="Статистика решения задач за период")
success: bool = Field(True, description="False, если запрос к Lichess завершился ошибкой (а не легитимным нулевым результатом)", example=True)
auth_failed: bool = Field(False, description="True, если Lichess отклонил токен (401/403) — permanent, не стоит ретраить; отличается от транзитных ошибок")
period_start: int = Field(..., description="Начало периода (Unix timestamp в миллисекундах)", example=1640995200000)
period_end: int = Field(..., description="Конец периода (Unix timestamp в миллисекундах)", example=1641081600000)
max_puzzles: int = Field(..., description="Максимальное количество задач для получения", example=50)

View file

@ -839,6 +839,7 @@ class StatsService:
return PuzzleOfPeriodResponse(
message="Неверный токен авторизации или доступ запрещен",
success=False,
auth_failed=True,
period_start=since_ms,
period_end=until_ms,
max_puzzles=max_puzzles,