perf: 流式错误判定预过滤、再减一个写索引,加可选明细保留期

- 流式放行前的上游错误判定加廉价子串预过滤("error" / response.failed):
  长思考阶段几十上百条事件不再逐条 json.Unmarshal。
- usage_logs 去掉 idx_channel_created:没有任何查询按 channel_id 过滤该表
  (渠道统计走 created_at 时间窗后在内存分组),复合索引只是每行一次多余的
  B-tree 插入;schema v9 迁移里显式 DROP 旧库索引。至此该表只剩
  (user_id, created_at)、created_at 两个真被用到的索引。
- 新增可选保留期(OT_USAGE_RETENTION_DAYS,默认 0=永久保留):每天分批
  (2000 行/批、批间 50ms)清理过期 usage_logs,避免长期运行后时间窗查询
  与备份随表增长变慢;余额流水 balance_logs 不在清理范围。

参数复测(记账稳态吞吐,3 次取中位):
- batch 64(当前)30.2k rows/s;batch 128 19.0k、batch 256 14.3k(更差,保持 64)
- wal_autocheckpoint(4000)、cache_size(-64MB) 与当前配置无差别,不引入

验证:go vet、go test ./...、-race(usage/proxy/store)全绿;新增保留期三项测试
(只删明细不动流水、未开启不动数据、跨分批删完)。
This commit is contained in:
Sakurasan committed 2026-10-30 16:45:57 +08:00
1 parent 8e5fe16611
commit 531a03c836
9 files changed
+224 -20

No files matched your search

+20 -12
View File
@@ -19,18 +19,20 @@ import (
)
type App struct {
Cfg *config.Config
DB *gorm.DB
Hasher *crypto.PasswordHasher
Enc *crypto.Encryptor
JWT *jwt.Manager
Usage *usage.Recorder
Health *channel.HealthMonitor
Limit *ratelimit.Limiter
Passkeys *passkey.Service
startedAt time.Time
ctx context.Context
cancel context.CancelFunc
Cfg *config.Config
DB *gorm.DB
Hasher *crypto.PasswordHasher
Enc *crypto.Encryptor
JWT *jwt.Manager
Usage *usage.Recorder
Health *channel.HealthMonitor
Limit *ratelimit.Limiter
Passkeys *passkey.Service
// usageRetention 用量明细保留期任务(未开启时为 no-op)
usageRetention func()
startedAt time.Time
ctx context.Context
cancel context.CancelFunc
}
func New(cfg *config.Config) (*App, error) {
@@ -61,6 +63,9 @@ func New(cfg *config.Config) (*App, error) {
})
a.Health.Start(a.ctx)
// 用量明细保留期(默认 0=永久保留,只清明细不动余额流水)
a.usageRetention = usage.StartRetention(a.ctx, db, cfg.Usage.RetentionDays)
a.Passkeys, err = passkey.New(db, passkey.Config{
RPID: cfg.WebAuthn.RPID, Origin: cfg.WebAuthn.RPOrigin, Name: cfg.WebAuthn.RPName,
})
@@ -71,6 +76,9 @@ func New(cfg *config.Config) (*App, error) {
}
func (a *App) Close() {
if a.usageRetention != nil {
a.usageRetention()
}
a.Usage.Close()
}