File size: 6,285 Bytes
042f4ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Rate limiter with Redis backend and in-memory fallback.

Uses Redis INCR + EXPIRE for multi-instance deployments. Falls back to
in-memory sliding window when Redis is unavailable.
"""

import logging
import os
import time
from collections import defaultdict

from fastapi import Depends, Request

from src.config import settings
from src.core.auth import get_current_user
from src.core.exceptions import RateLimitError

logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Redis backend (optional)
# ---------------------------------------------------------------------------

_redis_client = None
_redis_init_attempted = False


def _get_redis():
    """Lazy-init Redis connection. Returns None if unavailable."""
    global _redis_client, _redis_init_attempted
    if _redis_init_attempted:
        return _redis_client

    _redis_init_attempted = True
    redis_url = os.environ.get("REDIS_URL")
    if not redis_url:
        return None

    try:
        import redis
        _redis_client = redis.from_url(redis_url, decode_responses=True)
        _redis_client.ping()
        logger.info("Redis rate limiter connected")
        return _redis_client
    except Exception:
        logger.warning("Redis unavailable, falling back to in-memory rate limiter")
        _redis_client = None
        return None


# ---------------------------------------------------------------------------
# In-memory backend
# ---------------------------------------------------------------------------


class RateLimiter:
    """Sliding window rate limiter with Redis or in-memory backend."""

    def __init__(self, max_requests: int = 100, window_seconds: int = 60):
        self.max_requests = max_requests
        self.window_seconds = window_seconds
        self._requests: dict[str, list[float]] = defaultdict(list)

    def check(self, key: str) -> bool:
        r = _get_redis()
        if r is not None:
            return self._check_redis(r, key)
        return self._check_memory(key)

    def _check_redis(self, r, key: str) -> bool:
        redis_key = f"ratelimit:{key}"
        try:
            current = r.incr(redis_key)
            if current == 1:
                r.expire(redis_key, self.window_seconds)
            return current <= self.max_requests
        except Exception:
            logger.warning("Redis check failed, falling back to in-memory")
            return self._check_memory(key)

    def _check_memory(self, key: str) -> bool:
        now = time.monotonic()
        window_start = now - self.window_seconds
        timestamps = self._requests[key]
        self._requests[key] = [t for t in timestamps if t > window_start]

        if len(self._requests[key]) >= self.max_requests:
            return False

        self._requests[key].append(now)
        return True

    def get_limit_info(self, key: str) -> dict:
        r = _get_redis()
        if r is not None:
            return self._get_limit_info_redis(r, key)
        return self._get_limit_info_memory(key)

    def _get_limit_info_redis(self, r, key: str) -> dict:
        redis_key = f"ratelimit:{key}"
        try:
            current = int(r.get(redis_key) or 0)
            ttl = r.ttl(redis_key)
            remaining = max(0, self.max_requests - current)
            return {"remaining": remaining, "reset_in_seconds": max(ttl, 0)}
        except Exception:
            return self._get_limit_info_memory(key)

    def _get_limit_info_memory(self, key: str) -> dict:
        now = time.monotonic()
        window_start = now - self.window_seconds
        timestamps = self._requests.get(key, [])
        current = sum(1 for t in timestamps if t > window_start)
        remaining = max(0, self.max_requests - current)
        return {"remaining": remaining, "reset_in_seconds": self.window_seconds}


# ---------------------------------------------------------------------------
# Module-level singletons
# ---------------------------------------------------------------------------

_limiter: RateLimiter | None = None


def get_rate_limiter() -> RateLimiter:
    global _limiter
    if _limiter is None:
        _limiter = RateLimiter(
            max_requests=settings.rate_limit_requests,
            window_seconds=settings.rate_limit_window_seconds,
        )
    return _limiter


async def rate_limit_dependency(
    request: Request,
    current_user: dict = Depends(get_current_user),
) -> None:
    limiter = get_rate_limiter()
    key = current_user.get("id", "anonymous")
    allowed = limiter.check(key)
    info = limiter.get_limit_info(key)

    # Store rate limit info for response headers
    request.state.rate_limit_info = {
        "limit": limiter.max_requests,
        "remaining": info["remaining"],
        "reset": info["reset_in_seconds"],
    }

    if not allowed:
        raise RateLimitError(
            detail=f"Rate limit exceeded. Try again in {info['reset_in_seconds']} seconds"
        )


# Stricter limiter for unauthenticated endpoints (login, register)
_auth_limiter: RateLimiter | None = None


def _get_auth_limiter() -> RateLimiter:
    global _auth_limiter
    if _auth_limiter is None:
        _auth_limiter = RateLimiter(max_requests=20, window_seconds=60)
    return _auth_limiter


async def rate_limit_by_ip(request: Request) -> None:
    """Rate limit by client IP — for unauthenticated endpoints.

    Skipped during tests (pytest) to avoid false rate-limit hits from
    test fixtures that register/login many users from the same IP.
    """
    import sys
    if "pytest" in sys.modules:
        return
    # Extract real client IP from proxy headers (Cloudflare, reverse proxy)
    client_ip = (
        request.headers.get("cf-connecting-ip")
        or request.headers.get("x-forwarded-for", "").split(",")[0].strip()
        or (request.client.host if request.client else "unknown")
    )
    limiter = _get_auth_limiter()
    if not limiter.check(f"ip:{client_ip}"):
        info = limiter.get_limit_info(f"ip:{client_ip}")
        raise RateLimitError(
            detail=f"Too many requests. Try again in {info['reset_in_seconds']} seconds"
        )