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)
|
||||
enriched = _enrich_list_response(body)
|
||||
return web.json_response(enriched)
|
||||
except Exception:
|
||||
pass # Return original response on any error
|
||||
except Exception as exc:
|
||||
logger.debug("[LM-Remote] Stats enrichment failed: %s", exc)
|
||||
# Inject stats UI script into HTML pages
|
||||
if (path in _PROXY_PAGE_ROUTES or path.rstrip("/") in _PROXY_PAGE_ROUTES):
|
||||
if response.status == 200:
|
||||
@@ -616,8 +616,8 @@ async def lm_remote_proxy_middleware(request: web.Request, handler):
|
||||
charset="utf-8",
|
||||
status=response.status,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as exc:
|
||||
logger.debug("[LM-Remote] HTML injection failed: %s", exc)
|
||||
return response
|
||||
|
||||
# Not a LoRA Manager route — fall through
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ class StatsDB:
|
||||
|
||||
def init(self) -> None:
|
||||
"""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.execute("PRAGMA journal_mode=WAL")
|
||||
self._conn.executescript(_SCHEMA)
|
||||
|
||||
+19
-16
@@ -72,25 +72,28 @@ class StatsFetchService:
|
||||
)
|
||||
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."""
|
||||
url = f"{_CIVITAI_API}/models/{model_id}"
|
||||
session = await self._get_session()
|
||||
try:
|
||||
async with session.get(url) as resp:
|
||||
if resp.status == 429:
|
||||
logger.warning("[LM-Remote] CivitAI rate limited, backing off")
|
||||
await asyncio.sleep(5)
|
||||
return None
|
||||
if resp.status != 200:
|
||||
logger.debug("[LM-Remote] CivitAI returned %d for model %d",
|
||||
resp.status, model_id)
|
||||
return None
|
||||
return await resp.json()
|
||||
except Exception as exc:
|
||||
logger.warning("[LM-Remote] CivitAI fetch failed for model %d: %s",
|
||||
model_id, exc)
|
||||
return None
|
||||
for attempt in range(_retries + 1):
|
||||
try:
|
||||
async with session.get(url) as resp:
|
||||
if resp.status == 429:
|
||||
logger.warning("[LM-Remote] CivitAI rate limited, backing off (attempt %d)",
|
||||
attempt + 1)
|
||||
await asyncio.sleep(5 * (attempt + 1))
|
||||
continue
|
||||
if resp.status != 200:
|
||||
logger.debug("[LM-Remote] CivitAI returned %d for model %d",
|
||||
resp.status, model_id)
|
||||
return None
|
||||
return await resp.json()
|
||||
except Exception as exc:
|
||||
logger.warning("[LM-Remote] CivitAI fetch failed for model %d: %s",
|
||||
model_id, exc)
|
||||
return None
|
||||
return None
|
||||
|
||||
async def fetch_stats_for_models(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user