fix(perf): блок ④ — тяжёлую синхронщину с event loop в поток (не морозить всех)
Синхронный I/O/CPU в async-пути под нагрузкой морозил сервер у ВСЕХ (класс инцидента с сетью ФГИС, Fable audit): - audit-middleware: log_action (INSERT на КАЖДОМ запросе) → asyncio.to_thread. - регистрация: SMTP (SSL connect+login, timeout 10s) → to_thread. - GLM stream_claude: весь тяжёлый сбор контекста (_search_resources перебор 24k, _search_corpus, _search_fssc, _search_winter, RAG faiss+embed, _search_normative) → to_thread (был синхронным; в потоке уже был только _read_work_dir_files). Осталось (ниже частота, отдельно): save_message в чат-стриме, прочие db-вызовы. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -249,7 +249,9 @@ async def audit_middleware(request: Request, call_next):
|
||||
detail = sub
|
||||
ip = request.client.host if request.client else None
|
||||
try:
|
||||
log_action(username, action, method, path, detail, ip, response.status_code)
|
||||
# to_thread: синхронный INSERT на КАЖДОМ запросе иначе морозит event loop
|
||||
# при WAL-локе (busy_timeout=10s) — сервер замирал бы у всех (Fable audit).
|
||||
await asyncio.to_thread(log_action, username, action, method, path, detail, ip, response.status_code)
|
||||
except Exception:
|
||||
pass
|
||||
return response
|
||||
@@ -464,7 +466,8 @@ async def register(request: Request):
|
||||
code = result.get("code", "")
|
||||
if code:
|
||||
from email_service import send_verification_code
|
||||
sent = send_verification_code(email, code)
|
||||
# to_thread: SMTP (SSL connect + login, timeout 10s) иначе морозит event loop (Fable)
|
||||
sent = await asyncio.to_thread(send_verification_code, email, code)
|
||||
if not sent:
|
||||
return {"message": "Аккаунт создан, но email не отправлен. Обратитесь к администратору."}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user