feat: realtime_hub 장외 대기 적응형(개장 임박만 촘촘히)

고정 60s 폴링 → 평일 07:00~08:00(NXT 프리마켓 08:00 임박)만 60s,
그 외 장외(밤·주말·휴장)는 30분. 밤새 무의미한 1분 폴링 제거.
30분 상한은 자가교정용(개장 오버슬립 방지). 휴장·주말은 phase!=closed 라 자동 30분.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
hyowons
2026-06-08 14:42:34 +09:00
parent b813d01cfa
commit 4ae10b50fc
+18 -4
View File
@@ -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()