fix: ping-llm адаптивный кэш — 60с при успехе, 7с при ошибке

This commit is contained in:
“Naeel”
2026-06-03 06:45:36 +03:00
parent 787ed77b7a
commit fc897367b6
+11 -4
View File
@@ -195,11 +195,17 @@ def register(app):
@app.route("/api/v1/ping-llm", methods=["GET"]) @app.route("/api/v1/ping-llm", methods=["GET"])
def ping_llm(): def ping_llm():
"""Быстрая проверка доступности LLM (с кэшем 60с).""" """Проверка доступности LLM с адаптивным кэшем.
- Успех → кэш 60с (нет смысла переспрашивать)
- Ошибка → кэш 7с (возможно LLM уже ожил)
"""
global _ping_llm_cache global _ping_llm_cache
now = time.time() now = time.time()
if _ping_llm_cache and (now - _ping_llm_cache.get("ts", 0)) < 60: if _ping_llm_cache:
return jsonify(_ping_llm_cache["data"]) ttl = _ping_llm_cache.get("ttl", 7)
if (now - _ping_llm_cache.get("ts", 0)) < ttl:
return jsonify(_ping_llm_cache["data"])
cfg = load() cfg = load()
api_key = cfg["llm"]["api_key"] api_key = cfg["llm"]["api_key"]
@@ -220,7 +226,8 @@ def register(app):
ms = int((time.time() - t0) * 1000) ms = int((time.time() - t0) * 1000)
result = {"ok": False, "ms": ms, "error": "LLM unavailable"} result = {"ok": False, "ms": ms, "error": "LLM unavailable"}
_ping_llm_cache = {"ts": now, "data": result} ttl = 60 if result.get("ok") else 7
_ping_llm_cache = {"ts": now, "data": result, "ttl": ttl}
return jsonify(result) return jsonify(result)