""" WebSocket 재연결 대기 간격 — **REST 아님** (소켓 run_forever 끊김 후 재접속 전 sleep). 기본 시퀀스: 1 → 3 → 5 → 7 → 10초, 이후 10초 유지. env ``WS_RECONNECT_DELAY_SECS`` (쉼표 구분, DB/env_config). """ from __future__ import annotations from typing import List, Tuple from ..utils.env import get_env_from_db _DEFAULT_DELAYS: Tuple[float, ...] = (1.0, 3.0, 5.0, 7.0, 10.0) _cache: Tuple[float, ...] | None = None def invalidate_ws_reconnect_delay_cache() -> None: global _cache _cache = None def ws_reconnect_delays_sec() -> Tuple[float, ...]: global _cache if _cache is None: raw = ( get_env_from_db("WS_RECONNECT_DELAY_SECS", "1,3,5,7,10") or "1,3,5,7,10" ).strip() parts: List[float] = [] for piece in raw.split(","): piece = piece.strip() if not piece: continue try: val = float(piece) except ValueError: continue if val >= 0: parts.append(val) _cache = tuple(parts) if parts else _DEFAULT_DELAYS return _cache def ws_reconnect_delay_for_attempt(attempt: int) -> float: """1-based 재연결 시도 번호 → 대기 초. 시퀀스 초과 시 마지막 값.""" if attempt < 1: attempt = 1 delays = ws_reconnect_delays_sec() idx = min(attempt - 1, len(delays) - 1) return delays[idx]