mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-11 03:38:38 +00:00
refactor(lens): keep update retry settings next to the repository
This commit is contained in:
parent
e9f3d8a24d
commit
98a810a71c
2 changed files with 8 additions and 8 deletions
|
|
@ -7,7 +7,6 @@ from typing import Final, Protocol
|
|||
|
||||
from pydantic import BaseModel, JsonValue, TypeAdapter
|
||||
|
||||
from litellm.constants import LENS_UPDATE_ATTEMPTS, LENS_UPDATE_BACKOFF_SECONDS
|
||||
from litellm.proxy.db.prisma_client import PrismaWrapper
|
||||
from litellm.proxy.lens.models import Job, Lens, Scope, Worker
|
||||
|
||||
|
|
@ -22,6 +21,8 @@ class Row(BaseModel):
|
|||
|
||||
|
||||
_ROWS: Final = TypeAdapter(tuple[Row, ...])
|
||||
UPDATE_ATTEMPTS: Final = 40
|
||||
UPDATE_BACKOFF_SECONDS: Final = 0.02
|
||||
|
||||
|
||||
class LensRepository:
|
||||
|
|
@ -54,7 +55,7 @@ class LensRepository:
|
|||
self,
|
||||
lens_id: str,
|
||||
transform: Callable[[Lens], Lens],
|
||||
attempts: int = LENS_UPDATE_ATTEMPTS,
|
||||
attempts: int = UPDATE_ATTEMPTS,
|
||||
*,
|
||||
changed_only: bool = False,
|
||||
) -> Lens | None:
|
||||
|
|
@ -62,7 +63,7 @@ class LensRepository:
|
|||
completed, updated = await self._try_update(lens_id, transform, changed_only)
|
||||
if completed:
|
||||
return updated
|
||||
await self.sleep(random.uniform(0, LENS_UPDATE_BACKOFF_SECONDS * min(attempt + 1, 8)))
|
||||
await self.sleep(random.uniform(0, UPDATE_BACKOFF_SECONDS * min(attempt + 1, 8)))
|
||||
return None
|
||||
|
||||
async def _try_update(
|
||||
|
|
|
|||
|
|
@ -3,9 +3,8 @@ from typing import Final
|
|||
|
||||
import pytest
|
||||
|
||||
from litellm.constants import LENS_UPDATE_ATTEMPTS
|
||||
from litellm.proxy.lens.models import Check, Lens, LensSettings, Scope
|
||||
from litellm.proxy.lens.repository import LensRepository, Row
|
||||
from litellm.proxy.lens.repository import UPDATE_ATTEMPTS, LensRepository, Row
|
||||
|
||||
NOW: Final = datetime(2026, 1, 15, tzinfo=timezone.utc)
|
||||
STORED: Final = Lens(
|
||||
|
|
@ -59,8 +58,8 @@ async def test_update_backs_off_between_lost_writes_and_gives_up_after_the_limit
|
|||
async def record(seconds: float) -> None:
|
||||
waits.append(seconds)
|
||||
|
||||
db: Final = ContendedDatabase(losses=LENS_UPDATE_ATTEMPTS)
|
||||
db: Final = ContendedDatabase(losses=UPDATE_ATTEMPTS)
|
||||
assert await LensRepository(db, sleep=record).update("lens", renamed) is None
|
||||
assert db.writes == LENS_UPDATE_ATTEMPTS
|
||||
assert len(waits) == LENS_UPDATE_ATTEMPTS
|
||||
assert db.writes == UPDATE_ATTEMPTS
|
||||
assert len(waits) == UPDATE_ATTEMPTS
|
||||
assert all(w >= 0 for w in waits)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue