From 98a810a71ca369cb6c4ee95e47018546b80d200f Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 3 Oct 2026 18:31:59 -0700 Subject: [PATCH] refactor(lens): keep update retry settings next to the repository --- litellm/proxy/lens/repository.py | 7 ++++--- tests/unit/proxy/lens/test_repository.py | 9 ++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/litellm/proxy/lens/repository.py b/litellm/proxy/lens/repository.py index 8875cf3512b..15d2cfc34e6 100644 --- a/litellm/proxy/lens/repository.py +++ b/litellm/proxy/lens/repository.py @@ -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( diff --git a/tests/unit/proxy/lens/test_repository.py b/tests/unit/proxy/lens/test_repository.py index 1885e04abb3..40f2b83bc1b 100644 --- a/tests/unit/proxy/lens/test_repository.py +++ b/tests/unit/proxy/lens/test_repository.py @@ -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)