feat(failover): auto-switch to higher priority provider on recovery

- After circuit breaker reset, check if recovered provider has higher priority
- Automatically switch back if queue_order is lower (higher priority)
- Stream health check now resets circuit breaker on success/degraded
This commit is contained in:
YoVinchen
2025-12-22 00:35:36 +08:00
parent 59096d9c15
commit deea8455a9
2 changed files with 83 additions and 9 deletions
+9 -1
View File
@@ -6,10 +6,12 @@ import {
type StreamCheckResult,
} from "@/lib/api/model-test";
import type { AppId } from "@/lib/api";
import { useResetCircuitBreaker } from "@/lib/query/failover";
export function useStreamCheck(appId: AppId) {
const { t } = useTranslation();
const [checkingIds, setCheckingIds] = useState<Set<string>>(new Set());
const resetCircuitBreaker = useResetCircuitBreaker();
const checkProvider = useCallback(
async (
@@ -30,6 +32,9 @@ export function useStreamCheck(appId: AppId) {
}),
{ closeButton: true },
);
// 测试通过后重置熔断器状态
resetCircuitBreaker.mutate({ providerId, appType: appId });
} else if (result.status === "degraded") {
toast.warning(
t("streamCheck.degraded", {
@@ -38,6 +43,9 @@ export function useStreamCheck(appId: AppId) {
defaultValue: `${providerName} 响应较慢 (${result.responseTimeMs}ms)`,
}),
);
// 降级状态也重置熔断器,因为至少能通信
resetCircuitBreaker.mutate({ providerId, appType: appId });
} else {
toast.error(
t("streamCheck.failed", {
@@ -66,7 +74,7 @@ export function useStreamCheck(appId: AppId) {
});
}
},
[appId, t],
[appId, t, resetCircuitBreaker],
);
const isChecking = useCallback(