From fbb572948ddc6a65047003ea65f85e6539124368 Mon Sep 17 00:00:00 2001 From: shaw Date: Mon, 19 Jan 2026 11:45:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E6=9F=A5=E8=AF=A2=E4=BD=BF=E7=94=A8=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E8=B6=85=E6=97=B6=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/handler/admin/account_handler.go | 6 ++++-- backend/internal/repository/session_limit_cache.go | 11 +++++++++-- backend/internal/service/session_limit_cache.go | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/backend/internal/handler/admin/account_handler.go b/backend/internal/handler/admin/account_handler.go index 45d80272..10a53f56 100644 --- a/backend/internal/handler/admin/account_handler.go +++ b/backend/internal/handler/admin/account_handler.go @@ -173,6 +173,7 @@ func (h *AccountHandler) List(c *gin.Context) { // 识别需要查询窗口费用和会话数的账号(Anthropic OAuth/SetupToken 且启用了相应功能) windowCostAccountIDs := make([]int64, 0) sessionLimitAccountIDs := make([]int64, 0) + sessionIdleTimeouts := make(map[int64]time.Duration) // 各账号的会话空闲超时配置 for i := range accounts { acc := &accounts[i] if acc.IsAnthropicOAuthOrSetupToken() { @@ -181,6 +182,7 @@ func (h *AccountHandler) List(c *gin.Context) { } if acc.GetMaxSessions() > 0 { sessionLimitAccountIDs = append(sessionLimitAccountIDs, acc.ID) + sessionIdleTimeouts[acc.ID] = time.Duration(acc.GetSessionIdleTimeoutMinutes()) * time.Minute } } } @@ -189,9 +191,9 @@ func (h *AccountHandler) List(c *gin.Context) { var windowCosts map[int64]float64 var activeSessions map[int64]int - // 获取活跃会话数(批量查询) + // 获取活跃会话数(批量查询,传入各账号的 idleTimeout 配置) if len(sessionLimitAccountIDs) > 0 && h.sessionLimitCache != nil { - activeSessions, _ = h.sessionLimitCache.GetActiveSessionCountBatch(c.Request.Context(), sessionLimitAccountIDs) + activeSessions, _ = h.sessionLimitCache.GetActiveSessionCountBatch(c.Request.Context(), sessionLimitAccountIDs, sessionIdleTimeouts) if activeSessions == nil { activeSessions = make(map[int64]int) } diff --git a/backend/internal/repository/session_limit_cache.go b/backend/internal/repository/session_limit_cache.go index 16f2a69c..3dc89f87 100644 --- a/backend/internal/repository/session_limit_cache.go +++ b/backend/internal/repository/session_limit_cache.go @@ -217,7 +217,7 @@ func (c *sessionLimitCache) GetActiveSessionCount(ctx context.Context, accountID } // GetActiveSessionCountBatch 批量获取多个账号的活跃会话数 -func (c *sessionLimitCache) GetActiveSessionCountBatch(ctx context.Context, accountIDs []int64) (map[int64]int, error) { +func (c *sessionLimitCache) GetActiveSessionCountBatch(ctx context.Context, accountIDs []int64, idleTimeouts map[int64]time.Duration) (map[int64]int, error) { if len(accountIDs) == 0 { return make(map[int64]int), nil } @@ -226,11 +226,18 @@ func (c *sessionLimitCache) GetActiveSessionCountBatch(ctx context.Context, acco // 使用 pipeline 批量执行 pipe := c.rdb.Pipeline() - idleTimeoutSeconds := int(c.defaultIdleTimeout.Seconds()) cmds := make(map[int64]*redis.Cmd, len(accountIDs)) for _, accountID := range accountIDs { key := sessionLimitKey(accountID) + // 使用各账号自己的 idleTimeout,如果没有则用默认值 + idleTimeout := c.defaultIdleTimeout + if idleTimeouts != nil { + if t, ok := idleTimeouts[accountID]; ok && t > 0 { + idleTimeout = t + } + } + idleTimeoutSeconds := int(idleTimeout.Seconds()) cmds[accountID] = getActiveSessionCountScript.Run(ctx, pipe, []string{key}, idleTimeoutSeconds) } diff --git a/backend/internal/service/session_limit_cache.go b/backend/internal/service/session_limit_cache.go index f6f0c26a..5482d610 100644 --- a/backend/internal/service/session_limit_cache.go +++ b/backend/internal/service/session_limit_cache.go @@ -38,8 +38,9 @@ type SessionLimitCache interface { GetActiveSessionCount(ctx context.Context, accountID int64) (int, error) // GetActiveSessionCountBatch 批量获取多个账号的活跃会话数 + // idleTimeouts: 每个账号的空闲超时时间配置,key 为 accountID;若为 nil 或某账号不在其中,则使用默认超时 // 返回 map[accountID]count,查询失败的账号不在 map 中 - GetActiveSessionCountBatch(ctx context.Context, accountIDs []int64) (map[int64]int, error) + GetActiveSessionCountBatch(ctx context.Context, accountIDs []int64, idleTimeouts map[int64]time.Duration) (map[int64]int, error) // IsSessionActive 检查特定会话是否活跃(未过期) IsSessionActive(ctx context.Context, accountID int64, sessionUUID string) (bool, error)