v0.4.1:新增 CK 有效性检查并修复抢购调度
This commit is contained in:
+163
-42
@@ -464,9 +464,9 @@ func (r *Runner) RunTargets(ctx context.Context, accounts []Account, targets []T
|
||||
r.emitSummary(state, len(accounts), len(targets), state.maxRound(), time.Since(startedAt))
|
||||
}()
|
||||
|
||||
runWave := func(loop int) {
|
||||
runWave := func(loop int, waveAccounts []Account) {
|
||||
var wg sync.WaitGroup
|
||||
for _, account := range accounts {
|
||||
for _, account := range waveAccounts {
|
||||
account := account
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@@ -484,12 +484,23 @@ func (r *Runner) RunTargets(ctx context.Context, accounts []Account, targets []T
|
||||
// 2026-10-16 pc-1 实测 1421 次请求里 82% 被限流挡回。
|
||||
if state.needsPrefire() {
|
||||
state.markPrefire()
|
||||
runWave(1)
|
||||
// 预打只需要探活,不需要把所有账号都打一遍:账号多且网络慢时,
|
||||
// 全账号预打可能一直打到开抢之后,反而阻塞正式窗口。
|
||||
probeAccounts := accounts
|
||||
if limit := prefireProbeCount(cfg.Concurrency, len(probeAccounts)); limit < len(probeAccounts) {
|
||||
probeAccounts = probeAccounts[:limit]
|
||||
}
|
||||
runWave(1, probeAccounts)
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
if !state.windowExpired() {
|
||||
if err := sleepContext(ctx, time.Until(state.windowStart())); err != nil {
|
||||
}
|
||||
|
||||
// 无论是否预打,正式请求都不能早于「开抢 − StartAhead」。
|
||||
// 非预打分支过去直接进入循环,固定提前量在 250~310ms 时会产生窗口外请求。
|
||||
if !state.windowed() || !state.windowExpired() {
|
||||
if wait := time.Until(state.windowStart()); wait > 0 {
|
||||
if err := sleepContext(ctx, wait); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -525,7 +536,7 @@ func (r *Runner) Prewarm(ctx context.Context, accounts []Account, cfg Config, em
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
sem := make(chan struct{}, cfg.Concurrency)
|
||||
sem := make(chan struct{}, prewarmConcurrency(cfg.Concurrency))
|
||||
var wg sync.WaitGroup
|
||||
for _, account := range accounts {
|
||||
account := account
|
||||
@@ -583,7 +594,8 @@ func (r *Runner) runAccountLoop(ctx context.Context, client huya.ActivityClient,
|
||||
}
|
||||
state.noteRound(round)
|
||||
r.runAccountRound(ctx, client, state, account, targets, cfg, round, emit, cancel)
|
||||
if ctx.Err() != nil || state.allDone() || state.windowExpired() {
|
||||
if ctx.Err() != nil || state.allDone() || state.windowExpired() ||
|
||||
state.accountFinished(account.UID, len(targets)) {
|
||||
return
|
||||
}
|
||||
// 本账号自己的节奏:固定等 cfg.Delay。退避中的目标会在下一轮被 activeTargets 跳过,
|
||||
@@ -624,7 +636,7 @@ func (r *Runner) runAccountRound(ctx context.Context, client huya.ActivityClient
|
||||
emit(result)
|
||||
}
|
||||
r.refreshAfterSuccess(state, client, account, target, index, loop, cfg, result, emit)
|
||||
if ctx.Err() != nil {
|
||||
if ctx.Err() != nil || state.accountFinished(account.UID, len(targets)) {
|
||||
return
|
||||
}
|
||||
continue
|
||||
@@ -638,6 +650,10 @@ func (r *Runner) runAccountRound(ctx context.Context, client huya.ActivityClient
|
||||
// SharePrizeList 打开时,多账号复用同一份列表快照(带 TTL 的单飞缓存),
|
||||
// 避免每个账号每一轮都各查一次列表。
|
||||
list := r.listForAccount(ctx, client, account, cfg, state)
|
||||
if ctx.Err() != nil || state.windowExpired() {
|
||||
// 停止任务或窗口结束时,不要为每个目标补一条 list_error。
|
||||
return
|
||||
}
|
||||
if list.err != nil || list.resp == nil {
|
||||
errText := "奖品列表响应为空"
|
||||
if list.err != nil {
|
||||
@@ -672,6 +688,7 @@ func (r *Runner) runAccountRound(ctx context.Context, client huya.ActivityClient
|
||||
prize := matchPrize(prizes, target)
|
||||
if prize == nil {
|
||||
state.recordOutcome(CategoryNotFound)
|
||||
retryAt := state.scheduleRetry(account.UID, index, cfg, CategoryNotFound)
|
||||
result := Result{
|
||||
Loop: loop,
|
||||
UID: account.UID,
|
||||
@@ -680,6 +697,7 @@ func (r *Runner) runAccountRound(ctx context.Context, client huya.ActivityClient
|
||||
Action: "not_found",
|
||||
Category: CategoryNotFound,
|
||||
Message: notFoundMessage(cfg.Category),
|
||||
RetryAfter: retryAt,
|
||||
}
|
||||
if emit != nil {
|
||||
emit(result)
|
||||
@@ -703,7 +721,7 @@ func (r *Runner) runAccountRound(ctx context.Context, client huya.ActivityClient
|
||||
emit(result)
|
||||
}
|
||||
r.refreshAfterSuccess(state, client, account, target, index, loop, cfg, result, emit)
|
||||
if ctx.Err() != nil {
|
||||
if ctx.Err() != nil || state.accountFinished(account.UID, len(targets)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -765,6 +783,14 @@ func (r *Runner) exchangeTarget(ctx context.Context, client huya.ActivityClient,
|
||||
result.Error = ctx.Err().Error()
|
||||
return result
|
||||
}
|
||||
// 排队等额度期间窗口可能已经结束,不能把请求补发到窗口外。
|
||||
if state.windowExpired() {
|
||||
state.release()
|
||||
result.Action = actionExchangeCanceled
|
||||
result.Category = CategoryError
|
||||
result.Message = "窗口已结束,未发起兑换"
|
||||
return result
|
||||
}
|
||||
started := time.Now()
|
||||
exchange, err := client.ScoreExchangePrize(ctx, account.UID, account.Cookie, sid, pid)
|
||||
state.release()
|
||||
@@ -784,15 +810,17 @@ func (r *Runner) exchangeTarget(ctx context.Context, client huya.ActivityClient,
|
||||
|
||||
func (r *Runner) applyExchangeDecision(state *runState, account Account, index int, cfg Config, result Result, cancel context.CancelFunc) Result {
|
||||
state.recordOutcome(result.Category)
|
||||
state.observeThrottle(result.Category)
|
||||
state.observeThrottle(account.UID, result.Category)
|
||||
if result.Category == CategorySuccess {
|
||||
state.markSuccessUID(account.UID)
|
||||
}
|
||||
switch result.Category {
|
||||
case CategorySuccess:
|
||||
if cfg.StopAccountAfterSuccess {
|
||||
state.markDone(account.UID, index)
|
||||
state.markAccountDone(account.UID)
|
||||
result.Done = true
|
||||
} else {
|
||||
state.markDone(account.UID, index)
|
||||
}
|
||||
if cfg.StopAfterSuccess {
|
||||
cancel()
|
||||
@@ -802,27 +830,32 @@ func (r *Runner) applyExchangeDecision(state *runState, account Account, index i
|
||||
state.markDone(account.UID, index)
|
||||
result.Done = true
|
||||
}
|
||||
// 未开启「积分不足即停」时也要退避,避免每轮都重复撞同一个终态。
|
||||
if !result.Done {
|
||||
result.RetryAfter = state.scheduleRetry(account.UID, index, cfg, result.Category)
|
||||
}
|
||||
case CategoryOffShelf:
|
||||
// 已下架 / 奖品不存在:本窗口内不会再出现,直接终止该 target。
|
||||
state.markTargetDone(index)
|
||||
result.Done = true
|
||||
case CategorySessionLost:
|
||||
// 会话失效实测是瞬时的(下一轮即可成功),所以先照常重试;
|
||||
// 只有连续多次才停止该账号,避免真的失效后白打一整个窗口的请求。
|
||||
// 只有连续多次才停止整个账号,避免无效 CK 把其他目标也打满整个窗口。
|
||||
streak := state.bumpSessionStreak(account.UID, index)
|
||||
if limit := sessionLostLimit(cfg); streak >= limit {
|
||||
state.markDone(account.UID, index)
|
||||
state.markAccountDone(account.UID)
|
||||
result.Done = true
|
||||
result.Message = fmt.Sprintf("会话连续失效 %d 次,停止该账号", streak)
|
||||
} else {
|
||||
result.RetryAfter = state.scheduleRetry(account.UID, index, cfg, result.Category)
|
||||
}
|
||||
case CategorySoldOut:
|
||||
if cfg.StopOnSoldOut {
|
||||
case CategorySoldOut, CategoryNotOpen:
|
||||
if result.Category == CategorySoldOut && cfg.StopOnSoldOut {
|
||||
state.markTargetDone(index)
|
||||
result.Done = true
|
||||
} else {
|
||||
// 别人锁单未支付或取消会让库存回流,窗口内继续探测(只是放慢节奏)。
|
||||
// 售罄可能回流;未开放通常只是还没到点。两者都继续等待,但必须退避,
|
||||
// 否则窗口内会以配置的 Delay 持续轰同一个接口。
|
||||
result.RetryAfter = state.scheduleRetry(account.UID, index, cfg, result.Category)
|
||||
}
|
||||
case CategoryTooFrequent:
|
||||
@@ -869,8 +902,13 @@ func (s *runState) scheduleRetry(uid int64, index int, cfg Config, category stri
|
||||
// 6. 任何一次等待都不超过 maxRetryDelay——超过 1.5 秒就可能正好错过回流库存。
|
||||
func (s *runState) retryDelay(uid int64, index int, cfg Config, category string) time.Duration {
|
||||
base := cfg.ErrorBackoff
|
||||
if category == CategoryTooFrequent {
|
||||
switch category {
|
||||
case CategoryTooFrequent:
|
||||
base = cfg.TooFrequentBackoff
|
||||
case CategoryNotOpen:
|
||||
// 开抢边沿的首个请求可能只早了十几毫秒,不能用 300ms 直接错过先手;
|
||||
// 先按账号节奏快速重试,连续未开放再逐级放大。
|
||||
base = maxDuration(cfg.Delay, 50*time.Millisecond)
|
||||
}
|
||||
if base <= 0 {
|
||||
base = defaultRetryBackoff
|
||||
@@ -886,11 +924,26 @@ func (s *runState) retryDelay(uid int64, index int, cfg Config, category string)
|
||||
if factor := s.throttleFactor(); factor > 1 {
|
||||
delay = time.Duration(float64(delay) * factor)
|
||||
}
|
||||
return jitterDuration(delay)
|
||||
// 全局降速倍率和抖动必须在最后再裁剪,否则 1.5s 的上限会被放大到 6~7s,
|
||||
// 正好错过库存回流窗口。
|
||||
delay = jitterDuration(delay)
|
||||
if delay > maxRetryDelay {
|
||||
delay = maxRetryDelay
|
||||
}
|
||||
return delay
|
||||
}
|
||||
|
||||
// elapsed 返回任务开始至今的时长,用于按阶段取退避节奏。
|
||||
// elapsed 返回相对开抢时刻的已用时,用于按阶段取退避节奏。
|
||||
// 定时模式可能在开抢前数秒就启动(预打/等待),如果按 startedAt 计算,
|
||||
// 窗口一开始就已经进入「后段降速」,会白白牺牲开抢头几秒。
|
||||
func (s *runState) elapsed() time.Duration {
|
||||
if !s.openAt.IsZero() {
|
||||
elapsed := time.Since(s.openAt)
|
||||
if elapsed < 0 {
|
||||
return 0
|
||||
}
|
||||
return elapsed
|
||||
}
|
||||
if s.startedAt.IsZero() {
|
||||
return 0
|
||||
}
|
||||
@@ -919,7 +972,7 @@ func stageMultiplier(elapsed time.Duration, category string) int {
|
||||
// escalatingCategory 返回该分类是否参与「连续失败翻倍」的统计。
|
||||
func escalatingCategory(category string) bool {
|
||||
switch category {
|
||||
case CategoryFailed, CategoryTooFrequent, CategoryError, CategoryProtocol, CategorySessionLost, CategoryCondition:
|
||||
case CategoryFailed, CategoryTooFrequent, CategoryError, CategoryProtocol, CategorySessionLost, CategoryCondition, CategoryNotOpen:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
@@ -973,36 +1026,48 @@ func (s *runState) bumpSessionStreak(uid int64, index int) int {
|
||||
return streak
|
||||
}
|
||||
|
||||
// observeThrottle 把结局交给全局降速器。会话失效等其它结局会重置「无进展」计数。
|
||||
func (s *runState) observeThrottle(category string) {
|
||||
if s.governor == nil {
|
||||
return
|
||||
// observeThrottle 把结局交给全局降速器。只有明确拿到业务响应时才清空当前账号的
|
||||
// 会话失效连续计数;限流与网络错误不能证明会话有效。
|
||||
func (s *runState) observeThrottle(uid int64, category string) {
|
||||
if sessionAliveCategory(category) {
|
||||
s.clearSessionStreak(uid)
|
||||
}
|
||||
if category != CategoryTooFrequent {
|
||||
s.clearSessionStreak(category)
|
||||
if s.governor != nil {
|
||||
s.governor.observe(category)
|
||||
}
|
||||
s.governor.observe(category)
|
||||
}
|
||||
|
||||
// clearSessionStreak 在拿到任何非会话失效结局后清空连续失效计数。
|
||||
func (s *runState) clearSessionStreak(category string) {
|
||||
if category == CategorySessionLost {
|
||||
return
|
||||
// sessionAliveCategory 返回该结局是否能证明当前账号的登录态仍然有效。
|
||||
// 网络错误、协议异常和限流都不算:它们没有拿到可信的业务响应。
|
||||
func sessionAliveCategory(category string) bool {
|
||||
switch category {
|
||||
case CategorySuccess, CategoryNotOpen, CategorySoldOut, CategoryNotEnough,
|
||||
CategoryAlreadyDone, CategoryOffShelf, CategoryCondition, CategoryFailed:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// clearSessionStreak 在某个账号拿到非会话失效结局后,清空该账号的连续失效计数。
|
||||
//
|
||||
// 必须按账号清理,不能全局清理:多账号同时抢购时,其他账号的成功会不断把
|
||||
// 一个无效 CK 的 session_lost 计数归零,导致它永远达不到停止阈值。
|
||||
// 同理,这段逻辑不能依赖自适应降速器是否开启。
|
||||
func (s *runState) clearSessionStreak(uid int64) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for uid, items := range s.accounts {
|
||||
changed := false
|
||||
for index := range items {
|
||||
if items[index].SessionStreak != 0 {
|
||||
items[index].SessionStreak = 0
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
s.accounts[uid] = items
|
||||
items := s.accounts[uid]
|
||||
changed := false
|
||||
for index := range items {
|
||||
if items[index].SessionStreak != 0 {
|
||||
items[index].SessionStreak = 0
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
s.accounts[uid] = items
|
||||
}
|
||||
}
|
||||
|
||||
// throttleFactor 返回当前的自适应降速倍率(未启用时恒为 1)。
|
||||
@@ -1170,6 +1235,10 @@ func (r *Runner) listForAccount(ctx context.Context, client huya.ActivityClient,
|
||||
if !state.acquire(ctx) {
|
||||
return &listSnapshot{err: ctx.Err()}
|
||||
}
|
||||
if state.windowExpired() {
|
||||
state.release()
|
||||
return &listSnapshot{err: errors.New("窗口已结束")}
|
||||
}
|
||||
started := time.Now()
|
||||
resp, err := client.GetActPrizeList(ctx, account.UID, account.Cookie, cfg.SID)
|
||||
state.release()
|
||||
@@ -1189,6 +1258,37 @@ func (r *Runner) client() huya.ActivityClient {
|
||||
return huya.NewHTTPClient(nil)
|
||||
}
|
||||
|
||||
// prefireProbeCount 限制开抢前探活账号数:探活只需要少量样本,
|
||||
// 即使抢购并发设到 100,也不能在开抢前齐射一百个请求。
|
||||
func prefireProbeCount(configured, total int) int {
|
||||
if total <= 0 {
|
||||
return 0
|
||||
}
|
||||
if configured <= 0 {
|
||||
configured = 1
|
||||
}
|
||||
if configured > 8 {
|
||||
configured = 8
|
||||
}
|
||||
if configured > total {
|
||||
return total
|
||||
}
|
||||
return configured
|
||||
}
|
||||
|
||||
// prewarmConcurrency 给建连预热单独限流:抢购请求仍然严格按 Concurrency,
|
||||
// 但预热必须在开抢前 2 秒内完成。沿用 5 个并发时,几十上百个账号很容易
|
||||
// 预热到开抢之后才返回,反而错过整个窗口。
|
||||
func prewarmConcurrency(configured int) int {
|
||||
if configured < 8 {
|
||||
configured = 8
|
||||
}
|
||||
if configured > 16 {
|
||||
configured = 16
|
||||
}
|
||||
return configured
|
||||
}
|
||||
|
||||
func newRunState(accounts []Account, targetCount, concurrency int, cfg Config) *runState {
|
||||
if concurrency <= 0 {
|
||||
concurrency = 1
|
||||
@@ -1658,6 +1758,20 @@ func (s *runState) markDone(uid int64, index int) {
|
||||
}
|
||||
}
|
||||
|
||||
// markAccountDone 一次性结束该账号的所有目标。
|
||||
// 「账号成功停」和会话连续失效都是账号级终态;只停当前 target 会让同一账号
|
||||
// 继续打其他奖品,既违背开关语义,也可能造成重复扣分。
|
||||
func (s *runState) markAccountDone(uid int64) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
items := s.accounts[uid]
|
||||
for index := range items {
|
||||
items[index].Done = true
|
||||
items[index].RetryAfter = time.Time{}
|
||||
}
|
||||
s.accounts[uid] = items
|
||||
}
|
||||
|
||||
func (s *runState) markTargetDone(index int) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
@@ -1763,7 +1877,14 @@ func normalizeTargets(targets []Target) []Target {
|
||||
}
|
||||
|
||||
func shouldDirectExchange(target Target, cfg Config) bool {
|
||||
return cfg.DirectExchange && !cfg.ListBeforeExchange && target.PID > 0 && !target.Auto
|
||||
// 设置了价格/库存门槛时必须先查列表,否则直兑会绕过过滤条件,
|
||||
// 商品临时涨价或库存不足时仍会直接下单。
|
||||
return cfg.DirectExchange &&
|
||||
!cfg.ListBeforeExchange &&
|
||||
target.PID > 0 &&
|
||||
!target.Auto &&
|
||||
target.MaxPrice <= 0 &&
|
||||
target.MinLeft <= 0
|
||||
}
|
||||
|
||||
// notFoundMessage 在未命中时把类目限制写进提示,便于区分「被类目过滤掉」与「列表里确实没有」。
|
||||
|
||||
Reference in New Issue
Block a user