SeaWolf-AI commited on
Commit
c1e8f4e
·
verified ·
1 Parent(s): 6563d1a

fix: retry/backoff on 429 & cold-start in engine _api

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -31,14 +31,27 @@ def _engine():
31
 
32
 
33
  def _api(path, payload):
34
- c = _engine()
35
- if c is None:
36
- return {"error": "진단 엔진이 아직 연결되지 않았습니다(운영 설정 대기)."}
37
- try:
38
- res = c.predict(payload.get("url", ""), api_name="/" + path.strip("/"))
39
- return json.loads(res)
40
- except Exception as e:
41
- return {"error": "진단 엔진 호출 실패(첫 호출은 기동에 시간이 걸릴 수 있습니다): %s" % str(e)[:90]}
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
 
44
  def _domain(u):
 
31
 
32
 
33
  def _api(path, payload):
34
+ global _eng_client
35
+ import time as _t
36
+ last = ""
37
+ # 429(레이트리밋)·콜드스타트는 재시도로 흡수. 매 시도 간 백오프.
38
+ for attempt in range(4):
39
+ c = _engine()
40
+ if c is None:
41
+ return {"error": "진단 엔진이 아직 연결되지 않았습니다(운영 설정 대기)."}
42
+ try:
43
+ res = c.predict(payload.get("url", ""), api_name="/" + path.strip("/"))
44
+ return json.loads(res)
45
+ except Exception as e:
46
+ last = str(e)
47
+ transient = any(k in last for k in ("429", "Too Many", "503", "502", "504",
48
+ "timed out", "timeout", "Connection", "starting"))
49
+ if transient and attempt < 3:
50
+ _eng_client = None # 끊긴 커넥션 버리고 재연결
51
+ _t.sleep(2 + 3 * attempt) # 2s, 5s, 8s 백오프
52
+ continue
53
+ break
54
+ return {"error": "진단 엔진이 잠시 혼잡합니다. 10~20초 후 다시 눌러 주세요. (%s)" % last[:70]}
55
 
56
 
57
  def _domain(u):