Skip to content

Commit c1c31ed

Browse files
author
QTom
committed
feat: wire RPMCache into GatewayService and AccountHandler
1 parent 777be05 commit c1c31ed

5 files changed

Lines changed: 19 additions & 3 deletions

File tree

backend/cmd/server/wire_gen.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/internal/handler/admin/account_handler.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type AccountHandler struct {
5353
concurrencyService *service.ConcurrencyService
5454
crsSyncService *service.CRSSyncService
5555
sessionLimitCache service.SessionLimitCache
56+
rpmCache service.RPMCache
5657
tokenCacheInvalidator service.TokenCacheInvalidator
5758
}
5859

@@ -69,6 +70,7 @@ func NewAccountHandler(
6970
concurrencyService *service.ConcurrencyService,
7071
crsSyncService *service.CRSSyncService,
7172
sessionLimitCache service.SessionLimitCache,
73+
rpmCache service.RPMCache,
7274
tokenCacheInvalidator service.TokenCacheInvalidator,
7375
) *AccountHandler {
7476
return &AccountHandler{
@@ -83,6 +85,7 @@ func NewAccountHandler(
8385
concurrencyService: concurrencyService,
8486
crsSyncService: crsSyncService,
8587
sessionLimitCache: sessionLimitCache,
88+
rpmCache: rpmCache,
8689
tokenCacheInvalidator: tokenCacheInvalidator,
8790
}
8891
}
@@ -154,6 +157,7 @@ type AccountWithConcurrency struct {
154157
// 以下字段仅对 Anthropic OAuth/SetupToken 账号有效,且仅在启用相应功能时返回
155158
CurrentWindowCost *float64 `json:"current_window_cost,omitempty"` // 当前窗口费用
156159
ActiveSessions *int `json:"active_sessions,omitempty"` // 当前活跃会话数
160+
CurrentRPM *int `json:"current_rpm,omitempty"` // 当前分钟 RPM 计数
157161
}
158162

159163
func (h *AccountHandler) buildAccountResponseWithRuntime(ctx context.Context, account *service.Account) AccountWithConcurrency {
@@ -189,6 +193,12 @@ func (h *AccountHandler) buildAccountResponseWithRuntime(ctx context.Context, ac
189193
}
190194
}
191195
}
196+
197+
if h.rpmCache != nil && account.GetBaseRPM() > 0 {
198+
if rpm, err := h.rpmCache.GetRPM(ctx, account.ID); err == nil {
199+
item.CurrentRPM = &rpm
200+
}
201+
}
192202
}
193203

194204
return item

backend/internal/repository/rpm_cache.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/Wei-Shaw/sub2api/internal/service"
78
"github.com/redis/go-redis/v9"
89
)
910

@@ -36,7 +37,7 @@ type RPMCacheImpl struct {
3637
rdb *redis.Client
3738
}
3839

39-
func NewRPMCache(rdb *redis.Client) *RPMCacheImpl {
40+
func NewRPMCache(rdb *redis.Client) service.RPMCache {
4041
return &RPMCacheImpl{rdb: rdb}
4142
}
4243

backend/internal/repository/wire.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ var ProviderSet = wire.NewSet(
7979
NewTimeoutCounterCache,
8080
ProvideConcurrencyCache,
8181
ProvideSessionLimitCache,
82+
NewRPMCache,
8283
NewDashboardCache,
8384
NewEmailCache,
8485
NewIdentityCache,

backend/internal/service/gateway_service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ type GatewayService struct {
520520
concurrencyService *ConcurrencyService
521521
claudeTokenProvider *ClaudeTokenProvider
522522
sessionLimitCache SessionLimitCache // 会话数量限制缓存(仅 Anthropic OAuth/SetupToken)
523+
rpmCache RPMCache // RPM 计数缓存(仅 Anthropic OAuth/SetupToken)
523524
userGroupRateCache *gocache.Cache
524525
userGroupRateSF singleflight.Group
525526
modelsListCache *gocache.Cache
@@ -549,6 +550,7 @@ func NewGatewayService(
549550
deferredService *DeferredService,
550551
claudeTokenProvider *ClaudeTokenProvider,
551552
sessionLimitCache SessionLimitCache,
553+
rpmCache RPMCache,
552554
digestStore *DigestSessionStore,
553555
) *GatewayService {
554556
userGroupRateTTL := resolveUserGroupRateCacheTTL(cfg)
@@ -574,6 +576,7 @@ func NewGatewayService(
574576
deferredService: deferredService,
575577
claudeTokenProvider: claudeTokenProvider,
576578
sessionLimitCache: sessionLimitCache,
579+
rpmCache: rpmCache,
577580
userGroupRateCache: gocache.New(userGroupRateTTL, time.Minute),
578581
modelsListCache: gocache.New(modelsListTTL, time.Minute),
579582
modelsListCacheTTL: modelsListTTL,

0 commit comments

Comments
 (0)