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.
This commit is contained in:
erio committed 2026-03-06 15:48:58 +08:00
1 parent 63dff62673
commit 60f050d184
18 files changed
+339 -14

No files matched your search

@@ -65,6 +65,7 @@ func setupAccountDataRouter() (*gin.Engine, *stubAdminService) {
nil, nil,
nil, nil,
nil, nil,
nil,
) )
router.GET("/api/v1/admin/accounts/data", h.ExportData) router.GET("/api/v1/admin/accounts/data", h.ExportData)
@@ -55,6 +55,7 @@ type AccountHandler struct {
sessionLimitCache service.SessionLimitCache sessionLimitCache service.SessionLimitCache
rpmCache service.RPMCache rpmCache service.RPMCache
tokenCacheInvalidator service.TokenCacheInvalidator tokenCacheInvalidator service.TokenCacheInvalidator
gatewayCache service.GatewayCache
} }
// NewAccountHandler creates a new admin account handler // NewAccountHandler creates a new admin account handler
@@ -72,6 +73,7 @@ func NewAccountHandler(
sessionLimitCache service.SessionLimitCache, sessionLimitCache service.SessionLimitCache,
rpmCache service.RPMCache, rpmCache service.RPMCache,
tokenCacheInvalidator service.TokenCacheInvalidator, tokenCacheInvalidator service.TokenCacheInvalidator,
gatewayCache service.GatewayCache,
) *AccountHandler { ) *AccountHandler {
return &AccountHandler{ return &AccountHandler{
adminService: adminService, adminService: adminService,
@@ -87,6 +89,7 @@ func NewAccountHandler(
sessionLimitCache: sessionLimitCache, sessionLimitCache: sessionLimitCache,
rpmCache: rpmCache, rpmCache: rpmCache,
tokenCacheInvalidator: tokenCacheInvalidator, tokenCacheInvalidator: tokenCacheInvalidator,
gatewayCache: gatewayCache,
} }
} }
@@ -158,9 +161,10 @@ type AccountWithConcurrency struct {
*dto.Account *dto.Account
CurrentConcurrency int `json:"current_concurrency"` CurrentConcurrency int `json:"current_concurrency"`
// 以下字段仅对 Anthropic OAuth/SetupToken 账号有效,且仅在启用相应功能时返回 // 以下字段仅对 Anthropic OAuth/SetupToken 账号有效,且仅在启用相应功能时返回
CurrentWindowCost *float64 `json:"current_window_cost,omitempty"` // 当前窗口费用 CurrentWindowCost *float64 `json:"current_window_cost,omitempty"` // 当前窗口费用
ActiveSessions *int `json:"active_sessions,omitempty"` // 当前活跃会话数 ActiveSessions *int `json:"active_sessions,omitempty"` // 当前活跃会话数
CurrentRPM *int `json:"current_rpm,omitempty"` // 当前分钟 RPM 计数 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 { 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 windowCosts map[int64]float64
var activeSessions map[int64]int var activeSessions map[int64]int
var rpmCounts map[int64]int var rpmCounts map[int64]int
var affinityCounts map[int64]int
// 始终获取并发数(Redis ZCARD,极低开销) // 始终获取并发数(Redis ZCARD,极低开销)
if h.concurrencyService != nil { 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) windowCostAccountIDs := make([]int64, 0)
sessionLimitAccountIDs := make([]int64, 0) sessionLimitAccountIDs := make([]int64, 0)
rpmAccountIDs := make([]int64, 0) rpmAccountIDs := make([]int64, 0)
affinityAccountIDs := make([]int64, 0)
sessionIdleTimeouts := make(map[int64]time.Duration) // 各账号的会话空闲超时配置 sessionIdleTimeouts := make(map[int64]time.Duration) // 各账号的会话空闲超时配置
for i := range accounts { for i := range accounts {
acc := &accounts[i] acc := &accounts[i]
@@ -269,6 +275,9 @@ func (h *AccountHandler) List(c *gin.Context) {
if acc.GetBaseRPM() > 0 { if acc.GetBaseRPM() > 0 {
rpmAccountIDs = append(rpmAccountIDs, acc.ID) 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 查询后写入缓存 // 窗口费用获取:lite 模式从快照缓存读取,非 lite 模式执行 PostgreSQL 查询后写入缓存
if len(windowCostAccountIDs) > 0 { if len(windowCostAccountIDs) > 0 {
if lite { 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 result[i] = item
} }
@@ -1852,3 +1873,28 @@ func sanitizeExtraBaseRPM(extra map[string]any) {
} }
extra["base_rpm"] = v 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)
}
@@ -15,7 +15,7 @@ import (
func setupAccountMixedChannelRouter(adminSvc *stubAdminService) *gin.Engine { func setupAccountMixedChannelRouter(adminSvc *stubAdminService) *gin.Engine {
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
router := gin.New() 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/check-mixed-channel", accountHandler.CheckMixedChannel)
router.POST("/api/v1/admin/accounts", accountHandler.Create) router.POST("/api/v1/admin/accounts", accountHandler.Create)
router.PUT("/api/v1/admin/accounts/:id", accountHandler.Update) router.PUT("/api/v1/admin/accounts/:id", accountHandler.Update)
@@ -29,6 +29,7 @@ func TestAccountHandler_Create_AnthropicAPIKeyPassthroughExtraForwarded(t *testi
nil, nil,
nil, nil,
nil, nil,
nil,
) )
router := gin.New() router := gin.New()
@@ -36,7 +36,7 @@ func (f *failingAdminService) UpdateAccount(ctx context.Context, id int64, input
func setupAccountHandlerWithService(adminSvc service.AdminService) (*gin.Engine, *AccountHandler) { func setupAccountHandlerWithService(adminSvc service.AdminService) (*gin.Engine, *AccountHandler) {
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
router := gin.New() 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) router.POST("/api/v1/admin/accounts/batch-update-credentials", handler.BatchUpdateCredentials)
return router, handler return router, handler
} }
+135 -6
View File
@@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/Wei-Shaw/sub2api/internal/service" "github.com/Wei-Shaw/sub2api/internal/service"
@@ -11,8 +12,9 @@ import (
) )
const ( const (
stickySessionPrefix = "sticky_session:" stickySessionPrefix = "sticky_session:"
clientAffinityPrefix = "client_affinity:" clientAffinityPrefix = "client_affinity:"
accountAffinityPrefix = "account_affinity:"
) )
type gatewayCache struct { type gatewayCache struct {
@@ -70,16 +72,21 @@ redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])
return redis.call('ZREVRANGE', KEYS[1], 0, -1) return redis.call('ZREVRANGE', KEYS[1], 0, -1)
`) `)
// updateAffinityScript: 清理过期成员、添加/更新亲和关系、刷新 TTL // updateAffinityScript: 清理过期成员、添加/更新亲和关系、刷新 TTL,
// 同时维护反向索引 account_affinity:{accountID}
// KEYS[1] = client_affinity:{groupID}:{clientID} // KEYS[1] = client_affinity:{groupID}:{clientID}
// KEYS[2] = account_affinity:{accountID}
// ARGV[1] = 当前时间戳 // ARGV[1] = 当前时间戳
// ARGV[2] = TTL 秒数 // ARGV[2] = TTL 秒数
// ARGV[3] = accountID // ARGV[3] = accountID
// ARGV[4] = 过期阈值时间戳 (now - ttl) // ARGV[4] = 过期阈值时间戳 (now - ttl)
// ARGV[5] = 反向索引成员 "{groupID}:{clientID}"
var updateAffinityScript = redis.NewScript(` var updateAffinityScript = redis.NewScript(`
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[4]) redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[4])
redis.call('ZADD', KEYS[1], ARGV[1], ARGV[3]) redis.call('ZADD', KEYS[1], ARGV[1], ARGV[3])
redis.call('EXPIRE', KEYS[1], ARGV[2]) 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 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 { func (c *gatewayCache) UpdateClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64, ttl time.Duration) error {
key := buildAffinityKey(groupID, clientID) key := buildAffinityKey(groupID, clientID)
reverseKey := buildAccountAffinityKey(accountID)
reverseMember := fmt.Sprintf("%d:%s", groupID, clientID)
now := time.Now().Unix() now := time.Now().Unix()
ttlSeconds := int64(ttl.Seconds()) ttlSeconds := int64(ttl.Seconds())
expireThreshold := now - ttlSeconds expireThreshold := now - ttlSeconds
return updateAffinityScript.Run(ctx, c.rdb, []string{key}, return updateAffinityScript.Run(ctx, c.rdb, []string{key, reverseKey},
now, ttlSeconds, accountID, expireThreshold, now, ttlSeconds, accountID, expireThreshold, reverseMember,
).Err() ).Err()
} }
func (c *gatewayCache) RemoveClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64) error { func (c *gatewayCache) RemoveClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64) error {
key := buildAffinityKey(groupID, clientID) 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
} }
+1 -1
View File
@@ -648,7 +648,7 @@ func newContractDeps(t *testing.T) *contractDeps {
apiKeyHandler := handler.NewAPIKeyHandler(apiKeyService) apiKeyHandler := handler.NewAPIKeyHandler(apiKeyService)
usageHandler := handler.NewUsageHandler(usageService, apiKeyService) usageHandler := handler.NewUsageHandler(usageService, apiKeyService)
adminSettingHandler := adminhandler.NewSettingHandler(settingService, nil, nil, nil, nil) 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) { jwtAuth := func(c *gin.Context) {
c.Set(string(middleware.ContextKeyUser), middleware.AuthSubject{ c.Set(string(middleware.ContextKeyUser), middleware.AuthSubject{
+1
View File
@@ -257,6 +257,7 @@ func registerAccountRoutes(admin *gin.RouterGroup, h *handler.Handlers) {
accounts.DELETE("/:id/temp-unschedulable", h.Admin.Account.ClearTempUnschedulable) accounts.DELETE("/:id/temp-unschedulable", h.Admin.Account.ClearTempUnschedulable)
accounts.POST("/:id/schedulable", h.Admin.Account.SetSchedulable) accounts.POST("/:id/schedulable", h.Admin.Account.SetSchedulable)
accounts.GET("/:id/models", h.Admin.Account.GetAvailableModels) 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.POST("/batch", h.Admin.Account.BatchCreate)
accounts.GET("/data", h.Admin.Account.ExportData) accounts.GET("/data", h.Admin.Account.ExportData)
accounts.POST("/data", h.Admin.Account.ImportData) accounts.POST("/data", h.Admin.Account.ImportData)
@@ -39,6 +39,15 @@ func (c *stubSmartRetryCache) UpdateClientAffinity(_ context.Context, _ int64, _
func (c *stubSmartRetryCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { func (c *stubSmartRetryCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error {
return nil 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 // mockSmartRetryUpstream 用于 handleSmartRetry 测试的 mock upstream
type mockSmartRetryUpstream struct { type mockSmartRetryUpstream struct {
@@ -152,6 +152,15 @@ func (s *stickyGatewayCacheHotpathStub) UpdateClientAffinity(_ context.Context,
func (s *stickyGatewayCacheHotpathStub) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { func (s *stickyGatewayCacheHotpathStub) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error {
return nil 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) { func (s *modelsListAccountRepoStub) ListSchedulableByGroupID(ctx context.Context, groupID int64) ([]Account, error) {
s.listByGroupCalls.Add(1) s.listByGroupCalls.Add(1)
@@ -244,6 +244,15 @@ func (m *mockGatewayCacheForPlatform) UpdateClientAffinity(_ context.Context, _
func (m *mockGatewayCacheForPlatform) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { func (m *mockGatewayCacheForPlatform) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error {
return nil 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 { type mockGroupRepoForGateway struct {
groups map[int64]*Group groups map[int64]*Group
@@ -375,6 +375,25 @@ type GatewayCache interface {
UpdateClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64, ttl time.Duration) error UpdateClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64, ttl time.Duration) error
// RemoveClientAffinity 删除单个客户端亲和关系 // RemoveClientAffinity 删除单个客户端亲和关系
RemoveClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64) error 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 // derefGroupID safely dereferences *int64 to int64, returning 0 if nil
@@ -297,6 +297,15 @@ func (m *mockGatewayCacheForGemini) UpdateClientAffinity(_ context.Context, _ in
func (m *mockGatewayCacheForGemini) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { func (m *mockGatewayCacheForGemini) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error {
return nil 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 单平台选择 // TestGeminiMessagesCompatService_SelectAccountForModelWithExclusions_GeminiPlatform 测试 Gemini 单平台选择
func TestGeminiMessagesCompatService_SelectAccountForModelWithExclusions_GeminiPlatform(t *testing.T) { func TestGeminiMessagesCompatService_SelectAccountForModelWithExclusions_GeminiPlatform(t *testing.T) {
@@ -274,6 +274,15 @@ func (c *stubGatewayCache) UpdateClientAffinity(_ context.Context, _ int64, _ st
func (c *stubGatewayCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { func (c *stubGatewayCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error {
return nil 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) { func TestOpenAISelectAccountWithLoadAwareness_FiltersUnschedulable(t *testing.T) {
now := time.Now() now := time.Now()
@@ -202,6 +202,15 @@ func (c *openAIWSStateStoreTimeoutProbeCache) UpdateClientAffinity(_ context.Con
func (c *openAIWSStateStoreTimeoutProbeCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { func (c *openAIWSStateStoreTimeoutProbeCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error {
return nil 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) { func TestOpenAIWSStateStore_RedisOpsUseShortTimeout(t *testing.T) {
probe := &openAIWSStateStoreTimeoutProbeCache{} probe := &openAIWSStateStoreTimeoutProbeCache{}
+9
View File
@@ -106,6 +106,15 @@ func (c StubGatewayCache) UpdateClientAffinity(_ context.Context, _ int64, _ str
func (c StubGatewayCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error { func (c StubGatewayCache) RemoveClientAffinity(_ context.Context, _ int64, _ string, _ int64) error {
return nil 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 的空实现 // StubSessionLimitCache — service.SessionLimitCache 的空实现
@@ -1,5 +1,29 @@
<template> <template>
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<!-- Affinity Client Count Badge -->
<div
v-if="affinityClientCount > 0"
class="group/affinity relative"
@mouseenter="loadAffinityClients"
>
<span class="inline-flex items-center rounded-full bg-blue-100 px-1.5 py-0.5 text-xs font-medium text-blue-700 dark:bg-blue-900/30 dark:text-blue-400">
{{ affinityClientCount }}
</span>
<!-- Tooltip: lazy-loaded client list -->
<div
class="invisible absolute left-0 top-full z-[100] mt-1.5 min-w-[200px] max-w-[350px] rounded-lg bg-gray-800 px-3 py-2 text-xs text-white opacity-0 shadow-xl transition-all duration-200 group-hover/affinity:visible group-hover/affinity:opacity-100 dark:bg-gray-900"
>
<div v-if="affinityLoading" class="text-gray-400">Loading...</div>
<div v-else-if="affinityClients.length">
<div v-for="client in affinityClients" :key="client.client_id" class="py-0.5">
<span class="font-mono text-blue-300">{{ client.group_id }}:{{ client.client_id }}</span>
<span class="ml-1 text-gray-400">{{ formatAffinityTTL(client.ttl) }}</span>
</div>
</div>
<div class="absolute bottom-full left-3 border-[6px] border-transparent border-b-gray-800 dark:border-b-gray-900"></div>
</div>
</div>
<!-- Rate Limit Display (429) - Two-line layout --> <!-- Rate Limit Display (429) - Two-line layout -->
<div v-if="isRateLimited" class="flex flex-col items-center gap-1"> <div v-if="isRateLimited" class="flex flex-col items-center gap-1">
<span class="badge text-xs badge-warning">{{ t('admin.accounts.status.rateLimited') }}</span> <span class="badge text-xs badge-warning">{{ t('admin.accounts.status.rateLimited') }}</span>
@@ -129,10 +153,11 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue' import { computed, ref } from 'vue'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import type { Account } from '@/types' import type { Account } from '@/types'
import { formatCountdownWithSuffix, formatTime } from '@/utils/format' import { formatCountdownWithSuffix, formatTime } from '@/utils/format'
import { apiClient } from '@/api/client'
const { t } = useI18n() const { t } = useI18n()
@@ -144,6 +169,45 @@ const emit = defineEmits<{
(e: 'show-temp-unsched', account: Account): void (e: 'show-temp-unsched', account: Account): void
}>() }>()
// Affinity client count
interface AffinityClient {
group_id: number
client_id: string
ttl: number
}
const affinityClientCount = computed(() => {
return props.account.affinity_client_count ?? 0
})
const affinityClients = ref<AffinityClient[]>([])
const affinityLoading = ref(false)
let affinityLoaded = false
const loadAffinityClients = async () => {
if (affinityLoaded || affinityLoading.value) return
affinityLoading.value = true
try {
const { data } = await apiClient.get(`/admin/accounts/${props.account.id}/affinity-clients`)
affinityClients.value = data?.data ?? []
} catch {
affinityClients.value = []
} finally {
affinityLoading.value = false
affinityLoaded = true
}
}
const formatAffinityTTL = (seconds: number): string => {
if (seconds <= 0) return '0s'
const h = Math.floor(seconds / 3600)
const m = Math.floor((seconds % 3600) / 60)
const s = seconds % 60
if (h > 0) return `${h}h${m}m`
if (m > 0) return `${m}m${s}s`
return `${s}s`
}
// Computed: is rate limited (429) // Computed: is rate limited (429)
const isRateLimited = computed(() => { const isRateLimited = computed(() => {
if (!props.account.rate_limit_reset_at) return false if (!props.account.rate_limit_reset_at) return false
+1
View File
@@ -722,6 +722,7 @@ export interface Account {
current_window_cost?: number | null // 当前窗口费用 current_window_cost?: number | null // 当前窗口费用
active_sessions?: number | null // 当前活跃会话数 active_sessions?: number | null // 当前活跃会话数
current_rpm?: number | null // 当前分钟 RPM 计数 current_rpm?: number | null // 当前分钟 RPM 计数
affinity_client_count?: number | null // 亲和客户端数量
} }
// Account Usage types // Account Usage types