Spaces:
Paused
Paused
anurag008w commited on
Commit ·
4faa678
1
Parent(s): fc8867a
feat: optional suspended key reuse as last-resort; treat 429/402 as strike-bearing
Browse files- .env.example +6 -0
- multi-provider-key-rotator.cjs +15 -3
.env.example
CHANGED
|
@@ -176,6 +176,12 @@ LLM_API_KEY_FALLBACK_ENABLED=true
|
|
| 176 |
# upstream Retry-After header when provided. Capped to 10s internally.
|
| 177 |
# KEY_FETCH_RETRY_BASE_DELAY_MS=250
|
| 178 |
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
# Optional diagnostics log snapshot for key pools (default: off).
|
| 180 |
# KEY_ROTATOR_DIAGNOSTICS=true
|
| 181 |
# KEY_ROTATOR_DIAGNOSTICS_INTERVAL_MS=60000
|
|
|
|
| 176 |
# upstream Retry-After header when provided. Capped to 10s internally.
|
| 177 |
# KEY_FETCH_RETRY_BASE_DELAY_MS=250
|
| 178 |
#
|
| 179 |
+
# When all keys are suspended, default behavior is best-effort: reuse the
|
| 180 |
+
# soonest-recovering suspended key so requests keep flowing.
|
| 181 |
+
# Set false if you prefer strict withholding until cooldown expiry.
|
| 182 |
+
# KEY_USE_SUSPENDED_AS_LAST_RESORT=true
|
| 183 |
+
#
|
| 184 |
+
#
|
| 185 |
# Optional diagnostics log snapshot for key pools (default: off).
|
| 186 |
# KEY_ROTATOR_DIAGNOSTICS=true
|
| 187 |
# KEY_ROTATOR_DIAGNOSTICS_INTERVAL_MS=60000
|
multi-provider-key-rotator.cjs
CHANGED
|
@@ -68,6 +68,10 @@ const DIAGNOSTICS_INTERVAL_MS = Math.max(
|
|
| 68 |
10_000,
|
| 69 |
parseInt(process.env.KEY_ROTATOR_DIAGNOSTICS_INTERVAL_MS || '', 10) || 60_000,
|
| 70 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
// Long suspend window for exhausted/invalid keys.
|
| 72 |
// Capped to 16h to avoid oversuppressing pools for too long.
|
| 73 |
const formatHours = (ms) => (ms / (60 * 60 * 1000)).toFixed(ms % (60 * 60 * 1000) === 0 ? 0 : 2);
|
|
@@ -326,7 +330,13 @@ function nextKey(p) {
|
|
| 326 |
return bestPick.key;
|
| 327 |
}
|
| 328 |
|
| 329 |
-
// All keys are sitting out —
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
warn(`[key-rotator] ${p.name}: all ${total} key(s) suspended — using soonest-recovering key`);
|
| 331 |
let best = p.keys[0], bestExpiry = Infinity;
|
| 332 |
for (const k of p.keys) {
|
|
@@ -383,7 +393,9 @@ function handleStatus(p, key, status) {
|
|
| 383 |
}
|
| 384 |
|
| 385 |
if (status === 429 || status === 402) {
|
| 386 |
-
|
|
|
|
|
|
|
| 387 |
warn(`[key-rotator] ${p.name}: quota/rate status=${status} on ...${key.slice(-6)}`);
|
| 388 |
return;
|
| 389 |
}
|
|
@@ -662,4 +674,4 @@ patchHttpModule(http);
|
|
| 662 |
patchHttpModule(https);
|
| 663 |
startDiagnostics();
|
| 664 |
|
| 665 |
-
log(`[key-rotator] loaded — cooldown base:${BASE_COOLDOWN_MS/1000}s max-strikes:${MAX_STRIKES} perm-suspend:${formatHours(PERM_SUSPEND_MS)}h (cap 16h) max-inflight-per-key:${MAX_INFLIGHT_PER_KEY} diagnostics:${DIAGNOSTICS_ENABLED ? 'on' : 'off'} log-level:${LOG_LEVEL} verbose-picks:${VERBOSE_PICKS ? 'on' : 'off'}`);
|
|
|
|
| 68 |
10_000,
|
| 69 |
parseInt(process.env.KEY_ROTATOR_DIAGNOSTICS_INTERVAL_MS || '', 10) || 60_000,
|
| 70 |
);
|
| 71 |
+
|
| 72 |
+
const USE_SUSPENDED_KEY_AS_LAST_RESORT = !/^(0|false|no|off)$/i.test(
|
| 73 |
+
String(process.env.KEY_USE_SUSPENDED_AS_LAST_RESORT || 'true').trim(),
|
| 74 |
+
);
|
| 75 |
// Long suspend window for exhausted/invalid keys.
|
| 76 |
// Capped to 16h to avoid oversuppressing pools for too long.
|
| 77 |
const formatHours = (ms) => (ms / (60 * 60 * 1000)).toFixed(ms % (60 * 60 * 1000) === 0 ? 0 : 2);
|
|
|
|
| 330 |
return bestPick.key;
|
| 331 |
}
|
| 332 |
|
| 333 |
+
// All keys are sitting out — default to best-effort progress by reusing
|
| 334 |
+
// the soonest-recovering key, unless explicitly disabled.
|
| 335 |
+
if (!USE_SUSPENDED_KEY_AS_LAST_RESORT) {
|
| 336 |
+
warn(`[key-rotator] ${p.name}: all ${total} key(s) suspended — withholding key until cooldown expires (last-resort disabled)`);
|
| 337 |
+
return null;
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
warn(`[key-rotator] ${p.name}: all ${total} key(s) suspended — using soonest-recovering key`);
|
| 341 |
let best = p.keys[0], bestExpiry = Infinity;
|
| 342 |
for (const k of p.keys) {
|
|
|
|
| 393 |
}
|
| 394 |
|
| 395 |
if (status === 429 || status === 402) {
|
| 396 |
+
// Treat quota/rate-limit as strike-bearing failures so repeatedly
|
| 397 |
+
// exhausted keys quickly move into long suspend (up to 16h).
|
| 398 |
+
recordFailure(p, key);
|
| 399 |
warn(`[key-rotator] ${p.name}: quota/rate status=${status} on ...${key.slice(-6)}`);
|
| 400 |
return;
|
| 401 |
}
|
|
|
|
| 674 |
patchHttpModule(https);
|
| 675 |
startDiagnostics();
|
| 676 |
|
| 677 |
+
log(`[key-rotator] loaded — cooldown base:${BASE_COOLDOWN_MS/1000}s max-strikes:${MAX_STRIKES} perm-suspend:${formatHours(PERM_SUSPEND_MS)}h (cap 16h) max-inflight-per-key:${MAX_INFLIGHT_PER_KEY} diagnostics:${DIAGNOSTICS_ENABLED ? 'on' : 'off'} log-level:${LOG_LEVEL} verbose-picks:${VERBOSE_PICKS ? 'on' : 'off'} suspended-last-resort:${USE_SUSPENDED_KEY_AS_LAST_RESORT ? 'on' : 'off'}`);
|