fix: feed provider health into circuit breaker

This commit is contained in:
2026-06-09 12:02:29 +08:00
parent f9da506017
commit ca84754bd2
3 changed files with 27 additions and 7 deletions
+18 -5
View File
@@ -7,6 +7,7 @@
*/
const { pool } = require("./db");
const { recordProviderSuccess, recordProviderFailure, getAllBreakerStats } = require("./providerCircuitBreaker");
const CHECK_INTERVAL_MS = 5 * 60 * 1000;
const DASHSCOPE_TEST_MODEL = "qwen-max";
@@ -21,6 +22,15 @@ const providerHealthCache = {
grsai: { status: "unknown", lastCheck: null, lastError: null, details: null },
};
function recordProbeOutcome(provider, result, latencyMs) {
if (!provider) return;
if (result?.ok) {
recordProviderSuccess(provider, latencyMs);
} else {
recordProviderFailure(provider);
}
}
async function getDashScopeKey() {
const { rows } = await pool.query(
"SELECT id, api_key FROM api_keys WHERE provider LIKE '%dashscope%' AND enabled = 1 ORDER BY id LIMIT 1"
@@ -120,8 +130,10 @@ async function runHealthCheck() {
// ── DashScope ──
const dashKey = await getDashScopeKey();
if (dashKey) {
const startedAt = Date.now();
try {
const result = await probeDashScope(dashKey);
recordProbeOutcome("dashscope", result, Date.now() - startedAt);
const prev = providerHealthCache.dashscope.status;
providerHealthCache.dashscope = {
status: result.status,
@@ -144,6 +156,7 @@ async function runHealthCheck() {
}
}
} catch (err) {
recordProviderFailure("dashscope");
providerHealthCache.dashscope = {
status: "timeout",
lastCheck: new Date().toISOString(),
@@ -164,8 +177,10 @@ async function runHealthCheck() {
// ── GrsAI ──
const grsaiKey = await getGrsaiKey();
if (grsaiKey) {
const startedAt = Date.now();
try {
const result = await probeGrsai(grsaiKey);
recordProbeOutcome("grsai", result, Date.now() - startedAt);
const prev = providerHealthCache.grsai.status;
providerHealthCache.grsai = {
status: result.status,
@@ -186,6 +201,7 @@ async function runHealthCheck() {
}
}
} catch (err) {
recordProviderFailure("grsai");
providerHealthCache.grsai = {
status: "timeout",
lastCheck: new Date().toISOString(),
@@ -204,10 +220,7 @@ async function runHealthCheck() {
}
// ── Circuit breaker summary ──
try {
const cb = require("./providerCircuitBreaker");
providerHealthCache.circuitBreaker = cb.getProviderStatusMap ? cb.getProviderStatusMap() : null;
} catch {}
providerHealthCache.circuitBreaker = getAllBreakerStats();
// ── Admin low-balance alert ──
try {
@@ -256,4 +269,4 @@ module.exports = {
stopProviderHealthMonitor,
getProviderHealthCache,
runHealthCheck,
};
};