forked from prawinkumar1506/CatchMe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_pool.py
More file actions
92 lines (80 loc) · 3.95 KB
/
Copy pathapi_pool.py
File metadata and controls
92 lines (80 loc) · 3.95 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
import os
import time
from threading import Lock
from collections import defaultdict
class APIKeyPool:
def __init__(self):
self.keys = self._load_keys()
self.usage = defaultdict(int) # key -> request_count
self.total_usage = defaultdict(int) # key -> total requests ever
self.last_request = defaultdict(float) # key -> last request time
self.lock = Lock()
self.rate_limit = 25 # Adjusted for 12 teams
self.window = 60 # seconds
self.min_delay = 2.0 # Minimum 2 seconds between requests per key
self.current_index = 0 # Strict round-robin
self.last_reset = time.time()
print(f"API Pool initialized with {len(self.keys)} keys")
def _load_keys(self):
keys = []
# Load multiple keys from environment
for i in range(1, 11): # Support up to 10 keys
key = os.getenv(f"GROQ_API_KEY_{i}")
if key:
keys.append(key)
print(f"✅ Loaded GROQ_API_KEY_{i}")
else:
print(f"❌ GROQ_API_KEY_{i} not found")
# Fallback to single key
if not keys:
main_key = os.getenv("GROQ_API_KEY")
if main_key:
keys.append(main_key)
print(f"✅ Loaded fallback GROQ_API_KEY")
return keys
def get_available_key(self):
with self.lock:
current_time = time.time()
# Reset counters every minute
if current_time - self.last_reset > self.window:
self._print_usage_stats()
self.usage.clear()
self.last_reset = current_time
# Strict round-robin - always use next key in sequence
key_index = self.current_index
key = self.keys[key_index]
# Check if current key is under limit AND enough time has passed
time_since_last = current_time - self.last_request[key]
if (self.usage[key] < self.rate_limit and time_since_last >= self.min_delay):
self.usage[key] += 1
self.total_usage[key] += 1
self.last_request[key] = current_time
# Always move to next key
self.current_index = (self.current_index + 1) % len(self.keys)
print(f"✅ API Key {key_index + 1}: {self.usage[key]}/{self.rate_limit} (Total: {self.total_usage[key]})")
return key
else:
print(f"⏳ Key {key_index + 1} blocked: usage={self.usage[key]}/{self.rate_limit}, delay={time_since_last:.1f}s/{self.min_delay}s")
# If current key exhausted, try others
for i in range(1, len(self.keys)):
key_index = (self.current_index + i) % len(self.keys)
key = self.keys[key_index]
time_since_last = current_time - self.last_request[key]
if (self.usage[key] < self.rate_limit and time_since_last >= self.min_delay):
self.usage[key] += 1
self.total_usage[key] += 1
self.last_request[key] = current_time
self.current_index = (key_index + 1) % len(self.keys)
print(f"✅ API Key {key_index + 1}: {self.usage[key]}/{self.rate_limit} (Total: {self.total_usage[key]})")
return key
else:
print(f"⏳ Key {key_index + 1} blocked: usage={self.usage[key]}/{self.rate_limit}, delay={time_since_last:.1f}s/{self.min_delay}s")
print("⚠️ ALL API KEYS EXHAUSTED - Using fallback")
return None
def _print_usage_stats(self):
print("\n📊 API Usage Distribution (Last Minute):")
for i, key in enumerate(self.keys):
print(f"Key {i+1}: {self.usage[key]} requests (Total: {self.total_usage[key]})")
print()
# Global instance
api_pool = APIKeyPool()