兑换成功会扣积分,所以"余额确实少了多少"是一个独立于接口返回的确认证据—— 2026-09-30 出现过订单实际已支付、程序却报成失败的情况,只看接口返回不够。 基线的获取时机刻意放在抢购链路之外:轮次/预热期间任何额外请求都会挤占锁单, 账号越多越明显。基线只在三个时机更新: - 导入账号后自动查一次(后台执行,不阻塞导入返回) - 用户手动点"查询积分"时 - 每次抢购核对之后把最新余额存回,下一次抢购的基线就是上一次的终值,差值才准 账号被编辑(改名/换 Cookie)时清掉旧基线,删除账号时一并清理。 - 新增 ScoreBaselineStore(scores.json,与账号库同目录,原子写盘): 记录每个账号上次已知的积分余额与时间;账号库路径为空时退化为纯内存,不写工作目录 - 核对输出示例: "积分已扣减,兑换确认: 账号=a 抢购前=1000 抢购后=800 扣减=200" 并把"|积分-200"附到结果集,账号表里也能直接看到确认信息 - 没有基线时只记录余额并提示"下次抢购即可比对",不会误报"未扣分"; 积分接口失败只记警告,不改变"抢到"这个结论 - 抢购进行中调用导入不会去查询积分(避免与锁单抢连接),提示稍后手动查询 - 预热日志措辞修正:"开始预热: N 个请求" → "N 个账号"(每账号实际不止一个请求) - 新增测试:TestVerifyRunResultsConfirmsScoreDeduction、TestVerifyRunResultsWithoutBaseline、 TestVerifyRunResultsIgnoresScoreFailure、TestScoreBaselineStorePersists、 TestFetchScoreBaselinesRecordsValues
162 lines
4.3 KiB
Go
162 lines
4.3 KiB
Go
package seckill
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
|
|
"douyu_go/internal/logger"
|
|
)
|
|
|
|
// scoreBaselineFileName 是积分基线文件名,和账号库放在同一个数据目录。
|
|
const scoreBaselineFileName = "scores.json"
|
|
|
|
// scoreBaselineEntry 是单个账号上一次已知的积分余额。
|
|
type scoreBaselineEntry struct {
|
|
Score int `json:"score"`
|
|
At int64 `json:"at"` // Unix 秒,便于判断基线有多旧
|
|
}
|
|
|
|
// ScoreBaselineStore 记录每个账号"上次已知的积分余额",作为抢购结束后核对的基线。
|
|
//
|
|
// 刻意不在抢购链路里取基线:账号多的时候,预热或触发时刻的任何额外请求都会挤占抢购。
|
|
// 基线只在三个时机更新——导入账号时自动查一次、用户手动查积分时、以及每次抢购核对之后
|
|
// (把最新的余额存回去,下一次抢购的基线就是上一次的终值,差值才准)。
|
|
type ScoreBaselineStore struct {
|
|
mu sync.RWMutex
|
|
path string
|
|
entries map[string]scoreBaselineEntry
|
|
}
|
|
|
|
// NewScoreBaselineStore 创建积分基线存储;path 为空时退化为纯内存。
|
|
func NewScoreBaselineStore(path string) *ScoreBaselineStore {
|
|
return &ScoreBaselineStore{
|
|
path: path,
|
|
entries: make(map[string]scoreBaselineEntry),
|
|
}
|
|
}
|
|
|
|
// Load 从磁盘读取基线。文件缺失或损坏都不算错误,只是没有基线可用。
|
|
func (s *ScoreBaselineStore) Load() {
|
|
if s == nil || s.path == "" {
|
|
return
|
|
}
|
|
data, err := os.ReadFile(s.path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
loaded := make(map[string]scoreBaselineEntry)
|
|
if err := json.Unmarshal(data, &loaded); err != nil {
|
|
logger.LogEvent("核对", "warn", "积分基线文件解析失败,将重新累积: %v", err)
|
|
return
|
|
}
|
|
s.mu.Lock()
|
|
s.entries = loaded
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
// Get 返回账号上一次已知的积分余额。
|
|
func (s *ScoreBaselineStore) Get(accountName string) (int, bool, time.Time) {
|
|
if s == nil {
|
|
return 0, false, time.Time{}
|
|
}
|
|
s.mu.RLock()
|
|
entry, ok := s.entries[accountName]
|
|
s.mu.RUnlock()
|
|
if !ok {
|
|
return 0, false, time.Time{}
|
|
}
|
|
return entry.Score, true, time.Unix(entry.At, 0)
|
|
}
|
|
|
|
// Set 记录一个账号的最新积分余额,并落盘。
|
|
func (s *ScoreBaselineStore) Set(accountName string, score int) {
|
|
s.SetMany(map[string]int{accountName: score})
|
|
}
|
|
|
|
// SetMany 批量记录并落盘(一次写盘,避免每个账号都写一次文件)。
|
|
func (s *ScoreBaselineStore) SetMany(values map[string]int) {
|
|
if s == nil || len(values) == 0 {
|
|
return
|
|
}
|
|
now := time.Now().Unix()
|
|
s.mu.Lock()
|
|
for name, score := range values {
|
|
s.entries[name] = scoreBaselineEntry{Score: score, At: now}
|
|
}
|
|
snapshot := make(map[string]scoreBaselineEntry, len(s.entries))
|
|
for name, entry := range s.entries {
|
|
snapshot[name] = entry
|
|
}
|
|
s.mu.Unlock()
|
|
s.flush(snapshot)
|
|
}
|
|
|
|
// Forget 删除已不存在账号的基线。
|
|
func (s *ScoreBaselineStore) Forget(accountNames []string) {
|
|
if s == nil || len(accountNames) == 0 {
|
|
return
|
|
}
|
|
s.mu.Lock()
|
|
for _, name := range accountNames {
|
|
delete(s.entries, name)
|
|
}
|
|
snapshot := make(map[string]scoreBaselineEntry, len(s.entries))
|
|
for name, entry := range s.entries {
|
|
snapshot[name] = entry
|
|
}
|
|
s.mu.Unlock()
|
|
s.flush(snapshot)
|
|
}
|
|
|
|
// Names 返回已记录基线的账号名(按字典序),便于测试与排查。
|
|
func (s *ScoreBaselineStore) Names() []string {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
s.mu.RLock()
|
|
out := make([]string, 0, len(s.entries))
|
|
for name := range s.entries {
|
|
out = append(out, name)
|
|
}
|
|
s.mu.RUnlock()
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
// flush 原子写盘:先写临时文件再改名,避免半写入损坏。
|
|
func (s *ScoreBaselineStore) flush(snapshot map[string]scoreBaselineEntry) {
|
|
if s == nil || s.path == "" {
|
|
return
|
|
}
|
|
data, err := json.MarshalIndent(snapshot, "", " ")
|
|
if err != nil {
|
|
return
|
|
}
|
|
dir := filepath.Dir(s.path)
|
|
if dir != "" && dir != "." {
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
return
|
|
}
|
|
}
|
|
tmp, err := os.CreateTemp(dir, "."+filepath.Base(s.path)+".*.tmp")
|
|
if err != nil {
|
|
return
|
|
}
|
|
tmpPath := tmp.Name()
|
|
defer func() { _ = os.Remove(tmpPath) }()
|
|
if _, err := tmp.Write(data); err != nil {
|
|
_ = tmp.Close()
|
|
return
|
|
}
|
|
if err := tmp.Close(); err != nil {
|
|
return
|
|
}
|
|
if err := os.Rename(tmpPath, s.path); err != nil {
|
|
logger.LogEvent("核对", "warn", "积分基线写入失败: %v", err)
|
|
}
|
|
}
|