"""Thread-safe in-memory sliding-window rate limiter for HTTP route handlers."""
from __future__ import annotations

import threading
import time


class SlidingWindowRateLimiter:
    """Allow at most *max_calls* events per *window_seconds* sliding wall-clock window."""

    def __init__(self, max_calls: int = 10, window_seconds: float = 60.0) -> None:
        self._max = max_calls
        self._window = window_seconds
        self._timestamps: list[float] = []
        self._lock = threading.Lock()

    def allow(self) -> bool:
        now = time.time()
        cutoff = now - self._window
        with self._lock:
            self._timestamps = [t for t in self._timestamps if t > cutoff]
            if len(self._timestamps) >= self._max:
                return False
            self._timestamps.append(now)
            return True
