-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_rate_limiter.py
More file actions
77 lines (60 loc) · 2.76 KB
/
Copy pathdebug_rate_limiter.py
File metadata and controls
77 lines (60 loc) · 2.76 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
#!/usr/bin/env python3
"""Debug aid for TokenBucketRateLimiter.
Runs a pair of requests against a minimal config and prints bucket,
refill, history, and block status to help diagnose rate-limit behavior.
"""
# pylint: disable=protected-access
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from src.api_rate_limiter import TokenBucketRateLimiter, RateLimitConfig # noqa: E402
def debug_rate_limiter():
"""Debug the rate limiter behavior."""
print("🔍 Debugging Rate Limiter Issue")
print("=" * 50)
# Create config with minimal settings (same as test)
config = RateLimitConfig(requests_per_minute=1, burst_size=1)
print(
"Config: requests_per_minute="
f"{config.requests_per_minute}, "
f"burst_size={config.burst_size}"
)
rate_limiter = TokenBucketRateLimiter(config)
print(f"Initial buckets: {rate_limiter.buckets}")
print(f"Initial last_refill: {rate_limiter.last_refill}")
# Test first request
print("\n🚀 Testing First Request...")
client_ip = "127.0.0.1"
user_agent = ""
allowed1, reason1, meta1 = rate_limiter.allow_request(client_ip, user_agent)
print(f"First request - Allowed: {allowed1}, Reason: {reason1}")
print(f"Meta: {meta1}")
print(f"Buckets after first request: {rate_limiter.buckets}")
print(f"Last refill after first request: {rate_limiter.last_refill}")
# Test second request
print("\n🚀 Testing Second Request...")
allowed2, reason2, meta2 = rate_limiter.allow_request(client_ip, user_agent)
print(f"Second request - Allowed: {allowed2}, Reason: {reason2}")
print(f"Meta: {meta2}")
print(f"Buckets after second request: {rate_limiter.buckets}")
# Check what's in the bucket for this client
client_key = (
meta1.get("client_key")
or rate_limiter._get_client_key(client_ip, user_agent)
)
print(f"\n🔑 Client key: {client_key}")
print(f"Bucket value for client: {rate_limiter.buckets[client_key]}")
print(f"Last refill time for client: {rate_limiter.last_refill[client_key]}")
# Check if client is blocked
print(f"Client blocked: {rate_limiter._is_client_blocked(client_key)}")
print(f"Blocked clients: {rate_limiter.blocked_clients}")
# Check concurrent requests
print(f"Concurrent requests: {rate_limiter.concurrent_requests}")
# Check request history
print(f"Request history: {list(rate_limiter.request_history[client_key])}")
# Release the concurrent slots to keep metrics accurate across runs
rate_limiter.release_request(client_ip, user_agent)
rate_limiter.release_request(client_ip, user_agent)
print(f"After release - concurrent requests: {rate_limiter.concurrent_requests}")
if __name__ == "__main__":
debug_rate_limiter()