Files
dygou_go/internal/seckill/warmup.go
T
yml2213 5da35ab65e 抢购后自动核对积分余额(基线在导入账号时获取),发布0.3.5
兑换成功会扣积分,所以"余额确实少了多少"是一个独立于接口返回的确认证据——
2026-09-30 出现过订单实际已支付、程序却报成失败的情况,只看接口返回不够。

基线的获取时机刻意放在抢购链路之外:轮次/预热期间任何额外请求都会挤占锁单,
账号越多越明显。基线只在三个时机更新:
- 导入账号后自动查一次(后台执行,不阻塞导入返回)
- 用户手动点"查询积分"时
- 每次抢购核对之后把最新余额存回,下一次抢购的基线就是上一次的终值,差值才准
账号被编辑(改名/换 Cookie)时清掉旧基线,删除账号时一并清理。

- 新增 ScoreBaselineStore(scores.json,与账号库同目录,原子写盘):
  记录每个账号上次已知的积分余额与时间;账号库路径为空时退化为纯内存,不写工作目录
- 核对输出示例:
  "积分已扣减,兑换确认: 账号=a 抢购前=1000 抢购后=800 扣减=200"
  并把"|积分-200"附到结果集,账号表里也能直接看到确认信息
- 没有基线时只记录余额并提示"下次抢购即可比对",不会误报"未扣分";
  积分接口失败只记警告,不改变"抢到"这个结论
- 抢购进行中调用导入不会去查询积分(避免与锁单抢连接),提示稍后手动查询
- 预热日志措辞修正:"开始预热: N 个请求" → "N 个账号"(每账号实际不止一个请求)
- 新增测试:TestVerifyRunResultsConfirmsScoreDeduction、TestVerifyRunResultsWithoutBaseline、
  TestVerifyRunResultsIgnoresScoreFailure、TestScoreBaselineStorePersists、
  TestFetchScoreBaselinesRecordsValues
2026-10-03 19:57:13 +08:00

103 lines
2.9 KiB
Go

package seckill
import (
"fmt"
"sync"
"time"
"douyu_go/internal/logger"
"douyu_go/internal/types"
"douyu_go/internal/utils"
)
func warmupKey(attempt types.ExchangeAttempt) string {
return fmt.Sprintf("%s|%s", attempt.Account.Name, attempt.Product.CommodityID)
}
func (s *Scheduler) setWarmup(key string, data types.WarmupData) {
s.warmupsMu.Lock()
defer s.warmupsMu.Unlock()
if s.warmups == nil {
s.warmups = make(map[string]types.WarmupData)
}
s.warmups[key] = data
}
func (s *Scheduler) warmupFor(attempt types.ExchangeAttempt) (types.WarmupData, bool) {
s.warmupsMu.RLock()
data, ok := s.warmups[warmupKey(attempt)]
s.warmupsMu.RUnlock()
if !ok || data.CreatedAt.IsZero() || time.Since(data.CreatedAt) > warmupMaxAge {
return types.WarmupData{}, false
}
return data, true
}
func (s *Scheduler) clearWarmups() {
s.warmupsMu.Lock()
defer s.warmupsMu.Unlock()
s.warmups = make(map[string]types.WarmupData)
}
func (s *Scheduler) warmupAttempts(attempts []types.ExchangeAttempt) {
s.warmupRunMu.Lock()
defer s.warmupRunMu.Unlock()
attempts = uniqueWarmupAttempts(attempts)
if len(attempts) == 0 {
return
}
s.clearWarmups()
logger.LogEvent("预热", "info", "开始预热: %d 个账号", len(attempts))
settings := s.settingsSnapshot()
cli := s.clientSnapshot()
limit := make(chan struct{}, settings.concurrency)
var wg sync.WaitGroup
for _, attempt := range attempts {
wg.Add(1)
go func(attempt types.ExchangeAttempt) {
defer wg.Done()
limit <- struct{}{}
defer func() { <-limit }()
start := time.Now()
csrf, err := cli.GenerateCSRF(attempt.Account.Cookie)
if err != nil {
logger.LogAccountEvent("预热", "warn", attempt.Account.Name, "鉴权token预热失败: 账号=%s 商品=%s err=%v", attempt.Account.Name, attempt.Product.CommodityName, err)
return
}
roleName, err := cli.RoleName(attempt.Account.Cookie)
if err != nil {
logger.LogAccountEvent("预热", "warn", attempt.Account.Name, "角色预热失败: 账号=%s err=%v", attempt.Account.Name, err)
}
s.setWarmup(warmupKey(attempt), types.WarmupData{
CSRF: csrf,
RoleName: roleName,
Qualified: true,
CreatedAt: time.Now(),
})
logger.LogAccountEvent("预热", "success", attempt.Account.Name, "预热完成: 账号=%s 商品=%s 耗时=%s", attempt.Account.Name, attempt.Product.CommodityName, utils.RoundDuration(time.Since(start)))
}(attempt)
}
wg.Wait()
logger.LogEvent("预热", "info", "预热任务结束")
}
func uniqueWarmupAttempts(attempts []types.ExchangeAttempt) []types.ExchangeAttempt {
if len(attempts) <= 1 {
return attempts
}
seen := make(map[string]struct{}, len(attempts))
out := make([]types.ExchangeAttempt, 0, len(attempts))
for _, attempt := range attempts {
key := warmupKey(attempt)
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
out = append(out, attempt)
}
return out
}