fix: 하드닝 루프 8회차 — 체결 보정·고아 감지·청산 경보·실시간/차트·리스크 가드 일괄 수정
- 스윙 체결가 확인 전 executed_price NULL + fill_sweep, 진입가 resolve(ActiveTrade↔Portfolio), 0체결/부분체결 매수 장부 원복 - DT 고아 감지(근거 한정)·0체결 매도 복원·stale 점검 확대, 시간손절 -0.3% 원복 - 폴백 손절 ATR 캐시, 거래량부족-차단 신호 매수 차단, 현금 5% 유보 재적용, MDD peak/reset 수정 - KIS 서킷브레이커 청산 우회·경보, db_worker 재큐잉 순서, heartbeat·health-watch 사이드카 - 실시간 WS 재구독·41건 우선순위·다건 프레임 분할, 차트 샘플 제거·당일봉 합성, WS 서브프로토콜 인증 - 프론트: 미관리 그룹·확인 모달·차트 실시간 반영·예수금 표시 정정 - 전문가 에이전트 6종 추가(.claude/agents) - 유닛 스위트 2667 passed (소켓 차단 하), tsc OK Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y83Za98XsNw9zuA4FTUytN
This commit is contained in:
@@ -7,24 +7,35 @@ WebSocket 연결 관리 모듈
|
||||
- 싱글톤 websocket_manager 인스턴스 제공 (main.py 에서 import)
|
||||
"""
|
||||
from typing import Dict, Set
|
||||
import asyncio
|
||||
from fastapi import WebSocket
|
||||
from app.core.json_helpers import _dumps
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# [L-6] 실시간 시세 전송 1건당 클라이언트별 타임아웃(초). 초과 = 정체된 클라이언트 → 연결 해제.
|
||||
_MARKET_UPDATE_SEND_TIMEOUT = 5.0
|
||||
|
||||
|
||||
class WebSocketManager:
|
||||
def __init__(self):
|
||||
self.active_connections: Dict[str, WebSocket] = {}
|
||||
# 클라이언트별 구독 종목 세트
|
||||
self._subscriptions: Dict[str, Set[str]] = {}
|
||||
# 클라이언트별 "선택 종목"(차트를 보고 있는 종목) — KIS 등록 우선순위·호가 구독 대상(H-4)
|
||||
self._selected: Dict[str, Set[str]] = {}
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 연결 관리
|
||||
# ------------------------------------------------------------------
|
||||
async def connect(self, websocket: WebSocket, client_id: str):
|
||||
"""클라이언트 연결.
|
||||
"""클라이언트 연결 (서브프로토콜 없음 — connect_v2 위임)."""
|
||||
await self.connect_v2(websocket, client_id)
|
||||
|
||||
async def connect_v2(self, websocket: WebSocket, client_id: str, subprotocol: str | None = None):
|
||||
"""클라이언트 연결. subprotocol 이 있으면 accept(subprotocol=...) 로 응답
|
||||
(브라우저는 요청한 서브프로토콜이 응답에 없으면 연결을 끊는다).
|
||||
|
||||
동일 client_id로 재접속 시 active_connections를 교체.
|
||||
이전 핸들러는 websocket_endpoint의 루프에서 교체 감지 후 자연 종료.
|
||||
@@ -33,7 +44,7 @@ class WebSocketManager:
|
||||
if client_id in self.active_connections:
|
||||
logger.info(f"Client {client_id}: 기존 연결 교체 (이전 핸들러 자연 종료 대기)")
|
||||
|
||||
await websocket.accept()
|
||||
await websocket.accept(subprotocol=subprotocol)
|
||||
self.active_connections[client_id] = websocket
|
||||
self._subscriptions.setdefault(client_id, set())
|
||||
logger.info(f"Client {client_id} connected. Total: {len(self.active_connections)}")
|
||||
@@ -42,6 +53,7 @@ class WebSocketManager:
|
||||
"""클라이언트 연결 해제"""
|
||||
self.active_connections.pop(client_id, None)
|
||||
self._subscriptions.pop(client_id, None)
|
||||
self._selected.pop(client_id, None)
|
||||
logger.info(f"Client {client_id} disconnected. Total: {len(self.active_connections)}")
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
@@ -64,6 +76,18 @@ class WebSocketManager:
|
||||
self._subscriptions[client_id] = set(stock_codes)
|
||||
logger.debug(f"[sub=] {client_id}: 구독 목록 재설정 → {sorted(stock_codes)}")
|
||||
|
||||
def set_selected_codes_v2(self, client_id: str, stock_codes: list[str]):
|
||||
"""클라이언트의 선택 종목(차트 표시 종목)을 전체 교체."""
|
||||
self._selected[client_id] = {str(c) for c in stock_codes if c}
|
||||
|
||||
def get_selected_codes_v2(self) -> Set[str]:
|
||||
"""활성 클라이언트의 선택 종목 합집합 (해당 클라이언트가 실제 구독 중인 종목만)."""
|
||||
result: Set[str] = set()
|
||||
for client_id in self.active_connections:
|
||||
subs = self._subscriptions.get(client_id, set())
|
||||
result.update(c for c in self._selected.get(client_id, set()) if c in subs)
|
||||
return result
|
||||
|
||||
def get_subscriptions(self, client_id: str) -> list[str]:
|
||||
"""클라이언트의 현재 구독 목록 반환."""
|
||||
return sorted(self._subscriptions.get(client_id, set()))
|
||||
@@ -108,34 +132,45 @@ class WebSocketManager:
|
||||
|
||||
async def send_market_update(self, stock_code: str, data: dict):
|
||||
"""시장 데이터를 해당 종목을 구독한 클라이언트에게만 전송.
|
||||
구독자가 없으면 전체 브로드캐스트(기존 동작 유지).
|
||||
|
||||
[L-5] 구독자가 없으면 아무에게도 보내지 않는다(과거: 전체 브로드캐스트 폴백 —
|
||||
구독하지 않은 종목 틱이 모든 화면에 쏟아졌다).
|
||||
[L-6] 클라이언트별 전송을 병렬 + 타임아웃으로 — 느린 클라이언트 1개가 KIS 분배를 막지 않게.
|
||||
"""
|
||||
message = _dumps({
|
||||
"type": "market_update",
|
||||
"stock_code": stock_code,
|
||||
"data": data,
|
||||
})
|
||||
# 해당 종목 구독 클라이언트 필터링
|
||||
targets = [
|
||||
cid for cid, codes in self._subscriptions.items()
|
||||
if stock_code in codes and cid in self.active_connections
|
||||
]
|
||||
if not targets:
|
||||
# 구독자 없음 — 전체 브로드캐스트(fallback)
|
||||
await self.broadcast(message)
|
||||
return
|
||||
disconnected = []
|
||||
for cid in targets:
|
||||
message = _dumps({
|
||||
"type": "market_update",
|
||||
"stock_code": stock_code,
|
||||
"data": data,
|
||||
})
|
||||
|
||||
async def _send_one(cid: str):
|
||||
ws = self.active_connections.get(cid)
|
||||
if not ws:
|
||||
continue
|
||||
return None
|
||||
try:
|
||||
await ws.send_text(message)
|
||||
await asyncio.wait_for(ws.send_text(message), timeout=_MARKET_UPDATE_SEND_TIMEOUT)
|
||||
return None
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(f"send_market_update 타임아웃({_MARKET_UPDATE_SEND_TIMEOUT}s) → {cid} 연결 해제")
|
||||
return (cid, ws)
|
||||
except Exception as e:
|
||||
logger.error(f"send_market_update error → {cid}: {e}")
|
||||
disconnected.append(cid)
|
||||
for cid in disconnected:
|
||||
self.disconnect(cid)
|
||||
return (cid, ws)
|
||||
|
||||
results = await asyncio.gather(*(_send_one(cid) for cid in targets))
|
||||
for item in results:
|
||||
if not item:
|
||||
continue
|
||||
cid, ws = item
|
||||
# 전송 중 같은 client_id 로 재접속했으면 새 연결은 건드리지 않는다
|
||||
if self.active_connections.get(cid) is ws:
|
||||
self.disconnect(cid)
|
||||
|
||||
async def send_trade_notification(self, trade_data: dict):
|
||||
"""거래 알림 전송"""
|
||||
|
||||
Reference in New Issue
Block a user