Merge branch 'litellm_alerts_pr2_final_validation' of /Users/mfkhalil/Code/litellm-activity-before into litellm_alerts_reconcile_03

This commit is contained in:
moe-berri 2026-11-06 16:20:08 +08:00
commit eaf9e696ab
2 changed files with 29 additions and 1 deletions

View file

@ -6,6 +6,7 @@ from datetime import timedelta
from threading import BoundedSemaphore
from typing import Final, Protocol
from prisma.engine.errors import EngineRequestError
from prisma.errors import RawQueryError, TransactionExpiredError, TransactionNotStartedError
from pydantic import BaseModel
@ -109,6 +110,10 @@ class BoundedAnalyticsDatabase:
return await asyncio.wait_for(
self._query(query, args), timeout=6.25 if remaining is None else min(6.25, remaining)
)
except EngineRequestError as error:
if error.response.status not in (503, 504):
raise
raise AnalyticsCapacityError("Analytics exceeded its connection budget. Try again shortly") from error
except RawQueryError as error:
if _PostgresError.model_validate(error.meta or {}).code not in ("57014", "55P03"):
raise

View file

@ -6,7 +6,10 @@ from contextlib import asynccontextmanager, nullcontext
from datetime import timedelta
from typing import Final, Literal
import httpx
import pytest
from prisma.engine.errors import EngineRequestError
from prisma.http import Response
from litellm.repositories.analytics_budget import (
AnalyticsCapacityError,
@ -36,11 +39,16 @@ class _ClockedLoop(asyncio.SelectorEventLoop):
class _Driver:
def __init__(self, stage: Literal["begin", "query", "commit", "rollback", "healthy"]) -> None:
def __init__(
self, stage: Literal["begin", "query", "commit", "rollback", "healthy"], *, error: BaseException | None = None
) -> None:
self.stage = stage
self.error = error
self.cancelled = asyncio.Event()
async def _stall(self) -> None:
if self.error is not None:
raise self.error
try:
await asyncio.Event().wait()
finally:
@ -225,3 +233,18 @@ async def test_cancelled_analytics_keeps_admission_until_detached_engine_work_an
({"result": 1},),
({"result": 1},),
]
@pytest.mark.asyncio
@pytest.mark.parametrize("status", (503, 504, 422))
async def test_engine_admission_timeout_is_retryable_but_invalid_requests_are_not(status: int) -> None:
response: Final = Response(httpx.Response(status))
error: Final = EngineRequestError(response, '{"error_code":"P2028"}')
expected: Final = AnalyticsCapacityError if status in (503, 504) else EngineRequestError
with pytest.raises(expected):
await BoundedAnalyticsDatabase(_Driver("begin", error=error)).query_raw("SELECT 1 AS result")
healthy: Final = BoundedAnalyticsDatabase(_Driver("healthy"))
assert await asyncio.gather(healthy.query_raw("SELECT 1 AS result"), healthy.query_raw("SELECT 1 AS result")) == [
({"result": 1},),
({"result": 1},),
]