Speed up: preload BM25 at startup, httpx timeout+limits, fix lambda in to_thread

- index: lifespan preloads BM25 model so first /sparse_embedding request
  doesn't pay cold-start cost (~1-2s per worker)
- search: same BM25 preload + httpx timeout=30s + connection limits to
  avoid hanging on slow external APIs
- search: asyncio.to_thread(fn, arg) instead of lambda wrapper

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
q 2026-04-18 20:04:20 +03:00
parent 965fc906f3
commit d2e531cc07
2 changed files with 13 additions and 7 deletions

View file

@ -1,6 +1,7 @@
import asyncio
import logging
import os
from contextlib import asynccontextmanager
from functools import lru_cache
from typing import Any
@ -189,7 +190,13 @@ def build_chunks(
return result
app = FastAPI(title="Index Service", version="0.1.0")
@asynccontextmanager
async def lifespan(app: FastAPI):
await asyncio.to_thread(get_sparse_model)
yield
app = FastAPI(title="Index Service", version="0.1.0", lifespan=lifespan)
@app.get("/health")

View file

@ -148,11 +148,10 @@ def get_sparse_model() -> SparseTextEmbedding:
@asynccontextmanager
async def lifespan(app: FastAPI):
app.state.http = httpx.AsyncClient()
app.state.qdrant = AsyncQdrantClient(
url=QDRANT_URL,
api_key=API_KEY,
)
await asyncio.to_thread(get_sparse_model)
limits = httpx.Limits(max_connections=100, max_keepalive_connections=20)
app.state.http = httpx.AsyncClient(timeout=30.0, limits=limits)
app.state.qdrant = AsyncQdrantClient(url=QDRANT_URL, api_key=API_KEY)
try:
yield
finally:
@ -354,7 +353,7 @@ async def search(payload: SearchAPIRequest) -> SearchAPIResponse:
sparse_query = build_sparse_query(question)
dense_task = embed_dense(client, dense_query)
sparse_task = asyncio.to_thread(lambda: embed_sparse_sync(sparse_query))
sparse_task = asyncio.to_thread(embed_sparse_sync, sparse_query)
dense_vector, sparse_vector = await asyncio.gather(dense_task, sparse_task)
dense_vectors = [dense_vector]