-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrate_limiter.py
More file actions
337 lines (271 loc) · 13 KB
/
rate_limiter.py
File metadata and controls
337 lines (271 loc) · 13 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""
Discord Rate Limiter Module
Implements Discord's rate limiting requirements according to their documentation:
https://discord.com/developers/docs/topics/rate-limits
This module handles:
- Per-route rate limits with bucket tracking
- Global rate limits (50 requests/second for bots)
- Proper retry-after handling for 429 responses
- Rate limit header parsing and tracking
- Automatic backoff and retry logic
"""
import time
import threading
import requests
import json
from typing import Dict, Optional, Tuple, Any
from dataclasses import dataclass
from datetime import datetime, timedelta
import logging
@dataclass
class RateLimitBucket:
"""Represents a Discord rate limit bucket"""
limit: int = 0
remaining: int = 0
reset_time: float = 0.0
reset_after: float = 0.0
bucket_id: str = ""
last_request_time: float = 0.0
class DiscordRateLimiter:
"""
Discord-compliant rate limiter that handles:
- Per-route rate limits with bucket tracking
- Global rate limits (50 requests/second)
- Proper 429 retry handling
- Rate limit header parsing
"""
def __init__(self):
self.buckets: Dict[str, RateLimitBucket] = {}
self.global_rate_limit_reset = 0.0
self.global_requests_made = 0
self.global_window_start = time.time()
self.lock = threading.RLock()
# Global rate limit: 50 requests per second
self.global_limit = 50
self.global_window_size = 1.0
# Track invalid requests to avoid Cloudflare bans
self.invalid_requests_count = 0
self.invalid_requests_window_start = time.time()
self.max_invalid_requests = 9000 # Stay below 10,000 per 10 minutes
self.invalid_requests_window = 600
self.logger = logging.getLogger(__name__)
def _get_bucket_key(self, url: str, method: str = "POST") -> str:
"""
Generate a bucket key for rate limiting.
For webhooks, we use the webhook URL as the bucket key.
"""
if "/webhooks/" in url:
base_url = url.split("?")[0]
parts = base_url.split("/webhooks/")[1].split("/")
webhook_id = parts[0]
if len(parts) > 1 and parts[1]:
# Include token in bucket key for webhook+token endpoints
return f"{method}:webhook:{webhook_id}:{parts[1][:8]}"
else:
return f"{method}:webhook:{webhook_id}"
return f"{method}:{url}"
def _update_bucket_from_headers(self, bucket_key: str, headers: Dict[str, str]) -> RateLimitBucket:
"""Update bucket information from Discord response headers"""
with self.lock:
bucket = self.buckets.get(bucket_key, RateLimitBucket())
if "X-RateLimit-Limit" in headers:
bucket.limit = int(headers["X-RateLimit-Limit"])
if "X-RateLimit-Remaining" in headers:
bucket.remaining = int(headers["X-RateLimit-Remaining"])
if "X-RateLimit-Reset" in headers:
bucket.reset_time = float(headers["X-RateLimit-Reset"])
if "X-RateLimit-Reset-After" in headers:
bucket.reset_after = float(headers["X-RateLimit-Reset-After"])
if "X-RateLimit-Bucket" in headers:
bucket.bucket_id = headers["X-RateLimit-Bucket"]
bucket.last_request_time = time.time()
self.buckets[bucket_key] = bucket
return bucket
def _check_global_rate_limit(self) -> Optional[float]:
"""
Check global rate limit (50 requests/second).
Returns wait time in seconds if rate limited, None otherwise.
"""
with self.lock:
current_time = time.time()
# Reset window if needed
if current_time - self.global_window_start >= self.global_window_size:
self.global_requests_made = 0
self.global_window_start = current_time
# Check if we're at the limit
if self.global_requests_made >= self.global_limit:
wait_time = self.global_window_size - (current_time - self.global_window_start)
if wait_time > 0:
return wait_time
else:
# Window expired, reset
self.global_requests_made = 0
self.global_window_start = current_time
return None
def _check_bucket_rate_limit(self, bucket_key: str) -> Optional[float]:
"""
Check bucket-specific rate limit.
Returns wait time in seconds if rate limited, None otherwise.
"""
with self.lock:
bucket = self.buckets.get(bucket_key)
if not bucket:
return None
current_time = time.time()
# Check if bucket has reset
if current_time >= bucket.reset_time:
bucket.remaining = bucket.limit
return None
if bucket.remaining <= 0:
wait_time = bucket.reset_time - current_time
return max(wait_time, 0)
return None
def _increment_global_counter(self):
with self.lock:
self.global_requests_made += 1
def _decrement_bucket_remaining(self, bucket_key: str):
with self.lock:
bucket = self.buckets.get(bucket_key)
if bucket and bucket.remaining > 0:
bucket.remaining -= 1
def _handle_invalid_request(self, status_code: int):
"""Track invalid requests to avoid Cloudflare bans"""
if status_code in [401, 403, 429]:
with self.lock:
current_time = time.time()
# Reset window if needed
if current_time - self.invalid_requests_window_start >= self.invalid_requests_window:
self.invalid_requests_count = 0
self.invalid_requests_window_start = current_time
self.invalid_requests_count += 1
# Log warning if approaching limit
if self.invalid_requests_count > self.max_invalid_requests * 0.8:
self.logger.warning(
f"High invalid request count: {self.invalid_requests_count}/{self.max_invalid_requests}. "
f"Risk of Cloudflare ban."
)
def wait_for_rate_limit(self, url: str, method: str = "POST") -> bool:
"""
Wait for rate limits before making a request.
Returns True if request can proceed, False if should be aborted.
"""
bucket_key = self._get_bucket_key(url, method)
global_wait = self._check_global_rate_limit()
if global_wait:
self.logger.info(f"Global rate limit hit, waiting {global_wait:.2f} seconds")
time.sleep(global_wait)
bucket_wait = self._check_bucket_rate_limit(bucket_key)
if bucket_wait:
self.logger.info(f"Bucket rate limit hit for {bucket_key}, waiting {bucket_wait:.2f} seconds")
time.sleep(bucket_wait)
return True
def handle_response(self, response: requests.Response, url: str, method: str = "POST") -> Tuple[bool, Optional[float]]:
"""
Handle Discord API response and update rate limit tracking.
Returns:
(should_retry, wait_time):
- should_retry: True if request should be retried
- wait_time: Time to wait before retry (None if no retry needed)
"""
bucket_key = self._get_bucket_key(url, method)
# Update rate limit tracking from headers
self._update_bucket_from_headers(bucket_key, dict(response.headers))
# Track request counts
self._increment_global_counter()
if response.status_code == 429:
self._handle_invalid_request(429)
try:
retry_data = response.json()
retry_after = retry_data.get("retry_after", 1.0)
is_global = retry_data.get("global", False)
if is_global:
self.logger.warning(f"Global rate limit exceeded, waiting {retry_after} seconds")
with self.lock:
self.global_rate_limit_reset = time.time() + retry_after
else:
self.logger.warning(f"Bucket rate limit exceeded for {bucket_key}, waiting {retry_after} seconds")
return True, retry_after
except (json.JSONDecodeError, KeyError):
retry_after = float(response.headers.get("Retry-After", 1.0))
self.logger.warning(f"Rate limit exceeded, waiting {retry_after} seconds (from headers)")
return True, retry_after
elif response.status_code in [401, 403]:
self._handle_invalid_request(response.status_code)
return False, None
elif response.status_code == 404:
self.logger.error("Webhook not found (404) - check webhook URL")
return False, None
elif 500 <= response.status_code < 600:
# Server error - retry with exponential backoff
return True, min(2.0 ** (response.status_code - 500), 60.0)
else:
# Success or other status - decrement bucket counter
if response.status_code < 400:
self._decrement_bucket_remaining(bucket_key)
return False, None
def make_request_with_rate_limiting(self,
url: str,
method: str = "POST",
max_retries: int = 5,
**kwargs) -> Optional[requests.Response]:
"""
Make a rate-limited request to Discord API.
Args:
url: Discord API URL
method: HTTP method
max_retries: Maximum number of retry attempts
**kwargs: Additional arguments for requests
Returns:
Response object if successful, None if failed permanently
"""
for attempt in range(max_retries + 1):
if not self.wait_for_rate_limit(url, method):
return None
try:
response = requests.request(method, url, timeout=30, **kwargs)
should_retry, wait_time = self.handle_response(response, url, method)
if not should_retry:
return response
if attempt < max_retries and wait_time:
self.logger.info(f"Retrying request in {wait_time:.2f} seconds (attempt {attempt + 1}/{max_retries})")
time.sleep(wait_time)
continue
if attempt >= max_retries:
self.logger.error(f"Max retries ({max_retries}) exceeded for {url}")
return response
except requests.exceptions.RequestException as e:
self.logger.error(f"Request exception on attempt {attempt + 1}: {e}")
if attempt < max_retries:
wait_time = min(2.0 ** attempt, 60.0)
time.sleep(wait_time)
continue
else:
return None
return None
def get_rate_limit_status(self) -> Dict[str, Any]:
"""Get current rate limit status for monitoring"""
with self.lock:
current_time = time.time()
return {
"global": {
"requests_made": self.global_requests_made,
"limit": self.global_limit,
"window_remaining": max(0, self.global_window_size - (current_time - self.global_window_start)),
"reset_time": self.global_rate_limit_reset if self.global_rate_limit_reset > current_time else None
},
"buckets": {
bucket_key: {
"limit": bucket.limit,
"remaining": bucket.remaining,
"reset_time": bucket.reset_time,
"reset_after": max(0, bucket.reset_time - current_time) if bucket.reset_time > current_time else 0
}
for bucket_key, bucket in self.buckets.items()
},
"invalid_requests": {
"count": self.invalid_requests_count,
"limit": self.max_invalid_requests,
"window_remaining": max(0, self.invalid_requests_window - (current_time - self.invalid_requests_window_start))
}
}
discord_rate_limiter = DiscordRateLimiter()