update
This commit is contained in:
@@ -477,34 +477,34 @@ class KoreaInvestmentAPI:
|
||||
**kwargs
|
||||
)
|
||||
|
||||
# 성공
|
||||
if response.status_code == 200:
|
||||
duration = time.time() - start_time
|
||||
self.monitor.record_call(success=True, duration=duration, retries=retry_count)
|
||||
self.circuit_breaker.record_success()
|
||||
if self.monitor.should_alert(threshold=settings.API_ERROR_THRESHOLD):
|
||||
logger.warning(f"⚠️ API 오류율 높음: {self.monitor.get_error_rate():.1%}")
|
||||
return response.json(), response.headers
|
||||
# 성공
|
||||
if response.status_code == 200:
|
||||
duration = time.time() - start_time
|
||||
self.monitor.record_call(success=True, duration=duration, retries=retry_count)
|
||||
self.circuit_breaker.record_success()
|
||||
if self.monitor.should_alert(threshold=settings.API_ERROR_THRESHOLD):
|
||||
logger.warning(f"⚠️ API 오류율 높음: {self.monitor.get_error_rate():.1%}")
|
||||
return response.json(), response.headers
|
||||
|
||||
# 서버 오류 (5xx, 429) — 재시도
|
||||
elif response.status_code in [429, 500, 502, 503, 504]:
|
||||
retry_count += 1
|
||||
if attempt < self.max_retries:
|
||||
delay = self.retry_delay * (self.retry_backoff ** attempt)
|
||||
logger.warning(f"⏳ API 재시도 {attempt + 1}/{self.max_retries}: {response.status_code}, {delay:.1f}초 후")
|
||||
await asyncio.sleep(delay)
|
||||
continue
|
||||
else:
|
||||
self.circuit_breaker.record_failure()
|
||||
raise Exception(f"API 최대 재시도 초과: {response.status_code}")
|
||||
|
||||
# 클라이언트 오류 (4xx) — 재시도 불필요
|
||||
# 서버 오류 (5xx, 429) — 재시도
|
||||
elif response.status_code in [429, 500, 502, 503, 504]:
|
||||
retry_count += 1
|
||||
if attempt < self.max_retries:
|
||||
delay = self.retry_delay * (self.retry_backoff ** attempt)
|
||||
logger.warning(f"⏳ API 재시도 {attempt + 1}/{self.max_retries}: {response.status_code}, {delay:.1f}초 후")
|
||||
await asyncio.sleep(delay)
|
||||
continue
|
||||
else:
|
||||
self.monitor.record_call(success=False, duration=time.time() - start_time, retries=retry_count)
|
||||
try:
|
||||
return response.json(), response.headers
|
||||
except Exception:
|
||||
return {"rt_cd": str(response.status_code), "msg1": response.text[:200], "output": None}, None
|
||||
self.circuit_breaker.record_failure()
|
||||
raise Exception(f"API 최대 재시도 초과: {response.status_code}")
|
||||
|
||||
# 클라이언트 오류 (4xx) — 재시도 불필요
|
||||
else:
|
||||
self.monitor.record_call(success=False, duration=time.time() - start_time, retries=retry_count)
|
||||
try:
|
||||
return response.json(), response.headers
|
||||
except Exception:
|
||||
return {"rt_cd": str(response.status_code), "msg1": response.text[:200], "output": None}, None
|
||||
|
||||
except httpx.TimeoutException as e:
|
||||
retry_count += 1
|
||||
|
||||
Reference in New Issue
Block a user