File size: 11,508 Bytes
daf23ba
 
 
9730fbc
daf23ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""Distributed rate limiting service using Redis"""

import time
from src.core import json_utils
from typing import Dict, Optional, Tuple
from datetime import datetime, timedelta
import redis.asyncio as redis
from redis.exceptions import RedisError

from src.core.config import settings
from src.core import get_logger

logger = get_logger(__name__)


class DistributedRateLimiter:
    """Distributed rate limiter using Redis with sliding window algorithm"""
    
    def __init__(
        self,
        redis_url: Optional[str] = None,
        requests_per_minute: int = 60,
        requests_per_hour: int = 600,
        requests_per_day: int = 10000,
        burst_size: int = 10
    ):
        self.redis_url = redis_url or settings.REDIS_URL
        self.requests_per_minute = requests_per_minute
        self.requests_per_hour = requests_per_hour
        self.requests_per_day = requests_per_day
        self.burst_size = burst_size
        self._redis_client = None
        
    async def get_redis(self) -> redis.Redis:
        """Get Redis client with lazy initialization"""
        if self._redis_client is None:
            self._redis_client = redis.from_url(
                self.redis_url,
                encoding="utf-8",
                decode_responses=True,
                socket_keepalive=True,
                health_check_interval=30
            )
        return self._redis_client
    
    async def is_allowed(
        self, 
        identifier: str, 
        endpoint: Optional[str] = None
    ) -> Tuple[bool, Dict[str, any]]:
        """
        Check if request is allowed using sliding window algorithm
        
        Args:
            identifier: Unique identifier (IP address or user ID)
            endpoint: Optional endpoint for per-endpoint limits
            
        Returns:
            Tuple of (allowed, rate_info)
        """
        try:
            redis_client = await self.get_redis()
            current_time = time.time()
            
            # Create key prefix
            key_prefix = f"rate_limit:{identifier}"
            if endpoint:
                key_prefix += f":{endpoint}"
            
            # Keys for different windows
            minute_key = f"{key_prefix}:minute"
            hour_key = f"{key_prefix}:hour"
            day_key = f"{key_prefix}:day"
            burst_key = f"{key_prefix}:burst"
            
            # Use pipeline for atomic operations
            async with redis_client.pipeline() as pipe:
                # Get current counts
                pipe.zcount(minute_key, current_time - 60, current_time)
                pipe.zcount(hour_key, current_time - 3600, current_time)
                pipe.zcount(day_key, current_time - 86400, current_time)
                pipe.get(burst_key)
                
                results = await pipe.execute()
                
                minute_count = results[0]
                hour_count = results[1]
                day_count = results[2]
                burst_tokens = int(results[3] or self.burst_size)
            
            # Check limits
            if minute_count >= self.requests_per_minute:
                return False, {
                    "reason": "minute_limit_exceeded",
                    "limit": self.requests_per_minute,
                    "count": minute_count,
                    "reset_in": 60 - (current_time % 60)
                }
            
            if hour_count >= self.requests_per_hour:
                return False, {
                    "reason": "hour_limit_exceeded",
                    "limit": self.requests_per_hour,
                    "count": hour_count,
                    "reset_in": 3600 - (current_time % 3600)
                }
            
            if day_count >= self.requests_per_day:
                return False, {
                    "reason": "day_limit_exceeded",
                    "limit": self.requests_per_day,
                    "count": day_count,
                    "reset_in": 86400 - (current_time % 86400)
                }
            
            # Check burst limit
            if burst_tokens <= 0:
                return False, {
                    "reason": "burst_limit_exceeded",
                    "limit": self.burst_size,
                    "count": 0,
                    "reset_in": 1
                }
            
            # Request allowed - update counts
            request_id = f"{current_time}:{identifier}"
            
            async with redis_client.pipeline() as pipe:
                # Add to sliding windows
                pipe.zadd(minute_key, {request_id: current_time})
                pipe.zadd(hour_key, {request_id: current_time})
                pipe.zadd(day_key, {request_id: current_time})
                
                # Update burst tokens
                pipe.decr(burst_key)
                
                # Set expiration times
                pipe.expire(minute_key, 120)  # 2 minutes
                pipe.expire(hour_key, 7200)   # 2 hours
                pipe.expire(day_key, 172800)  # 2 days
                pipe.expire(burst_key, 60)    # 1 minute
                
                # Clean old entries
                pipe.zremrangebyscore(minute_key, 0, current_time - 60)
                pipe.zremrangebyscore(hour_key, 0, current_time - 3600)
                pipe.zremrangebyscore(day_key, 0, current_time - 86400)
                
                await pipe.execute()
            
            # Replenish burst tokens over time
            await self._replenish_burst_tokens(burst_key, burst_tokens)
            
            return True, {
                "allowed": True,
                "minute_count": minute_count + 1,
                "hour_count": hour_count + 1,
                "day_count": day_count + 1,
                "burst_remaining": burst_tokens - 1,
                "limits": {
                    "per_minute": self.requests_per_minute,
                    "per_hour": self.requests_per_hour,
                    "per_day": self.requests_per_day,
                    "burst": self.burst_size
                }
            }
            
        except RedisError as e:
            logger.error(f"Redis error in rate limiting: {e}")
            # Fail open - allow request if Redis is down
            return True, {"error": "rate_limit_unavailable"}
        except Exception as e:
            logger.error(f"Unexpected error in rate limiting: {e}")
            return True, {"error": "rate_limit_error"}
    
    async def _replenish_burst_tokens(self, burst_key: str, current_tokens: int):
        """Replenish burst tokens over time"""
        if current_tokens < self.burst_size:
            try:
                redis_client = await self.get_redis()
                # Replenish 1 token per second up to burst_size
                await redis_client.set(
                    burst_key, 
                    min(current_tokens + 1, self.burst_size),
                    ex=60,
                    nx=True  # Only set if doesn't exist
                )
            except Exception:
                pass
    
    async def reset_limits(self, identifier: str, endpoint: Optional[str] = None):
        """Reset rate limits for an identifier"""
        try:
            redis_client = await self.get_redis()
            key_prefix = f"rate_limit:{identifier}"
            if endpoint:
                key_prefix += f":{endpoint}"
            
            keys = [
                f"{key_prefix}:minute",
                f"{key_prefix}:hour",
                f"{key_prefix}:day",
                f"{key_prefix}:burst"
            ]
            
            if keys:
                await redis_client.delete(*keys)
                
        except Exception as e:
            logger.error(f"Error resetting limits: {e}")
    
    async def get_limit_status(self, identifier: str, endpoint: Optional[str] = None) -> Dict[str, any]:
        """Get current limit status for an identifier"""
        try:
            redis_client = await self.get_redis()
            current_time = time.time()
            
            key_prefix = f"rate_limit:{identifier}"
            if endpoint:
                key_prefix += f":{endpoint}"
            
            minute_key = f"{key_prefix}:minute"
            hour_key = f"{key_prefix}:hour"
            day_key = f"{key_prefix}:day"
            burst_key = f"{key_prefix}:burst"
            
            async with redis_client.pipeline() as pipe:
                pipe.zcount(minute_key, current_time - 60, current_time)
                pipe.zcount(hour_key, current_time - 3600, current_time)
                pipe.zcount(day_key, current_time - 86400, current_time)
                pipe.get(burst_key)
                
                results = await pipe.execute()
            
            return {
                "minute": {
                    "used": results[0],
                    "limit": self.requests_per_minute,
                    "remaining": self.requests_per_minute - results[0]
                },
                "hour": {
                    "used": results[1],
                    "limit": self.requests_per_hour,
                    "remaining": self.requests_per_hour - results[1]
                },
                "day": {
                    "used": results[2],
                    "limit": self.requests_per_day,
                    "remaining": self.requests_per_day - results[2]
                },
                "burst": {
                    "remaining": int(results[3] or self.burst_size),
                    "limit": self.burst_size
                }
            }
            
        except Exception as e:
            logger.error(f"Error getting limit status: {e}")
            return {}
    
    async def cleanup_old_entries(self):
        """Clean up old entries from all rate limit keys"""
        try:
            redis_client = await self.get_redis()
            current_time = time.time()
            
            # Scan for rate limit keys
            cursor = 0
            while True:
                cursor, keys = await redis_client.scan(
                    cursor, 
                    match="rate_limit:*", 
                    count=100
                )
                
                for key in keys:
                    if key.endswith(":minute"):
                        await redis_client.zremrangebyscore(key, 0, current_time - 60)
                    elif key.endswith(":hour"):
                        await redis_client.zremrangebyscore(key, 0, current_time - 3600)
                    elif key.endswith(":day"):
                        await redis_client.zremrangebyscore(key, 0, current_time - 86400)
                
                if cursor == 0:
                    break
                    
        except Exception as e:
            logger.error(f"Error cleaning up old entries: {e}")
    
    async def close(self):
        """Close Redis connection"""
        if self._redis_client:
            await self._redis_client.close()


# Singleton instance
_rate_limiter = None

def get_rate_limiter() -> DistributedRateLimiter:
    """Get singleton rate limiter instance"""
    global _rate_limiter
    if _rate_limiter is None:
        _rate_limiter = DistributedRateLimiter(
            requests_per_minute=settings.RATE_LIMIT_PER_MINUTE,
            requests_per_hour=settings.RATE_LIMIT_PER_HOUR,
            requests_per_day=settings.RATE_LIMIT_PER_DAY
        )
    return _rate_limiter