From 60f050d184466c8c143bcb3c7d0064c90a9a3322 Mon Sep 17 00:00:00 2001 From: erio Date: Fri, 6 Mar 2026 15:48:58 +0800 Subject: [PATCH] feat: add account affinity client count indicator Add reverse Redis index (account_affinity:{accountID}) to track which clients are bound to each account via affinity scheduling. Display a blue badge with client count in the admin account list, with a hover tooltip showing client details (groupID:clientID + TTL). Backend: reverse index in Lua scripts, batch query methods, new GET /accounts/:id/affinity-clients endpoint. Frontend: blue badge + lazy-loaded tooltip in AccountStatusIndicator. --- .../admin/account_data_handler_test.go | 1 + .../internal/handler/admin/account_handler.go | 54 ++++++- .../account_handler_mixed_channel_test.go | 2 +- .../admin/account_handler_passthrough_test.go | 1 + .../admin/batch_update_credentials_test.go | 2 +- backend/internal/repository/gateway_cache.go | 141 +++++++++++++++++- backend/internal/server/api_contract_test.go | 2 +- backend/internal/server/routes/admin.go | 1 + .../service/antigravity_smart_retry_test.go | 9 ++ .../gateway_hotpath_optimization_test.go | 9 ++ .../service/gateway_multiplatform_test.go | 9 ++ backend/internal/service/gateway_service.go | 19 +++ .../service/gemini_multiplatform_test.go | 9 ++ .../service/openai_gateway_service_test.go | 9 ++ .../service/openai_ws_state_store_test.go | 9 ++ backend/internal/testutil/stubs.go | 9 ++ .../account/AccountStatusIndicator.vue | 66 +++++++- frontend/src/types/index.ts | 1 + 18 files changed, 339 insertions(+), 14 deletions(-) diff --git a/backend/internal/handler/admin/account_data_handler_test.go b/backend/internal/handler/admin/account_data_handler_test.go index 285033a17d..e516b275f2 100644 --- a/backend/internal/handler/admin/account_data_handler_test.go +++ b/backend/internal/handler/admin/account_data_handler_test.go @@ -65,6 +65,7 @@ func setupAccountDataRouter() (*gin.Engine, *stubAdminService) { nil, nil, nil, + nil, ) router.GET("/api/v1/admin/accounts/data", h.ExportData) diff --git a/backend/internal/handler/admin/account_handler.go b/backend/internal/handler/admin/account_handler.go index 843d51c516..2fa0e20c4c 100644 --- a/backend/internal/handler/admin/account_handler.go +++ b/backend/internal/handler/admin/account_handler.go @@ -55,6 +55,7 @@ type AccountHandler struct { sessionLimitCache service.SessionLimitCache rpmCache service.RPMCache tokenCacheInvalidator service.TokenCacheInvalidator + gatewayCache service.GatewayCache } // NewAccountHandler creates a new admin account handler @@ -72,6 +73,7 @@ func NewAccountHandler( sessionLimitCache service.SessionLimitCache, rpmCache service.RPMCache, tokenCacheInvalidator service.TokenCacheInvalidator, + gatewayCache service.GatewayCache, ) *AccountHandler { return &AccountHandler{ adminService: adminService, @@ -87,6 +89,7 @@ func NewAccountHandler( sessionLimitCache: sessionLimitCache, rpmCache: rpmCache, tokenCacheInvalidator: tokenCacheInvalidator, + gatewayCache: gatewayCache, } } @@ -158,9 +161,10 @@ type AccountWithConcurrency struct { *dto.Account CurrentConcurrency int `json:"current_concurrency"` // 以下字段仅对 Anthropic OAuth/SetupToken 账号有效,且仅在启用相应功能时返回 - CurrentWindowCost *float64 `json:"current_window_cost,omitempty"` // 当前窗口费用 - ActiveSessions *int `json:"active_sessions,omitempty"` // 当前活跃会话数 - CurrentRPM *int `json:"current_rpm,omitempty"` // 当前分钟 RPM 计数 + CurrentWindowCost *float64 `json:"current_window_cost,omitempty"` // 当前窗口费用 + ActiveSessions *int `json:"active_sessions,omitempty"` // 当前活跃会话数 + CurrentRPM *int `json:"current_rpm,omitempty"` // 当前分钟 RPM 计数 + AffinityClientCount *int `json:"affinity_client_count,omitempty"` // 亲和客户端数量 } func (h *AccountHandler) buildAccountResponseWithRuntime(ctx context.Context, account *service.Account) AccountWithConcurrency { @@ -243,6 +247,7 @@ func (h *AccountHandler) List(c *gin.Context) { var windowCosts map[int64]float64 var activeSessions map[int64]int var rpmCounts map[int64]int + var affinityCounts map[int64]int // 始终获取并发数(Redis ZCARD,极低开销) if h.concurrencyService != nil { @@ -251,10 +256,11 @@ func (h *AccountHandler) List(c *gin.Context) { } } - // 识别需要查询窗口费用、会话数和 RPM 的账号(Anthropic OAuth/SetupToken 且启用了相应功能) + // 识别需要查询窗口费用、会话数、RPM 和亲和客户端的账号(Anthropic OAuth/SetupToken 且启用了相应功能) windowCostAccountIDs := make([]int64, 0) sessionLimitAccountIDs := make([]int64, 0) rpmAccountIDs := make([]int64, 0) + affinityAccountIDs := make([]int64, 0) sessionIdleTimeouts := make(map[int64]time.Duration) // 各账号的会话空闲超时配置 for i := range accounts { acc := &accounts[i] @@ -269,6 +275,9 @@ func (h *AccountHandler) List(c *gin.Context) { if acc.GetBaseRPM() > 0 { rpmAccountIDs = append(rpmAccountIDs, acc.ID) } + if acc.IsClientAffinityEnabled() { + affinityAccountIDs = append(affinityAccountIDs, acc.ID) + } } } @@ -288,6 +297,11 @@ func (h *AccountHandler) List(c *gin.Context) { } } + // 获取亲和客户端数量(Redis pipeline,低开销) + if len(affinityAccountIDs) > 0 && h.gatewayCache != nil { + affinityCounts, _ = h.gatewayCache.GetAccountAffinityCountBatch(c.Request.Context(), affinityAccountIDs, service.ClientAffinityTTL()) + } + // 窗口费用获取:lite 模式从快照缓存读取,非 lite 模式执行 PostgreSQL 查询后写入缓存 if len(windowCostAccountIDs) > 0 { if lite { @@ -362,6 +376,13 @@ func (h *AccountHandler) List(c *gin.Context) { } } + // 添加亲和客户端数量(仅当启用时) + if affinityCounts != nil { + if count, ok := affinityCounts[acc.ID]; ok { + item.AffinityClientCount = &count + } + } + result[i] = item } @@ -1852,3 +1873,28 @@ func sanitizeExtraBaseRPM(extra map[string]any) { } extra["base_rpm"] = v } + +// GetAffinityClients 返回指定账号的亲和客户端列表 +// GET /api/v1/admin/accounts/:id/affinity-clients +func (h *AccountHandler) GetAffinityClients(c *gin.Context) { + id, err := strconv.ParseInt(c.Param("id"), 10, 64) + if err != nil { + response.Error(c, http.StatusBadRequest, "invalid id") + return + } + + if h.gatewayCache == nil { + response.Success(c, []service.AffinityClientInfo{}) + return + } + + clients, err := h.gatewayCache.GetAccountAffinityClients(c.Request.Context(), id, service.ClientAffinityTTL()) + if err != nil { + response.ErrorFrom(c, err) + return + } + if clients == nil { + clients = []service.AffinityClientInfo{} + } + response.Success(c, clients) +} diff --git a/backend/internal/handler/admin/account_handler_mixed_channel_test.go b/backend/internal/handler/admin/account_handler_mixed_channel_test.go index 5b81db2aad..5e45619064 100644 --- a/backend/internal/handler/admin/account_handler_mixed_channel_test.go +++ b/backend/internal/handler/admin/account_handler_mixed_channel_test.go @@ -15,7 +15,7 @@ import ( func setupAccountMixedChannelRouter(adminSvc *stubAdminService) *gin.Engine { gin.SetMode(gin.TestMode) router := gin.New() - accountHandler := NewAccountHandler(adminSvc, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) + accountHandler := NewAccountHandler(adminSvc, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) router.POST("/api/v1/admin/accounts/check-mixed-channel", accountHandler.CheckMixedChannel) router.POST("/api/v1/admin/accounts", accountHandler.Create) router.PUT("/api/v1/admin/accounts/:id", accountHandler.Update) diff --git a/backend/internal/handler/admin/account_handler_passthrough_test.go b/backend/internal/handler/admin/account_handler_passthrough_test.go index d86501c047..a11f2c5195 100644 --- a/backend/internal/handler/admin/account_handler_passthrough_test.go +++ b/backend/internal/handler/admin/account_handler_passthrough_test.go @@ -29,6 +29,7 @@ func TestAccountHandler_Create_AnthropicAPIKeyPassthroughExtraForwarded(t *testi nil, nil, nil, + nil, ) router := gin.New() diff --git a/backend/internal/handler/admin/batch_update_credentials_test.go b/backend/internal/handler/admin/batch_update_credentials_test.go index 0b1b669174..f2cd1e3a1c 100644 --- a/backend/internal/handler/admin/batch_update_credentials_test.go +++ b/backend/internal/handler/admin/batch_update_credentials_test.go @@ -36,7 +36,7 @@ func (f *failingAdminService) UpdateAccount(ctx context.Context, id int64, input func setupAccountHandlerWithService(adminSvc service.AdminService) (*gin.Engine, *AccountHandler) { gin.SetMode(gin.TestMode) router := gin.New() - handler := NewAccountHandler(adminSvc, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) + handler := NewAccountHandler(adminSvc, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) router.POST("/api/v1/admin/accounts/batch-update-credentials", handler.BatchUpdateCredentials) return router, handler } diff --git a/backend/internal/repository/gateway_cache.go b/backend/internal/repository/gateway_cache.go index 8fa6d0c401..6fed5b6874 100644 --- a/backend/internal/repository/gateway_cache.go +++ b/backend/internal/repository/gateway_cache.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "strconv" + "strings" "time" "github.com/Wei-Shaw/sub2api/internal/service" @@ -11,8 +12,9 @@ import ( ) const ( - stickySessionPrefix = "sticky_session:" - clientAffinityPrefix = "client_affinity:" + stickySessionPrefix = "sticky_session:" + clientAffinityPrefix = "client_affinity:" + accountAffinityPrefix = "account_affinity:" ) type gatewayCache struct { @@ -70,16 +72,21 @@ redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1]) return redis.call('ZREVRANGE', KEYS[1], 0, -1) `) -// updateAffinityScript: 清理过期成员、添加/更新亲和关系、刷新 TTL +// updateAffinityScript: 清理过期成员、添加/更新亲和关系、刷新 TTL, +// 同时维护反向索引 account_affinity:{accountID} // KEYS[1] = client_affinity:{groupID}:{clientID} +// KEYS[2] = account_affinity:{accountID} // ARGV[1] = 当前时间戳 // ARGV[2] = TTL 秒数 // ARGV[3] = accountID // ARGV[4] = 过期阈值时间戳 (now - ttl) +// ARGV[5] = 反向索引成员 "{groupID}:{clientID}" var updateAffinityScript = redis.NewScript(` redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[4]) redis.call('ZADD', KEYS[1], ARGV[1], ARGV[3]) redis.call('EXPIRE', KEYS[1], ARGV[2]) +redis.call('ZADD', KEYS[2], ARGV[1], ARGV[5]) +redis.call('EXPIRE', KEYS[2], ARGV[2]) return 1 `) @@ -109,16 +116,138 @@ func (c *gatewayCache) GetClientAffinityAccounts(ctx context.Context, groupID in func (c *gatewayCache) UpdateClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64, ttl time.Duration) error { key := buildAffinityKey(groupID, clientID) + reverseKey := buildAccountAffinityKey(accountID) + reverseMember := fmt.Sprintf("%d:%s", groupID, clientID) now := time.Now().Unix() ttlSeconds := int64(ttl.Seconds()) expireThreshold := now - ttlSeconds - return updateAffinityScript.Run(ctx, c.rdb, []string{key}, - now, ttlSeconds, accountID, expireThreshold, + return updateAffinityScript.Run(ctx, c.rdb, []string{key, reverseKey}, + now, ttlSeconds, accountID, expireThreshold, reverseMember, ).Err() } func (c *gatewayCache) RemoveClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64) error { key := buildAffinityKey(groupID, clientID) - return c.rdb.ZRem(ctx, key, accountID).Err() + reverseKey := buildAccountAffinityKey(accountID) + reverseMember := fmt.Sprintf("%d:%s", groupID, clientID) + pipe := c.rdb.Pipeline() + pipe.ZRem(ctx, key, accountID) + pipe.ZRem(ctx, reverseKey, reverseMember) + _, err := pipe.Exec(ctx) + return err +} + +// buildAccountAffinityKey 构建账号亲和反向索引 key +// 格式: account_affinity:{accountID} +func buildAccountAffinityKey(accountID int64) string { + return fmt.Sprintf("%s%d", accountAffinityPrefix, accountID) +} + +// getAccountAffinityScript: 清理过期成员后返回 ZCARD +// KEYS[1] = account_affinity:{accountID} +// ARGV[1] = 过期阈值时间戳 (now - ttl) +var getAccountAffinityScript = redis.NewScript(` +redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1]) +return redis.call('ZCARD', KEYS[1]) +`) + +func (c *gatewayCache) GetAccountAffinityCount(ctx context.Context, accountID int64, ttl time.Duration) (int, error) { + key := buildAccountAffinityKey(accountID) + expireThreshold := time.Now().Unix() - int64(ttl.Seconds()) + + count, err := getAccountAffinityScript.Run(ctx, c.rdb, []string{key}, expireThreshold).Int() + if err != nil && err != redis.Nil { + return 0, err + } + return count, nil +} + +func (c *gatewayCache) GetAccountAffinityCountBatch(ctx context.Context, accountIDs []int64, ttl time.Duration) (map[int64]int, error) { + if len(accountIDs) == 0 { + return nil, nil + } + + expireThreshold := time.Now().Unix() - int64(ttl.Seconds()) + pipe := c.rdb.Pipeline() + cmds := make([]*redis.Cmd, len(accountIDs)) + + for i, id := range accountIDs { + key := buildAccountAffinityKey(id) + cmds[i] = getAccountAffinityScript.Run(ctx, pipe, []string{key}, expireThreshold) + } + + _, err := pipe.Exec(ctx) + if err != nil && err != redis.Nil { + return nil, err + } + + result := make(map[int64]int, len(accountIDs)) + for i, id := range accountIDs { + count, err := cmds[i].Int() + if err != nil && err != redis.Nil { + continue + } + if count > 0 { + result[id] = count + } + } + return result, nil +} + +// getAccountAffinityClientsScript: 清理过期成员后返回成员列表及 score +// KEYS[1] = account_affinity:{accountID} +// ARGV[1] = 过期阈值时间戳 (now - ttl) +var getAccountAffinityClientsScript = redis.NewScript(` +redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1]) +return redis.call('ZRANGE', KEYS[1], 0, -1, 'WITHSCORES') +`) + +func (c *gatewayCache) GetAccountAffinityClients(ctx context.Context, accountID int64, ttl time.Duration) ([]service.AffinityClientInfo, error) { + key := buildAccountAffinityKey(accountID) + now := time.Now().Unix() + expireThreshold := now - int64(ttl.Seconds()) + + raw, err := getAccountAffinityClientsScript.Run(ctx, c.rdb, []string{key}, expireThreshold).StringSlice() + if err != nil { + if err == redis.Nil { + return nil, nil + } + return nil, err + } + + // ZRANGE WITHSCORES returns [member1, score1, member2, score2, ...] + clients := make([]service.AffinityClientInfo, 0, len(raw)/2) + for i := 0; i+1 < len(raw); i += 2 { + member := raw[i] + scoreStr := raw[i+1] + + score, parseErr := strconv.ParseInt(scoreStr, 10, 64) + if parseErr != nil { + continue + } + + // member format: "{groupID}:{clientID}" + sepIdx := strings.IndexByte(member, ':') + if sepIdx < 0 { + continue + } + groupID, parseErr := strconv.ParseInt(member[:sepIdx], 10, 64) + if parseErr != nil { + continue + } + clientID := member[sepIdx+1:] + + remaining := score + int64(ttl.Seconds()) - now + if remaining < 0 { + remaining = 0 + } + + clients = append(clients, service.AffinityClientInfo{ + GroupID: groupID, + ClientID: clientID, + TTL: remaining, + }) + } + return clients, nil } diff --git a/backend/internal/server/api_contract_test.go b/backend/internal/server/api_contract_test.go index aafbbe21cb..537e64b4ea 100644 --- a/backend/internal/server/api_contract_test.go +++ b/backend/internal/server/api_contract_test.go @@ -648,7 +648,7 @@ func newContractDeps(t *testing.T) *contractDeps { apiKeyHandler := handler.NewAPIKeyHandler(apiKeyService) usageHandler := handler.NewUsageHandler(usageService, apiKeyService) adminSettingHandler := adminhandler.NewSettingHandler(settingService, nil, nil, nil, nil) - adminAccountHandler := adminhandler.NewAccountHandler(adminService, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) + adminAccountHandler := adminhandler.NewAccountHandler(adminService, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) jwtAuth := func(c *gin.Context) { c.Set(string(middleware.ContextKeyUser), middleware.AuthSubject{ diff --git a/backend/internal/server/routes/admin.go b/backend/internal/server/routes/admin.go index 2e53feb36f..46366a03b5 100644 --- a/backend/internal/server/routes/admin.go +++ b/backend/internal/server/routes/admin.go @@ -257,6 +257,7 @@ func registerAccountRoutes(admin *gin.RouterGroup, h *handler.Handlers) { accounts.DELETE("/:id/temp-unschedulable", h.Admin.Account.ClearTempUnschedulable) accounts.POST("/:id/schedulable", h.Admin.Account.SetSchedulable) accounts.GET("/:id/models", h.Admin.Account.GetAvailableModels) + accounts.GET("/:id/affinity-clients", h.Admin.Account.GetAffinityClients) accounts.POST("/batch", h.Admin.Account.BatchCreate) accounts.GET("/data", h.Admin.Account.ExportData) accounts.POST("/data", h.Admin.Account.ImportData) diff --git a/backend/internal/service/antigravity_smart_retry_test.go b/backend/internal/service/antigravity_smart_retry_test.go index 7f836bf47c..a516805758 100644 --- a/backend/internal/service/antigravity_smart_retry_test.go +++ b/backend/internal/service/antigravity_smart_retry_test.go @@ -39,6 +39,15 @@ func (c *stubSmartRetryCache) UpdateClientAffinity(_ context.Context, _ int64, _ func (c *stubSmartRetryCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { return nil } +func (c *stubSmartRetryCache) GetAccountAffinityCount(_ context.Context, _ int64, _ time.Duration) (int, error) { + return 0, nil +} +func (c *stubSmartRetryCache) GetAccountAffinityCountBatch(_ context.Context, _ []int64, _ time.Duration) (map[int64]int, error) { + return nil, nil +} +func (c *stubSmartRetryCache) GetAccountAffinityClients(_ context.Context, _ int64, _ time.Duration) ([]AffinityClientInfo, error) { + return nil, nil +} // mockSmartRetryUpstream 用于 handleSmartRetry 测试的 mock upstream type mockSmartRetryUpstream struct { diff --git a/backend/internal/service/gateway_hotpath_optimization_test.go b/backend/internal/service/gateway_hotpath_optimization_test.go index 137490e8db..5eb97de993 100644 --- a/backend/internal/service/gateway_hotpath_optimization_test.go +++ b/backend/internal/service/gateway_hotpath_optimization_test.go @@ -152,6 +152,15 @@ func (s *stickyGatewayCacheHotpathStub) UpdateClientAffinity(_ context.Context, func (s *stickyGatewayCacheHotpathStub) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { return nil } +func (s *stickyGatewayCacheHotpathStub) GetAccountAffinityCount(_ context.Context, _ int64, _ time.Duration) (int, error) { + return 0, nil +} +func (s *stickyGatewayCacheHotpathStub) GetAccountAffinityCountBatch(_ context.Context, _ []int64, _ time.Duration) (map[int64]int, error) { + return nil, nil +} +func (s *stickyGatewayCacheHotpathStub) GetAccountAffinityClients(_ context.Context, _ int64, _ time.Duration) ([]AffinityClientInfo, error) { + return nil, nil +} func (s *modelsListAccountRepoStub) ListSchedulableByGroupID(ctx context.Context, groupID int64) ([]Account, error) { s.listByGroupCalls.Add(1) diff --git a/backend/internal/service/gateway_multiplatform_test.go b/backend/internal/service/gateway_multiplatform_test.go index 466ff294bb..c513d6b179 100644 --- a/backend/internal/service/gateway_multiplatform_test.go +++ b/backend/internal/service/gateway_multiplatform_test.go @@ -244,6 +244,15 @@ func (m *mockGatewayCacheForPlatform) UpdateClientAffinity(_ context.Context, _ func (m *mockGatewayCacheForPlatform) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { return nil } +func (m *mockGatewayCacheForPlatform) GetAccountAffinityCount(_ context.Context, _ int64, _ time.Duration) (int, error) { + return 0, nil +} +func (m *mockGatewayCacheForPlatform) GetAccountAffinityCountBatch(_ context.Context, _ []int64, _ time.Duration) (map[int64]int, error) { + return nil, nil +} +func (m *mockGatewayCacheForPlatform) GetAccountAffinityClients(_ context.Context, _ int64, _ time.Duration) ([]AffinityClientInfo, error) { + return nil, nil +} type mockGroupRepoForGateway struct { groups map[int64]*Group diff --git a/backend/internal/service/gateway_service.go b/backend/internal/service/gateway_service.go index 0fe2545ec0..964e1a0348 100644 --- a/backend/internal/service/gateway_service.go +++ b/backend/internal/service/gateway_service.go @@ -375,6 +375,25 @@ type GatewayCache interface { UpdateClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64, ttl time.Duration) error // RemoveClientAffinity 删除单个客户端亲和关系 RemoveClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64) error + + // GetAccountAffinityCount 获取账号的亲和客户端数量(清理过期后) + GetAccountAffinityCount(ctx context.Context, accountID int64, ttl time.Duration) (int, error) + // GetAccountAffinityCountBatch 批量获取多个账号的亲和客户端数量 + GetAccountAffinityCountBatch(ctx context.Context, accountIDs []int64, ttl time.Duration) (map[int64]int, error) + // GetAccountAffinityClients 获取账号的亲和客户端详情列表 + GetAccountAffinityClients(ctx context.Context, accountID int64, ttl time.Duration) ([]AffinityClientInfo, error) +} + +// AffinityClientInfo 表示一个亲和客户端的详情 +type AffinityClientInfo struct { + GroupID int64 `json:"group_id"` + ClientID string `json:"client_id"` + TTL int64 `json:"ttl"` // 剩余秒数 +} + +// ClientAffinityTTL 返回客户端亲和 TTL(与粘性会话 TTL 相同) +func ClientAffinityTTL() time.Duration { + return stickySessionTTL } // derefGroupID safely dereferences *int64 to int64, returning 0 if nil diff --git a/backend/internal/service/gemini_multiplatform_test.go b/backend/internal/service/gemini_multiplatform_test.go index 56f94d5f1f..a08ef690b2 100644 --- a/backend/internal/service/gemini_multiplatform_test.go +++ b/backend/internal/service/gemini_multiplatform_test.go @@ -297,6 +297,15 @@ func (m *mockGatewayCacheForGemini) UpdateClientAffinity(_ context.Context, _ in func (m *mockGatewayCacheForGemini) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { return nil } +func (m *mockGatewayCacheForGemini) GetAccountAffinityCount(_ context.Context, _ int64, _ time.Duration) (int, error) { + return 0, nil +} +func (m *mockGatewayCacheForGemini) GetAccountAffinityCountBatch(_ context.Context, _ []int64, _ time.Duration) (map[int64]int, error) { + return nil, nil +} +func (m *mockGatewayCacheForGemini) GetAccountAffinityClients(_ context.Context, _ int64, _ time.Duration) ([]AffinityClientInfo, error) { + return nil, nil +} // TestGeminiMessagesCompatService_SelectAccountForModelWithExclusions_GeminiPlatform 测试 Gemini 单平台选择 func TestGeminiMessagesCompatService_SelectAccountForModelWithExclusions_GeminiPlatform(t *testing.T) { diff --git a/backend/internal/service/openai_gateway_service_test.go b/backend/internal/service/openai_gateway_service_test.go index 7e53c8996e..7b7b6da415 100644 --- a/backend/internal/service/openai_gateway_service_test.go +++ b/backend/internal/service/openai_gateway_service_test.go @@ -274,6 +274,15 @@ func (c *stubGatewayCache) UpdateClientAffinity(_ context.Context, _ int64, _ st func (c *stubGatewayCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { return nil } +func (c *stubGatewayCache) GetAccountAffinityCount(_ context.Context, _ int64, _ time.Duration) (int, error) { + return 0, nil +} +func (c *stubGatewayCache) GetAccountAffinityCountBatch(_ context.Context, _ []int64, _ time.Duration) (map[int64]int, error) { + return nil, nil +} +func (c *stubGatewayCache) GetAccountAffinityClients(_ context.Context, _ int64, _ time.Duration) ([]AffinityClientInfo, error) { + return nil, nil +} func TestOpenAISelectAccountWithLoadAwareness_FiltersUnschedulable(t *testing.T) { now := time.Now() diff --git a/backend/internal/service/openai_ws_state_store_test.go b/backend/internal/service/openai_ws_state_store_test.go index 6f77f64356..273bfadddb 100644 --- a/backend/internal/service/openai_ws_state_store_test.go +++ b/backend/internal/service/openai_ws_state_store_test.go @@ -202,6 +202,15 @@ func (c *openAIWSStateStoreTimeoutProbeCache) UpdateClientAffinity(_ context.Con func (c *openAIWSStateStoreTimeoutProbeCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { return nil } +func (c *openAIWSStateStoreTimeoutProbeCache) GetAccountAffinityCount(_ context.Context, _ int64, _ time.Duration) (int, error) { + return 0, nil +} +func (c *openAIWSStateStoreTimeoutProbeCache) GetAccountAffinityCountBatch(_ context.Context, _ []int64, _ time.Duration) (map[int64]int, error) { + return nil, nil +} +func (c *openAIWSStateStoreTimeoutProbeCache) GetAccountAffinityClients(_ context.Context, _ int64, _ time.Duration) ([]AffinityClientInfo, error) { + return nil, nil +} func TestOpenAIWSStateStore_RedisOpsUseShortTimeout(t *testing.T) { probe := &openAIWSStateStoreTimeoutProbeCache{} diff --git a/backend/internal/testutil/stubs.go b/backend/internal/testutil/stubs.go index 669372ef51..8c6f54dd07 100644 --- a/backend/internal/testutil/stubs.go +++ b/backend/internal/testutil/stubs.go @@ -106,6 +106,15 @@ func (c StubGatewayCache) UpdateClientAffinity(_ context.Context, _ int64, _ str func (c StubGatewayCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { return nil } +func (c StubGatewayCache) GetAccountAffinityCount(_ context.Context, _ int64, _ time.Duration) (int, error) { + return 0, nil +} +func (c StubGatewayCache) GetAccountAffinityCountBatch(_ context.Context, _ []int64, _ time.Duration) (map[int64]int, error) { + return nil, nil +} +func (c StubGatewayCache) GetAccountAffinityClients(_ context.Context, _ int64, _ time.Duration) ([]service.AffinityClientInfo, error) { + return nil, nil +} // ============================================================ // StubSessionLimitCache — service.SessionLimitCache 的空实现 diff --git a/frontend/src/components/account/AccountStatusIndicator.vue b/frontend/src/components/account/AccountStatusIndicator.vue index 728b310ccc..3a8d632f42 100644 --- a/frontend/src/components/account/AccountStatusIndicator.vue +++ b/frontend/src/components/account/AccountStatusIndicator.vue @@ -1,5 +1,29 @@