fix: address code review findings
- Add retry (2 attempts) on CivitAI 429 rate limit instead of silently skipping - Use check_same_thread=False for SQLite connection (aiohttp context) - Add debug logging to silent exception handlers in proxy Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -599,8 +599,8 @@ async def lm_remote_proxy_middleware(request: web.Request, handler):
|
|||||||
body = json.loads(response.body)
|
body = json.loads(response.body)
|
||||||
enriched = _enrich_list_response(body)
|
enriched = _enrich_list_response(body)
|
||||||
return web.json_response(enriched)
|
return web.json_response(enriched)
|
||||||
except Exception:
|
except Exception as exc:
|
||||||
pass # Return original response on any error
|
logger.debug("[LM-Remote] Stats enrichment failed: %s", exc)
|
||||||
# Inject stats UI script into HTML pages
|
# Inject stats UI script into HTML pages
|
||||||
if (path in _PROXY_PAGE_ROUTES or path.rstrip("/") in _PROXY_PAGE_ROUTES):
|
if (path in _PROXY_PAGE_ROUTES or path.rstrip("/") in _PROXY_PAGE_ROUTES):
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
@@ -616,8 +616,8 @@ async def lm_remote_proxy_middleware(request: web.Request, handler):
|
|||||||
charset="utf-8",
|
charset="utf-8",
|
||||||
status=response.status,
|
status=response.status,
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception as exc:
|
||||||
pass
|
logger.debug("[LM-Remote] HTML injection failed: %s", exc)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
# Not a LoRA Manager route — fall through
|
# Not a LoRA Manager route — fall through
|
||||||
|
|||||||
+1
-1
@@ -32,7 +32,7 @@ class StatsDB:
|
|||||||
|
|
||||||
def init(self) -> None:
|
def init(self) -> None:
|
||||||
"""Create the database and table if they don't exist."""
|
"""Create the database and table if they don't exist."""
|
||||||
self._conn = sqlite3.connect(str(self._db_path))
|
self._conn = sqlite3.connect(str(self._db_path), check_same_thread=False)
|
||||||
self._conn.row_factory = sqlite3.Row
|
self._conn.row_factory = sqlite3.Row
|
||||||
self._conn.execute("PRAGMA journal_mode=WAL")
|
self._conn.execute("PRAGMA journal_mode=WAL")
|
||||||
self._conn.executescript(_SCHEMA)
|
self._conn.executescript(_SCHEMA)
|
||||||
|
|||||||
+19
-16
@@ -72,25 +72,28 @@ class StatsFetchService:
|
|||||||
)
|
)
|
||||||
return self._session
|
return self._session
|
||||||
|
|
||||||
async def _fetch_model(self, model_id: int) -> dict | None:
|
async def _fetch_model(self, model_id: int, _retries: int = 2) -> dict | None:
|
||||||
"""Fetch a single model's data from CivitAI API."""
|
"""Fetch a single model's data from CivitAI API."""
|
||||||
url = f"{_CIVITAI_API}/models/{model_id}"
|
url = f"{_CIVITAI_API}/models/{model_id}"
|
||||||
session = await self._get_session()
|
session = await self._get_session()
|
||||||
try:
|
for attempt in range(_retries + 1):
|
||||||
async with session.get(url) as resp:
|
try:
|
||||||
if resp.status == 429:
|
async with session.get(url) as resp:
|
||||||
logger.warning("[LM-Remote] CivitAI rate limited, backing off")
|
if resp.status == 429:
|
||||||
await asyncio.sleep(5)
|
logger.warning("[LM-Remote] CivitAI rate limited, backing off (attempt %d)",
|
||||||
return None
|
attempt + 1)
|
||||||
if resp.status != 200:
|
await asyncio.sleep(5 * (attempt + 1))
|
||||||
logger.debug("[LM-Remote] CivitAI returned %d for model %d",
|
continue
|
||||||
resp.status, model_id)
|
if resp.status != 200:
|
||||||
return None
|
logger.debug("[LM-Remote] CivitAI returned %d for model %d",
|
||||||
return await resp.json()
|
resp.status, model_id)
|
||||||
except Exception as exc:
|
return None
|
||||||
logger.warning("[LM-Remote] CivitAI fetch failed for model %d: %s",
|
return await resp.json()
|
||||||
model_id, exc)
|
except Exception as exc:
|
||||||
return None
|
logger.warning("[LM-Remote] CivitAI fetch failed for model %d: %s",
|
||||||
|
model_id, exc)
|
||||||
|
return None
|
||||||
|
return None
|
||||||
|
|
||||||
async def fetch_stats_for_models(
|
async def fetch_stats_for_models(
|
||||||
self,
|
self,
|
||||||
|
|||||||
Reference in New Issue
Block a user