From 49adc9c405b9eee5b64dddf30e7cc209ecfdc623 Mon Sep 17 00:00:00 2001 From: shuim Date: Thu, 2 Apr 2026 11:28:15 +0900 Subject: [PATCH] =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/services/kis_realtime_stream.py | 45 +++++++++++---------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/backend/app/services/kis_realtime_stream.py b/backend/app/services/kis_realtime_stream.py index 415658c..fe38a89 100644 --- a/backend/app/services/kis_realtime_stream.py +++ b/backend/app/services/kis_realtime_stream.py @@ -474,31 +474,32 @@ class KisRealtimeStream: # WebSocket handshake returned a non-101 HTTP response (often 200) connection_attempts += 1 status = getattr(isc, 'status_code', None) - logger.error(f"❌ KIS WS InvalidStatusCode: HTTP {status}") - # Try HTTP GET diagnostic (replace wss:// -> https://) - if _httpx is not None: - try: - http_url = ws_full_url.replace('wss://', 'https://') - logger.info(f"πŸ” Performing HTTP GET diagnostic to {http_url}") - async with _httpx.AsyncClient(timeout=10.0, verify=True) as client: - # include Origin in diagnostic GET if available to mirror websocket handshake - diag_headers = {"User-Agent": "Mozilla/5.0"} - if origin: - diag_headers["Origin"] = origin - resp = await client.get(http_url, headers=diag_headers) - headers_snippet = dict(list(resp.headers.items())[:10]) - body_snippet = (resp.text or '')[:1000] - logger.error(f"πŸ” Diagnostic HTTP {resp.status_code} headers={headers_snippet} body={body_snippet}") - except Exception as diag_exc: - logger.error(f"πŸ” Diagnostic HTTP GET failed: {diag_exc}", exc_info=True) - else: - logger.error("πŸ” httpx not installed β€” cannot perform HTTP diagnostic") - # reset ws state and approval_key to attempt re-issue on next loop + # 진단 λ‘œκ·ΈλŠ” 처음 3회만 좜λ ₯ (둜그 도배 λ°©μ§€) + if connection_attempts <= 3: + logger.error(f"❌ KIS WS InvalidStatusCode: HTTP {status}") + if _httpx is not None: + try: + http_url = ws_full_url.replace('wss://', 'https://') + logger.info(f"πŸ” Performing HTTP GET diagnostic to {http_url}") + async with _httpx.AsyncClient(timeout=10.0, verify=True) as client: + diag_headers = {"User-Agent": "Mozilla/5.0"} + if origin: + diag_headers["Origin"] = origin + resp = await client.get(http_url, headers=diag_headers) + body_snippet = (resp.text or '')[:200] + logger.error(f"πŸ” Diagnostic HTTP {resp.status_code} body={body_snippet}") + except Exception as diag_exc: + logger.error(f"πŸ” Diagnostic HTTP GET failed: {diag_exc}") + elif connection_attempts == 4: + logger.warning( + f"⚠️ KIS WS μ„œλ²„ 비정상 (HTTP {status} λ°˜ν™˜). " + f"5λΆ„ κ°„κ²©μœΌλ‘œ μž¬μ‹œλ„ν•©λ‹ˆλ‹€. REST APIλŠ” 정상 λ™μž‘ν•©λ‹ˆλ‹€." + ) + # reset ws state self._ws = None self._subscribed_codes.clear() self._approval_key = None - # continue to retry after outer sleep - continue + # outer backoff둜 이동 (continue 제거) except ConnectionClosed as e: connection_attempts += 1