mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
每个请求一个扣费事务时,同一用户的 UPDATE users SET balance 在同一行上排队抢行锁,单用户吞吐 约 1000 RPS;用量队列溢出转同步扣费后,延迟升到十几秒、内存涨到 GB 级。 - 新增 UsageBillingLaneCombiner 作为 UsageBillingRepository 注入:按用户 id 分 64 片,每个用户 一条结算通道。通道空闲时请求立即走单条 Apply;结算进行中到达的请求排队,下一轮最多 128 条在 一个事务内由 ApplyBatch 结算,每个调用方拿到自己的结果。不设攒批等待计时器,也不设配置开关。 - ApplyBatch 与按顺序逐条 Apply 等价:批量认领 usage_billing_dedup(指纹冲突、归档命中按条返回), 每个用户只扣一次余额;透支、Key 额度耗尽、账号额度越线等逐条结果用 decimal 前缀和还原。加锁 顺序为 user_subscriptions → users → api_keys → accounts,表内按 id 升序。账号额度越线时每批 每个账号写一条 scheduler outbox。 - 批量返回错误或 panic 时整批回滚,逐条回退到单条 Apply,共用一个截止时间;排队请求的结算超时 从结算开始时计。 - applyUsageBilling 在 Apply 返回后用独立超时做鉴权缓存失效与余额缓存同步。 - BatchUpdateLastUsed 在同一事务内先按 id 升序 FOR NO KEY UPDATE 锁定账号行再更新,不阻塞引用 accounts 的外键检查。 - 停机时 cleanup 等待各通道排空,之后的请求直连仓储。 bench(关闭在途预留):单用户闭环 200 并发 823 → 1564 RPS; s1500 成功率 70% → 100%;16 用户闭环 1355 → 1619 RPS;s1000 p99 总耗时 10.2s → 2.3s、内存 3.2GB → 1.2GB(对比改动前)。低负载无回退,对账差额为 0。
113 lines
3.2 KiB
Go
113 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/Wei-Shaw/sub2api/internal/handler"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestProvideServiceBuildInfo(t *testing.T) {
|
|
in := handler.BuildInfo{
|
|
Version: "v-test",
|
|
BuildType: "release",
|
|
}
|
|
out := provideServiceBuildInfo(in)
|
|
require.Equal(t, in.Version, out.Version)
|
|
require.Equal(t, in.BuildType, out.BuildType)
|
|
}
|
|
|
|
func TestProvideCleanup_WithMinimalDependencies_NoPanic(t *testing.T) {
|
|
cfg := &config.Config{}
|
|
|
|
oauthSvc := service.NewOAuthService(nil, nil)
|
|
openAIOAuthSvc := service.NewOpenAIOAuthService(nil, nil)
|
|
geminiOAuthSvc := service.NewGeminiOAuthService(nil, nil, nil, nil, cfg)
|
|
antigravityOAuthSvc := service.NewAntigravityOAuthService(nil)
|
|
|
|
tokenRefreshSvc := service.NewTokenRefreshService(
|
|
nil,
|
|
oauthSvc,
|
|
openAIOAuthSvc,
|
|
geminiOAuthSvc,
|
|
antigravityOAuthSvc,
|
|
nil,
|
|
nil,
|
|
cfg,
|
|
nil,
|
|
)
|
|
accountExpirySvc := service.NewAccountExpiryService(nil, time.Second)
|
|
codexVersionSyncSvc := service.NewOpenAICodexVersionSyncService(nil, nil, nil, time.Second)
|
|
claudeCodeVersionSyncSvc := service.NewClaudeCodeVersionSyncService(nil, nil, nil, time.Second)
|
|
proxyExpirySvc := service.NewProxyExpiryService(nil, time.Second)
|
|
subscriptionExpirySvc := service.NewSubscriptionExpiryService(nil, time.Second)
|
|
pricingSvc := service.NewPricingService(cfg, nil)
|
|
emailQueueSvc := service.NewEmailQueueService(nil, 1)
|
|
billingCacheSvc := service.NewBillingCacheService(nil, nil, nil, nil, nil, nil, cfg, nil)
|
|
idempotencyCleanupSvc := service.NewIdempotencyCleanupService(nil, cfg)
|
|
schedulerSnapshotSvc := service.NewSchedulerSnapshotService(nil, nil, nil, nil, cfg)
|
|
opsSystemLogSinkSvc := service.NewOpsSystemLogSink(nil)
|
|
|
|
cleanup := provideCleanup(
|
|
nil, // entClient
|
|
nil, // redis
|
|
&service.OpsMetricsCollector{},
|
|
&service.OpsAggregationService{},
|
|
&service.OpsAlertEvaluatorService{},
|
|
&service.OpsCleanupService{},
|
|
&service.OpsScheduledReportService{},
|
|
opsSystemLogSinkSvc,
|
|
nil, // opsService
|
|
nil, // opsIngressRejectAggregator
|
|
nil, // apiKeyService
|
|
nil, // authCacheInvalidationWorker
|
|
schedulerSnapshotSvc,
|
|
tokenRefreshSvc,
|
|
accountExpirySvc,
|
|
nil, // cnProviderBalanceCheck
|
|
codexVersionSyncSvc,
|
|
claudeCodeVersionSyncSvc,
|
|
proxyExpirySvc,
|
|
subscriptionExpirySvc,
|
|
&service.UsageCleanupService{},
|
|
idempotencyCleanupSvc,
|
|
service.NewGeminiCachedContentService(nil, nil, cfg),
|
|
&service.BatchImageCleanupService{},
|
|
nil, // batchImageWorker
|
|
pricingSvc,
|
|
emailQueueSvc,
|
|
billingCacheSvc,
|
|
&service.UsageRecordWorkerPool{},
|
|
nil, // usageBillingLanes
|
|
service.ProvideBalanceNotifyService(nil, nil, nil, nil),
|
|
service.NewDigestSessionStore(),
|
|
&service.SubscriptionService{},
|
|
oauthSvc,
|
|
openAIOAuthSvc,
|
|
geminiOAuthSvc,
|
|
antigravityOAuthSvc,
|
|
nil, // grokOAuth
|
|
nil, // openAIGateway
|
|
nil, // scheduledTestRunner
|
|
nil, // backupSvc
|
|
nil, // paymentOrderExpiry
|
|
nil, // channelMonitorRunner
|
|
nil, // channelMonitorV2Aggregator
|
|
nil, // quotaFlusher
|
|
nil, // upstreamBillingProbe
|
|
nil, // ollamaCloudUsage
|
|
nil, // opencodeGoUsage
|
|
nil, // auditLog
|
|
nil, // openAIAutoReset
|
|
nil, // promptAudit
|
|
nil, // pluginManager
|
|
)
|
|
|
|
require.NotPanics(t, func() {
|
|
cleanup()
|
|
})
|
|
}
|