Files
CC-Switch/src/lib/query/failover.ts
T
YoVinchen 1586451862 Feat/auto failover switch (#440)
* feat(failover): add auto-failover master switch with proxy integration

- Add persistent auto_failover_enabled setting in database
- Add get/set_auto_failover_enabled commands
- Provider router respects master switch state
- Proxy shutdown automatically disables failover
- Enabling failover auto-starts proxy server
- Optimistic updates for failover queue toggle

* feat(proxy): persist proxy takeover state across app restarts

- Add proxy_takeover_{app_type} settings for per-app state tracking
- Restore proxy takeover state automatically on app startup
- Preserve state on normal exit, clear on manual stop
- Add stop_with_restore_keep_state method for graceful shutdown

* fix(proxy): set takeover state for all apps in start_with_takeover

* fix(windows): hide console window when checking CLI versions

Add CREATE_NO_WINDOW flag to prevent command prompt from flashing
when detecting claude/codex/gemini CLI versions on Windows.

* refactor(failover): make auto-failover toggle per-app independent

- Change setting key from 'auto_failover_enabled' to 'auto_failover_enabled_{app_type}'
- Update provider_router to check per-app failover setting
- When failover disabled, use current provider only; when enabled, use queue order
- Add unit tests for failover enabled/disabled behavior

* 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

* chore: remove unused start_proxy_with_takeover command

- Remove command registration from lib.rs
- Add comment clarifying failover queue is preserved on proxy stop

* feat(ui): integrate failover controls into provider cards

- Add failover toggle button to provider card actions
- Show priority badge (P1, P2, ...) for queued providers
- Highlight active provider with green border in failover mode
- Sync drag-drop order with failover queue
- Move per-app failover toggle to FailoverQueueManager
- Simplify SettingsPage failover section

* test(providers): add mocks for failover hooks in ProviderList tests

* refactor(failover): merge failover_queue table into providers

- Add in_failover_queue field to providers table
- Remove standalone failover_queue table and related indexes
- Simplify queue ordering by reusing sort_index field
- Remove reorder_failover_queue and set_failover_item_enabled commands
- Update frontend to use simplified FailoverQueueItem type

* fix(database): ensure in_failover_queue column exists for v2 databases

Add column check in create_tables to handle existing v2 databases
that were created before the failover queue refactor.

* fix(ui): differentiate active provider border color by proxy mode

- Use green border/gradient when proxy takeover is active
- Use blue border/gradient in normal mode (no proxy)
- Improves visual distinction between proxy and non-proxy states

* fix(database): clear provider health record when removing from failover queue

When a provider is removed from the failover queue, its health monitoring
is no longer needed. This change ensures the health record is also deleted
from the database to prevent stale data.

* fix(failover): improve cache cleanup for provider health and circuit breaker

- Use removeQueries instead of invalidateQueries when stopping proxy to
  completely clear health and circuit breaker caches
- Clear provider health and circuit breaker caches when removing from
  failover queue
- Refresh failover queue after drag-sort since queue order depends on
  sort_index
- Only show health badge when provider is in failover queue

* style: apply prettier formatting to App.tsx and ProviderList.tsx

* fix(proxy): handle missing health records and clear health on proxy stop

- Return default healthy state when provider health record not found
- Add clear_provider_health_for_app to clear health for specific app
- Clear app health records when stopping proxy takeover

* fix(proxy): track actual provider used in forwarding for accurate logging

Introduce ForwardResult and ForwardError structs to return the actual
provider that handled the request. This ensures usage statistics and
error logs reflect the correct provider after failover.
2025-12-23 12:37:36 +08:00

238 lines
6.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { failoverApi } from "@/lib/api/failover";
// ========== 熔断器 Hooks ==========
/**
* 获取供应商健康状态
*/
export function useProviderHealth(providerId: string, appType: string) {
return useQuery({
queryKey: ["providerHealth", providerId, appType],
queryFn: () => failoverApi.getProviderHealth(providerId, appType),
enabled: !!providerId && !!appType,
refetchInterval: 5000, // 每 5 秒刷新一次
retry: false,
});
}
/**
* 重置熔断器
*
* 重置后后端会检查是否应该切回优先级更高的供应商,
* 因此需要同时刷新供应商列表和代理状态。
*/
export function useResetCircuitBreaker() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
providerId,
appType,
}: {
providerId: string;
appType: string;
}) => failoverApi.resetCircuitBreaker(providerId, appType),
onSuccess: (_, variables) => {
// 刷新健康状态
queryClient.invalidateQueries({
queryKey: ["providerHealth", variables.providerId, variables.appType],
});
// 刷新供应商列表(因为可能发生了自动恢复切换)
queryClient.invalidateQueries({
queryKey: ["providers", variables.appType],
});
// 刷新代理状态(更新 active_targets
queryClient.invalidateQueries({
queryKey: ["proxyStatus"],
});
},
});
}
/**
* 获取熔断器配置
*/
export function useCircuitBreakerConfig() {
return useQuery({
queryKey: ["circuitBreakerConfig"],
queryFn: () => failoverApi.getCircuitBreakerConfig(),
});
}
/**
* 更新熔断器配置
*/
export function useUpdateCircuitBreakerConfig() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: failoverApi.updateCircuitBreakerConfig,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["circuitBreakerConfig"] });
},
});
}
/**
* 获取熔断器统计信息
*/
export function useCircuitBreakerStats(providerId: string, appType: string) {
return useQuery({
queryKey: ["circuitBreakerStats", providerId, appType],
queryFn: () => failoverApi.getCircuitBreakerStats(providerId, appType),
enabled: !!providerId && !!appType,
refetchInterval: 5000, // 每 5 秒刷新一次
});
}
// ========== 故障转移队列 Hooks(新) ==========
/**
* 获取故障转移队列
*/
export function useFailoverQueue(appType: string) {
return useQuery({
queryKey: ["failoverQueue", appType],
queryFn: () => failoverApi.getFailoverQueue(appType),
enabled: !!appType,
});
}
/**
* 获取可添加到队列的供应商
*/
export function useAvailableProvidersForFailover(appType: string) {
return useQuery({
queryKey: ["availableProvidersForFailover", appType],
queryFn: () => failoverApi.getAvailableProvidersForFailover(appType),
enabled: !!appType,
});
}
/**
* 添加供应商到故障转移队列
*/
export function useAddToFailoverQueue() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
appType,
providerId,
}: {
appType: string;
providerId: string;
}) => failoverApi.addToFailoverQueue(appType, providerId),
onSuccess: (_, variables) => {
queryClient.invalidateQueries({
queryKey: ["failoverQueue", variables.appType],
});
queryClient.invalidateQueries({
queryKey: ["availableProvidersForFailover", variables.appType],
});
queryClient.invalidateQueries({
queryKey: ["providers", variables.appType],
});
},
});
}
/**
* 从故障转移队列移除供应商
*/
export function useRemoveFromFailoverQueue() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
appType,
providerId,
}: {
appType: string;
providerId: string;
}) => failoverApi.removeFromFailoverQueue(appType, providerId),
onSuccess: (_, variables) => {
queryClient.invalidateQueries({
queryKey: ["failoverQueue", variables.appType],
});
queryClient.invalidateQueries({
queryKey: ["availableProvidersForFailover", variables.appType],
});
queryClient.invalidateQueries({
queryKey: ["providers", variables.appType],
});
// 清除该供应商的健康状态缓存(退出队列后不再需要健康监控)
queryClient.invalidateQueries({
queryKey: ["providerHealth", variables.providerId, variables.appType],
});
// 清除该供应商的熔断器统计缓存
queryClient.invalidateQueries({
queryKey: [
"circuitBreakerStats",
variables.providerId,
variables.appType,
],
});
},
});
}
// ========== 自动故障转移开关 Hooks ==========
/**
* 获取指定应用的自动故障转移开关状态
*/
export function useAutoFailoverEnabled(appType: string) {
return useQuery({
queryKey: ["autoFailoverEnabled", appType],
queryFn: () => failoverApi.getAutoFailoverEnabled(appType),
// 默认值为 false(与后端保持一致)
placeholderData: false,
});
}
/**
* 设置指定应用的自动故障转移开关状态
*/
export function useSetAutoFailoverEnabled() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ appType, enabled }: { appType: string; enabled: boolean }) =>
failoverApi.setAutoFailoverEnabled(appType, enabled),
// 乐观更新
onMutate: async ({ appType, enabled }) => {
await queryClient.cancelQueries({
queryKey: ["autoFailoverEnabled", appType],
});
const previousValue = queryClient.getQueryData<boolean>([
"autoFailoverEnabled",
appType,
]);
queryClient.setQueryData(["autoFailoverEnabled", appType], enabled);
return { previousValue, appType };
},
// 错误时回滚
onError: (_error, _variables, context) => {
if (context?.previousValue !== undefined) {
queryClient.setQueryData(
["autoFailoverEnabled", context.appType],
context.previousValue,
);
}
},
// 无论成功失败,都重新获取
onSettled: (_, __, variables) => {
queryClient.invalidateQueries({
queryKey: ["autoFailoverEnabled", variables.appType],
});
},
});
}