Spaces:
Sleeping
Sleeping
Add round-robin index for load balancing
Browse files
app.py
CHANGED
|
@@ -26,6 +26,9 @@ if not OLLAMA_KEYS:
|
|
| 26 |
OLLAMA_KEYS.append("ollam") # Dummy key jika ENV kosong
|
| 27 |
|
| 28 |
# Inisialisasi Status Key
|
|
|
|
|
|
|
|
|
|
| 29 |
key_status = {}
|
| 30 |
for idx, k in enumerate(OLLAMA_KEYS, 1):
|
| 31 |
key_status[k] = {
|
|
@@ -42,27 +45,33 @@ def log(msg):
|
|
| 42 |
|
| 43 |
def get_and_lock_key(exclude_keys=None):
|
| 44 |
"""
|
| 45 |
-
|
| 46 |
-
Ini
|
| 47 |
"""
|
|
|
|
|
|
|
| 48 |
if exclude_keys is None:
|
| 49 |
exclude_keys = set()
|
| 50 |
|
| 51 |
-
# Cek apakah semua key mati? Jika ya, reset semuanya
|
| 52 |
if not any(v["healthy"] for v in key_status.values()):
|
| 53 |
log("⚠️ Semua API Key berstatus mati/unhealthy. Melakukan RESET MASSAL...")
|
| 54 |
for v in key_status.values():
|
| 55 |
v["failures"] = 0
|
| 56 |
v["healthy"] = True
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
for
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# ==========================================
|
| 68 |
# ENDPOINTS
|
|
|
|
| 26 |
OLLAMA_KEYS.append("ollam") # Dummy key jika ENV kosong
|
| 27 |
|
| 28 |
# Inisialisasi Status Key
|
| 29 |
+
# Round-Robin Index for load balancing
|
| 30 |
+
last_used_index = 0
|
| 31 |
+
|
| 32 |
key_status = {}
|
| 33 |
for idx, k in enumerate(OLLAMA_KEYS, 1):
|
| 34 |
key_status[k] = {
|
|
|
|
| 45 |
|
| 46 |
def get_and_lock_key(exclude_keys=None):
|
| 47 |
"""
|
| 48 |
+
Round-Robin + Atomic Lock: Pilih key berurutan dari last_used_index.
|
| 49 |
+
Ini memastikan burst request terdistribusi merata ke semua key.
|
| 50 |
"""
|
| 51 |
+
global last_used_index
|
| 52 |
+
|
| 53 |
if exclude_keys is None:
|
| 54 |
exclude_keys = set()
|
| 55 |
|
| 56 |
+
# Cek apakah semua key mati? Jika ya, reset semuanya
|
| 57 |
if not any(v["healthy"] for v in key_status.values()):
|
| 58 |
log("⚠️ Semua API Key berstatus mati/unhealthy. Melakukan RESET MASSAL...")
|
| 59 |
for v in key_status.values():
|
| 60 |
v["failures"] = 0
|
| 61 |
v["healthy"] = True
|
| 62 |
+
last_used_index = 0
|
| 63 |
|
| 64 |
+
# Round-robin: cari key berurutan dari last_used_index
|
| 65 |
+
for i in range(len(OLLAMA_KEYS)):
|
| 66 |
+
idx = (last_used_index + i) % len(OLLAMA_KEYS)
|
| 67 |
+
key = OLLAMA_KEYS[idx]
|
| 68 |
+
|
| 69 |
+
if key_status[key]["healthy"] and not key_status[key]["in_use"] and key not in exclude_keys:
|
| 70 |
+
last_used_index = idx
|
| 71 |
+
key_status[key]["in_use"] = True
|
| 72 |
+
return key
|
| 73 |
+
|
| 74 |
+
return None
|
| 75 |
|
| 76 |
# ==========================================
|
| 77 |
# ENDPOINTS
|