Serialize HaikuRAG singleton creation in backend get_client
This commit is contained in:
parent
258ffa41fa
commit
2ad51f5dca
1 changed files with 13 additions and 4 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
@ -59,15 +60,23 @@ logger.info(f"QA Provider: {Config.qa.model.provider}, Model: {Config.qa.model.n
|
||||||
|
|
||||||
# Only HaikuRAG client is a singleton (expensive to create)
|
# Only HaikuRAG client is a singleton (expensive to create)
|
||||||
_client: HaikuRAG | None = None
|
_client: HaikuRAG | None = None
|
||||||
|
_client_lock = asyncio.Lock()
|
||||||
|
|
||||||
|
|
||||||
async def get_client() -> HaikuRAG:
|
async def get_client() -> HaikuRAG:
|
||||||
"""Get or create cached client."""
|
"""Get or create the cached client.
|
||||||
|
|
||||||
|
Guarded by a lock because the first request after startup can race with
|
||||||
|
itself: two concurrent callers would both pass the None check, each build
|
||||||
|
and enter a HaikuRAG, and the loser would leak its LanceDB connection.
|
||||||
|
"""
|
||||||
global _client
|
global _client
|
||||||
if _client is None:
|
if _client is None:
|
||||||
client = HaikuRAG(db_path=db_path, config=Config, create=True)
|
async with _client_lock:
|
||||||
await client.__aenter__()
|
if _client is None:
|
||||||
_client = client
|
client = HaikuRAG(db_path=db_path, config=Config, create=True)
|
||||||
|
await client.__aenter__()
|
||||||
|
_client = client
|
||||||
return _client
|
return _client
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue