"""IP 단위 호출 제한 — 프로세스 메모리에만 있는 아주 단순한 것.""" import time from collections import defaultdict, deque _hits: dict[str, deque] = defaultdict(deque) def allow(key: str, limit: int, window_sec: float) -> bool: """key 가 window_sec 안에서 limit 번을 넘지 않았으면 True(그리고 1회로 센다).""" now = time.monotonic() bucket = _hits[key] while bucket and now - bucket[0] > window_sec: bucket.popleft() if len(bucket) >= limit: return False bucket.append(now) # 안 쓰는 키가 쌓이는 걸 막는다. if not bucket: _hits.pop(key, None) return True def reset(): """테스트 전용 — 프로세스 상태를 비운다.""" _hits.clear()