diff --git a/backend/internal/handler/admin/account_handler.go b/backend/internal/handler/admin/account_handler.go index 33c91dae..c81bcc29 100644 --- a/backend/internal/handler/admin/account_handler.go +++ b/backend/internal/handler/admin/account_handler.go @@ -211,12 +211,8 @@ func (h *AccountHandler) List(c *gin.Context) { } accCopy := acc // 闭包捕获 g.Go(func() error { - var startTime time.Time - if accCopy.SessionWindowStart != nil { - startTime = *accCopy.SessionWindowStart - } else { - startTime = time.Now().Add(-5 * time.Hour) - } + // 使用统一的窗口开始时间计算逻辑(考虑窗口过期情况) + startTime := accCopy.GetCurrentWindowStartTime() stats, err := h.accountUsageService.GetAccountWindowStats(gctx, accCopy.ID, startTime) if err == nil && stats != nil { mu.Lock() diff --git a/backend/internal/repository/account_repo.go b/backend/internal/repository/account_repo.go index f7725820..6f4f0e81 100644 --- a/backend/internal/repository/account_repo.go +++ b/backend/internal/repository/account_repo.go @@ -960,7 +960,16 @@ func (r *accountRepository) UpdateSessionWindow(ctx context.Context, id int64, s builder.SetSessionWindowEnd(*end) } _, err := builder.Save(ctx) - return err + if err != nil { + return err + } + // 触发调度器缓存更新(仅当窗口时间有变化时) + if start != nil || end != nil { + if err := enqueueSchedulerOutbox(ctx, r.sql, service.SchedulerOutboxEventAccountChanged, &id, nil, nil); err != nil { + log.Printf("[SchedulerOutbox] enqueue session window update failed: account=%d err=%v", id, err) + } + } + return nil } func (r *accountRepository) SetSchedulable(ctx context.Context, id int64, schedulable bool) error { diff --git a/backend/internal/service/account.go b/backend/internal/service/account.go index 16c6bb45..94fa107e 100644 --- a/backend/internal/service/account.go +++ b/backend/internal/service/account.go @@ -671,6 +671,23 @@ func (a *Account) CheckWindowCostSchedulability(currentWindowCost float64) Windo return WindowCostNotSchedulable } +// GetCurrentWindowStartTime 获取当前有效的窗口开始时间 +// 逻辑: +// 1. 如果窗口未过期(SessionWindowEnd 存在且在当前时间之后),使用记录的 SessionWindowStart +// 2. 否则(窗口过期或未设置),使用新的预测窗口开始时间(从当前整点开始) +func (a *Account) GetCurrentWindowStartTime() time.Time { + now := time.Now() + + // 窗口未过期,使用记录的窗口开始时间 + if a.SessionWindowStart != nil && a.SessionWindowEnd != nil && now.Before(*a.SessionWindowEnd) { + return *a.SessionWindowStart + } + + // 窗口已过期或未设置,预测新的窗口开始时间(从当前整点开始) + // 与 ratelimit_service.go 中 UpdateSessionWindow 的预测逻辑保持一致 + return time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()) +} + // parseExtraFloat64 从 extra 字段解析 float64 值 func parseExtraFloat64(value any) float64 { switch v := value.(type) { diff --git a/backend/internal/service/account_usage_service.go b/backend/internal/service/account_usage_service.go index 6f012385..93d8a2f3 100644 --- a/backend/internal/service/account_usage_service.go +++ b/backend/internal/service/account_usage_service.go @@ -369,12 +369,8 @@ func (s *AccountUsageService) addWindowStats(ctx context.Context, account *Accou // 如果没有缓存,从数据库查询 if windowStats == nil { - var startTime time.Time - if account.SessionWindowStart != nil { - startTime = *account.SessionWindowStart - } else { - startTime = time.Now().Add(-5 * time.Hour) - } + // 使用统一的窗口开始时间计算逻辑(考虑窗口过期情况) + startTime := account.GetCurrentWindowStartTime() stats, err := s.usageLogRepo.GetAccountWindowStats(ctx, account.ID, startTime) if err != nil { diff --git a/backend/internal/service/gateway_service.go b/backend/internal/service/gateway_service.go index 2363cef2..01d7cbf3 100644 --- a/backend/internal/service/gateway_service.go +++ b/backend/internal/service/gateway_service.go @@ -1228,12 +1228,8 @@ func (s *GatewayService) isAccountSchedulableForWindowCost(ctx context.Context, // 缓存未命中,从数据库查询 { - var startTime time.Time - if account.SessionWindowStart != nil { - startTime = *account.SessionWindowStart - } else { - startTime = time.Now().Add(-5 * time.Hour) - } + // 使用统一的窗口开始时间计算逻辑(考虑窗口过期情况) + startTime := account.GetCurrentWindowStartTime() stats, err := s.usageLogRepo.GetAccountWindowStats(ctx, account.ID, startTime) if err != nil {