From 4ae10b50fcbf4e8b6def741d6eeeac167fbf9ca2 Mon Sep 17 00:00:00 2001 From: hyowons Date: Mon, 8 Jun 2026 14:42:34 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20realtime=5Fhub=20=EC=9E=A5=EC=99=B8=20?= =?UTF-8?q?=EB=8C=80=EA=B8=B0=20=EC=A0=81=EC=9D=91=ED=98=95(=EA=B0=9C?= =?UTF-8?q?=EC=9E=A5=20=EC=9E=84=EB=B0=95=EB=A7=8C=20=EC=B4=98=EC=B4=98?= =?UTF-8?q?=ED=9E=88)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 고정 60s 폴링 → 평일 07:00~08:00(NXT 프리마켓 08:00 임박)만 60s, 그 외 장외(밤·주말·휴장)는 30분. 밤새 무의미한 1분 폴링 제거. 30분 상한은 자가교정용(개장 오버슬립 방지). 휴장·주말은 phase!=closed 라 자동 30분. Co-Authored-By: Claude Opus 4.8 --- .../stock/workspace/scripts/realtime_hub.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/agents/stock/workspace/scripts/realtime_hub.py b/agents/stock/workspace/scripts/realtime_hub.py index c489536..10441c2 100644 --- a/agents/stock/workspace/scripts/realtime_hub.py +++ b/agents/stock/workspace/scripts/realtime_hub.py @@ -45,8 +45,9 @@ _BACKOFF_MAX = 30.0 _MAX_CONN_AGE = 25 * 60 # recv 타임아웃 — 이 시간 내 아무것도 안 오면 루프 돌며 stop/age 체크 _RECV_TIMEOUT = 5.0 -# 장 마감 시간대 재확인 주기 — 연결 안 하고 대기하며 이 간격으로 개장 여부 폴링 -_CLOSED_POLL_SEC = 60.0 +# 장 마감 시간대 대기 주기 — 개장 임박(평일 07:00~08:00, NXT 프리마켓 08:00 직전)엔 촘촘히, 그 외(밤·주말·휴장)엔 드물게. +_PREOPEN_POLL_SEC = 60.0 # 개장 임박: 1분마다 → 08:00 개장을 1분 안에 포착 +_DEEP_CLOSED_POLL_SEC = 1800.0 # 밤·주말·휴장: 30분마다 (자가교정 상한, 헛바퀴 최소화) def _ws_url() -> str: @@ -146,13 +147,26 @@ class RealtimeHub: except Exception: return True + def _closed_wait_seconds(self) -> float: + """장 외 대기 시간. 평일 장외(closed)이고 NXT 프리마켓(08:00) 임박(07:00~08:00)이면 60s, + 그 외(밤·주말·휴장)는 30분. 휴장·주말은 phase가 'closed' 아니라 자동으로 30분.""" + try: + import behive_web + phase = behive_web._market_phase_state().get('phase') + hm = datetime.now(KST).hour * 60 + datetime.now(KST).minute + if phase == 'closed' and 7 * 60 <= hm < 8 * 60: + return _PREOPEN_POLL_SEC + except Exception: + return _PREOPEN_POLL_SEC # 실패 시 보수적으로 자주 확인 + return _DEEP_CLOSED_POLL_SEC + def _run(self) -> None: backoff = _BACKOFF_START while not self._stop.is_set(): if not self._market_open(): - # 장 외(주말·휴장·장외 시간) → 연결 안 하고 대기. 재연결 폭주·로그 노이즈 방지. + # 장 외 → 연결 안 하고 대기. 개장 임박엔 촘촘히, 밤·주말·휴장엔 드물게(헛바퀴 최소화). self._connected.clear() - self._stop.wait(_CLOSED_POLL_SEC) + self._stop.wait(self._closed_wait_seconds()) continue try: self._connect_once()