后端在"导入账号/手动查询/抢购后核对"时都会写入积分基线,但界面一直没有读它: 积分列的 scoreMap 只由"查询积分"按钮填充,所以自动核对完余额后主界面不动, 用户必须再点一次"查询积分"才看到新余额。 - 新增 GetScoreBaselines 绑定:一次性返回各账号最近一次已记录的积分余额 (Scheduler.ScoreBaselines → ScoreBaselineStore.Snapshot),界面直接读取, 不必再为每个账号单独发请求 - 前端 useAccounts 新增 syncScoresFromBackend:把后端基线写入 scoreMap 与 localStorage(与手动查询同一份状态);导入场景下会短暂轮询等待后端异步查询写完 - 抢购状态回到 idle 时自动同步一次(对应抢购结束后的核对结果) - 导入账号成功后按导入的账号名同步一次 - 新增断言:核对后 ScoreBaselines 返回最新余额
176 lines
4.6 KiB
Go
176 lines
4.6 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)
|
|
}
|
|
|
|
// Snapshot 返回全部账号的最新积分余额副本。
|
|
func (s *ScoreBaselineStore) Snapshot() map[string]int {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
out := make(map[string]int, len(s.entries))
|
|
for name, entry := range s.entries {
|
|
out[name] = entry.Score
|
|
}
|
|
return out
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|