64 lines
2.0 KiB
JavaScript
64 lines
2.0 KiB
JavaScript
// 简易熔断器:按 label 分桶,连续失败 N 次后,在冷却期内直接抛错
|
|
// 不同 label(如 deepseek / github / http)各自独立熔断,互不影响
|
|
const circuits = new Map(); // label -> { failures, openUntil }
|
|
const CIRCUIT_THRESHOLD = 5; // 连续失败 5 次触发熔断
|
|
const CIRCUIT_COOLDOWN_MS = 60000; // 熔断冷却 60 秒
|
|
|
|
function getCircuit(label) {
|
|
let c = circuits.get(label);
|
|
if (!c) {
|
|
c = { failures: 0, openUntil: 0 };
|
|
circuits.set(label, c);
|
|
}
|
|
return c;
|
|
}
|
|
|
|
function checkCircuit(label) {
|
|
const c = getCircuit(label);
|
|
if (Date.now() < c.openUntil) {
|
|
throw new Error(`[circuit-breaker] ${label} 熔断中,${Math.ceil((c.openUntil - Date.now()) / 1000)}s 后恢复`);
|
|
}
|
|
}
|
|
|
|
function recordSuccess(label) {
|
|
const c = getCircuit(label);
|
|
c.failures = 0;
|
|
c.openUntil = 0;
|
|
}
|
|
|
|
function recordFailure(label) {
|
|
const c = getCircuit(label);
|
|
c.failures++;
|
|
if (c.failures >= CIRCUIT_THRESHOLD) {
|
|
c.openUntil = Date.now() + CIRCUIT_COOLDOWN_MS;
|
|
console.warn(` 🔌 [circuit-breaker] ${label} 连续失败 ${c.failures} 次,熔断 ${CIRCUIT_COOLDOWN_MS / 1000}s`);
|
|
}
|
|
}
|
|
|
|
async function withRetry(fn, { maxRetries = 3, baseDelay = 1000, label = "operation" } = {}) {
|
|
checkCircuit(label);
|
|
let lastError;
|
|
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
try {
|
|
const result = await fn();
|
|
recordSuccess(label);
|
|
return result;
|
|
} catch (error) {
|
|
lastError = error;
|
|
recordFailure(label);
|
|
if (attempt < maxRetries) {
|
|
// 指数退避 + 随机抖动(避免雷群效应)
|
|
const jitter = Math.random() * baseDelay * 0.5;
|
|
const delay = baseDelay * Math.pow(2, attempt - 1) + jitter;
|
|
console.warn(` ⚠ ${label} 第${attempt}次失败,${(delay / 1000).toFixed(1)}s后重试...`);
|
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
// 每次重试前检查熔断
|
|
checkCircuit(label);
|
|
}
|
|
}
|
|
}
|
|
throw lastError;
|
|
}
|
|
|
|
export { withRetry };
|