diff --git a/docs/代理池与白名单.md b/docs/代理池与白名单.md index 120c4f6..088b0ac 100644 --- a/docs/代理池与白名单.md +++ b/docs/代理池与白名单.md @@ -172,7 +172,9 @@ 品赞启用自动白名单时: -1. 先尝试提取代理。 +白名单请求由每台抢购电脑上的本地实例直接发往品赞,控制中心不参与代理提取或白名单请求。 + +1. 本地实例先尝试提取代理。 2. 平台提示当前 IP 不在白名单。 3. 从平台错误中读取实际公网 IP。 4. 同一台电脑的多个实例通过跨进程文件锁串行处理该 IP。 @@ -200,6 +202,8 @@ 代理提取重试间隔已调整为 1.2 秒。定时任务会在 `T-120s` 开始准备,并持续重试到 `T-15s`,为限频恢复留出窗口。 +`whiteList-verify` 返回的业务错误也会保留原始消息并执行同样的致命/可重试分类,不再把所有非零 `code` 都判定为致命错误。 + ## 8. 账号分组 账号按名称排序后顺序分组,保证相同账号集合尽量得到稳定分组。 @@ -355,9 +359,14 @@ T-0s 本地定时触发 5. 非严格模式且无备用代理时,整组切换到直连。 6. 补提取成功后会优先重新接回仍在等待的失败组。 7. 切换后继续补提取备用代理。 +8. 运行期间每 20 秒探测一次备用代理,连续 2 次失败后移出备用池并补提取。 +9. 严格模式失败组等待备用期间,该组账号暂停发请求,不继续消耗故障代理和抢购次数。 +10. 补提取失败后按 2 秒节奏继续重试,直到任务停止、代理恢复或代理池失效。 非严格模式已经主动切换到直连的组,不会在运行中自动切回代理;严格模式保留失败组状态,因此可以在新备用到达后重新绑定。 +运行前门禁会检查代理池是否过期、是否被配置更新、是否存在失败组,以及是否覆盖本次选择的全部账号。备用数量不足不会导致已覆盖账号的活动代理被重建。 + ## 13. 代理有效期 当前系统支持: @@ -645,3 +654,6 @@ cd frontend && npm run type-check | 2026-11-21 | 备用代理默认和最低比例调整为 50% | 已实现 | | 2026-11-21 | 50% 备用是否足以覆盖集中故障 | 待评审 | | 2026-11-21 | 主界面必须直接显示实例内各代理组和账号归属,不能依赖日志分析 | 已实现 | +| 2026-11-21 | 备用不足不得导致活动代理池重复提取 | 已实现 | +| 2026-11-21 | 严格模式失败组暂停请求并持续补提取,不空打故障代理 | 已实现 | +| 2026-11-21 | 运行期持续探测备用代理,失败节点移出备用池 | 已实现 | diff --git a/internal/app/app_seckill.go b/internal/app/app_seckill.go index 676664e..43eec40 100644 --- a/internal/app/app_seckill.go +++ b/internal/app/app_seckill.go @@ -28,7 +28,7 @@ func (a *App) StartSeckill(accountProducts map[string]string) types.MessageResul if err := a.scheduler.PrepareProxyGroups(accountProducts); err != nil { return types.MessageResult{Error: "代理准备失败: " + err.Error()} } - if ok, reason := a.scheduler.ProxyRunGate(); !ok { + if ok, reason := a.scheduler.ProxyRunGateFor(accountProducts); !ok { return types.MessageResult{Error: "严格代理模式未就绪: " + reason} } if !a.scheduler.TryStartRun() { @@ -84,7 +84,7 @@ func (a *App) startXiaodianSeckill(req types.FixedSeckillRequest, message string if err := a.scheduler.PrepareProxyGroups(req.AccountProducts); err != nil { return types.MessageResult{Error: "代理准备失败: " + err.Error()} } - if ok, reason := a.scheduler.ProxyRunGate(); !ok { + if ok, reason := a.scheduler.ProxyRunGateFor(req.AccountProducts); !ok { return types.MessageResult{Error: "严格代理模式未就绪: " + reason} } if !a.scheduler.TryStartRun() { diff --git a/internal/proxy/ipzan.go b/internal/proxy/ipzan.go index 0c675dd..10853a7 100644 --- a/internal/proxy/ipzan.go +++ b/internal/proxy/ipzan.go @@ -243,14 +243,25 @@ func (p *IPZanProvider) verifyWhitelist(ctx context.Context, httpClient *http.Cl return false, err } var payload struct { - Code *int `json:"code"` - Data json.RawMessage `json:"data"` + Code *int `json:"code"` + Message string `json:"message"` + Info string `json:"info"` + Msg string `json:"msg"` + Data json.RawMessage `json:"data"` } if err := json.Unmarshal(body, &payload); err != nil { return false, fmt.Errorf("解析品赞白名单校验响应失败: %w", err) } - if payload.Code == nil || *payload.Code != 0 { - return false, &ProviderError{Provider: "品赞", Message: "白名单校验失败", Fatal: true} + if payload.Code == nil { + return false, fmt.Errorf("品赞白名单校验响应缺少 code: %s", truncateText(string(body), 120)) + } + if *payload.Code != 0 { + message := firstNonEmpty(payload.Message, payload.Info, payload.Msg, "品赞返回未知错误") + return false, &ProviderError{ + Provider: "品赞", + Message: "校验白名单失败: " + message, + Fatal: isIPZanFatalMessage(message), + } } var exists bool if json.Unmarshal(payload.Data, &exists) == nil { diff --git a/internal/proxy/service_test.go b/internal/proxy/service_test.go index 399e414..fe77034 100644 --- a/internal/proxy/service_test.go +++ b/internal/proxy/service_test.go @@ -310,3 +310,30 @@ func TestIPZanEnsureWhitelistSignsAndAdds(t *testing.T) { t.Fatal("whiteList-add was not called") } } + +func TestIPZanWhitelistVerifyRateLimitIsRetryable(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/whiteList-verify" { + t.Fatalf("unexpected path %s", r.URL.Path) + } + io.WriteString(w, `{"code":-1,"data":null,"status":200,"message":"白名单仅支持1秒访一次"}`) + })) + defer server.Close() + + provider := &IPZanProvider{baseURL: server.URL, now: time.Now} + _, err := provider.verifyWhitelist( + context.Background(), + server.Client(), + "plan-no", + "1.2.3.4", + ) + if err == nil { + t.Fatal("expected whitelist verify error") + } + if IsFatal(err) { + t.Fatalf("rate limit should be retryable: %v", err) + } + if !strings.Contains(err.Error(), "白名单仅支持1秒访一次") { + t.Fatalf("provider message lost: %v", err) + } +} diff --git a/internal/seckill/attempt.go b/internal/seckill/attempt.go index 6e5718c..79b241f 100644 --- a/internal/seckill/attempt.go +++ b/internal/seckill/attempt.go @@ -112,12 +112,22 @@ type attemptOutcome struct { // 这不是"没抢到",而是"发早了"。它不该消耗账号的重试节奏, // 更不该触发限流降频——否则有效尝试次数会白白浪费在活动开始之前。 notStarted bool + // paused 表示账号所属代理组已故障,当前正在等待备用代理或补提取。 + paused bool } // runAccountAttempt 按活动执行一次完整尝试。 func (s *Scheduler) runAccountAttempt(attempt types.ExchangeAttempt, mode types.SeckillMode, ctl *runControl) attemptOutcome { cli := s.clientForAccount(attempt.Account.Name) if cli == nil { + if s.accountProxyPaused(attempt.Account.Name) { + return attemptOutcome{result: types.ExchangeResult{ + Account: attempt.Account.Name, + Product: attempt.Product.CommodityName, + Attempt: attempt.Index, + Message: "等待备用代理", + }, paused: true} + } return attemptOutcome{result: types.ExchangeResult{ Account: attempt.Account.Name, Product: attempt.Product.CommodityName, @@ -146,6 +156,14 @@ func (s *Scheduler) runAccountAttempt(attempt types.ExchangeAttempt, mode types. func (s *Scheduler) runPeaceLockPay(attempt types.ExchangeAttempt, ctl *runControl) attemptOutcome { cli := s.clientForAccount(attempt.Account.Name) if cli == nil { + if s.accountProxyPaused(attempt.Account.Name) { + return attemptOutcome{result: types.ExchangeResult{ + Account: attempt.Account.Name, + Product: attempt.Product.CommodityName, + Attempt: attempt.Index, + Message: "等待备用代理", + }, paused: true} + } return attemptOutcome{result: types.ExchangeResult{ Account: attempt.Account.Name, Product: attempt.Product.CommodityName, @@ -342,6 +360,14 @@ func (s *Scheduler) payOnce(cli *client.DouyuClient, attempt types.ExchangeAttem func (s *Scheduler) runDirectExchange(attempt types.ExchangeAttempt, reuseCache bool, ctl *runControl) attemptOutcome { cli := s.clientForAccount(attempt.Account.Name) if cli == nil { + if s.accountProxyPaused(attempt.Account.Name) { + return attemptOutcome{result: types.ExchangeResult{ + Account: attempt.Account.Name, + Product: attempt.Product.CommodityName, + Attempt: attempt.Index, + Message: "等待备用代理", + }, paused: true} + } return attemptOutcome{result: types.ExchangeResult{ Account: attempt.Account.Name, Product: attempt.Product.CommodityName, @@ -486,7 +512,11 @@ func (s *Scheduler) runXiaodianAttempt(attempt types.ExchangeAttempt) types.Exch return result } if cli == nil { - result.Message = "失败|没有可用代理客户端" + if s.accountProxyPaused(attempt.Account.Name) { + result.Message = "等待备用代理" + } else { + result.Message = "失败|没有可用代理客户端" + } return result } diff --git a/internal/seckill/proxy_groups.go b/internal/seckill/proxy_groups.go index 3ef395d..ee380b2 100644 --- a/internal/seckill/proxy_groups.go +++ b/internal/seckill/proxy_groups.go @@ -26,6 +26,8 @@ const ( proxyValidationTimeout = 3 * time.Second proxyValidationWorkers = 3 proxyValidateAttempts = 1 + proxyStandbyProbeEvery = 20 * time.Second + proxyStandbyFailLimit = 2 proxyImmediateTimeout = 5 * time.Second proxyScheduledTimeout = 20 * time.Second proxyRefillTimeout = 20 * time.Second @@ -56,6 +58,7 @@ type validatedProxy struct { region string source string expiresAt time.Time + failures int } type proxyRuntimeGroup struct { @@ -210,6 +213,7 @@ func (s *Scheduler) closeAccountClients() { s.proxyStandbyTarget = 0 s.proxyPoolGeneration++ s.proxyRefillInFlight = false + s.proxyRefillRetryAt = time.Time{} s.proxyFallbackActive = false s.clientMu.Unlock() closeDouyuClientList(oldClients) @@ -250,6 +254,7 @@ func (s *Scheduler) replaceAccountClients(pool preparedProxyPool) { s.proxyStandbyTarget = pool.standbyTarget s.proxyPoolGeneration++ s.proxyRefillInFlight = false + s.proxyRefillRetryAt = time.Time{} s.proxyFallbackActive = false s.clientMu.Unlock() closeDouyuClientList(oldClients) @@ -434,14 +439,16 @@ func (s *Scheduler) accountClientsCover(accountNames []string) bool { if s.accountClientsDirty { return false } - if len(s.proxyStandbys) < s.proxyStandbyTarget { - return false - } if !s.accountClientsExpireAt.IsZero() && !time.Now().Before(s.accountClientsExpireAt.Add(-30*time.Second)) { return false } for _, name := range accountNames { - if s.accountClients[name] == nil { + cli := s.accountClients[name] + if cli == nil || cli == s.directClient { + return false + } + group := s.proxyGroups[cli] + if group == nil || group.failed || group.activeURL == "" { return false } } @@ -488,6 +495,114 @@ func (s *Scheduler) allClients() []*client.DouyuClient { return out } +func (s *Scheduler) monitorProxyStandbys(stop <-chan struct{}, generation uint64) { + ticker := time.NewTicker(proxyStandbyProbeEvery) + defer ticker.Stop() + for { + select { + case <-stop: + return + case <-ticker.C: + if !s.probeProxyStandbys(generation) { + return + } + } + } +} + +func (s *Scheduler) probeProxyStandbys(generation uint64) bool { + s.clientMu.RLock() + if generation != s.proxyPoolGeneration { + s.clientMu.RUnlock() + return false + } + standbys := append([]validatedProxy(nil), s.proxyStandbys...) + s.clientMu.RUnlock() + if len(standbys) == 0 { + return true + } + + type probeResult struct { + proxy validatedProxy + rtt time.Duration + err error + } + results := make(chan probeResult, len(standbys)) + limit := make(chan struct{}, proxyValidationWorkers) + var wg sync.WaitGroup + for _, standby := range standbys { + wg.Add(1) + go func(standby validatedProxy) { + defer wg.Done() + limit <- struct{}{} + defer func() { <-limit }() + if standby.client == nil { + results <- probeResult{proxy: standby, err: errors.New("备用代理客户端为空")} + return + } + probe, err := standby.client.ProbeProxyEndpoint() + results <- probeResult{proxy: standby, rtt: probe.Latency, err: err} + }(standby) + } + wg.Wait() + close(results) + + var failedClients []*client.DouyuClient + needRefill := false + s.clientMu.Lock() + if generation != s.proxyPoolGeneration { + s.clientMu.Unlock() + return false + } + for result := range results { + index := findStandbyIndexLocked(s.proxyStandbys, result.proxy.client) + if index < 0 { + continue + } + if result.err == nil { + s.proxyStandbys[index].failures = 0 + s.proxyStandbys[index].rtt = result.rtt + continue + } + s.proxyStandbys[index].failures++ + if s.proxyStandbys[index].failures < proxyStandbyFailLimit { + continue + } + failed := s.proxyStandbys[index] + s.proxyStandbys = append(s.proxyStandbys[:index], s.proxyStandbys[index+1:]...) + s.proxyClientPool = removeClientFromPool(s.proxyClientPool, failed.client) + failedClients = append(failedClients, failed.client) + needRefill = true + logger.LogEvent("代理", "warn", "备用代理连续探测失败,已移出备用池: 出口=%s err=%v", + client.MaskProxyURL(failed.url), result.err) + } + s.clientMu.Unlock() + closeDouyuClientList(failedClients) + if needRefill && s.IsRunning() { + s.startProxyRefill() + } + return true +} + +func findStandbyIndexLocked(standbys []validatedProxy, proxyClient *client.DouyuClient) int { + for index := range standbys { + if standbys[index].client == proxyClient { + return index + } + } + return -1 +} + +func removeClientFromPool(pool []*client.DouyuClient, target *client.DouyuClient) []*client.DouyuClient { + out := pool[:0] + for _, cli := range pool { + if cli != target { + out = append(out, cli) + } + } + return out +} + // prepareProxyGroups 将本次参与抢购的账号按固定顺序分组,并为每组准备独立客户端。 // forceRefresh=true 用于定时任务启动前主动刷新短效代理。 func (s *Scheduler) prepareProxyGroups(accountProducts map[string]string, forceRefresh bool) error { @@ -978,6 +1093,7 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error if err == nil { group.consecutiveFailures = 0 group.lastError = "" + group.failed = false s.updateProxyGroupHistoryLocked(group) s.clientMu.Unlock() return @@ -1137,6 +1253,9 @@ func replaceClientInOrder(order []*client.DouyuClient, oldClient, newClient *cli } func (s *Scheduler) startProxyRefill() { + if !s.IsRunning() { + return + } cfg := s.proxySettings() if !cfg.enabled || cfg.proxyAPI == "" { return @@ -1227,11 +1346,44 @@ func (s *Scheduler) refillProxyStandbys(base *client.DouyuClient, cfg proxySetti if remainingFailed > 0 { logger.LogEvent("代理", "warn", "仍有%d个代理组等待备用代理,请检查提取 API 配额与白名单", remainingFailed) } - if standbyCount < standbyTarget && len(proxies) > 0 { - s.startProxyRefill() + if s.IsRunning() && (remainingFailed > 0 || standbyCount < standbyTarget) { + s.scheduleProxyRefillRetry(generation) } } +func (s *Scheduler) scheduleProxyRefillRetry(generation uint64) { + s.clientMu.Lock() + if generation != s.proxyPoolGeneration || s.proxyRefillInFlight { + s.clientMu.Unlock() + return + } + now := time.Now() + if now.Before(s.proxyRefillRetryAt) { + s.clientMu.Unlock() + return + } + s.proxyRefillRetryAt = now.Add(proxyRefillRetryDelay) + s.clientMu.Unlock() + + time.AfterFunc(proxyRefillRetryDelay, func() { + if !s.IsRunning() { + return + } + s.clientMu.Lock() + if generation != s.proxyPoolGeneration { + s.clientMu.Unlock() + return + } + s.proxyRefillRetryAt = time.Time{} + needStandby := s.proxyStandbyTarget - len(s.proxyStandbys) + failedGroups := countFailedProxyGroups(s.proxyGroups) + s.clientMu.Unlock() + if needStandby > 0 || failedGroups > 0 { + s.startProxyRefill() + } + }) +} + func findFailedProxyGroup(groups map[*client.DouyuClient]*proxyRuntimeGroup) (*client.DouyuClient, *proxyRuntimeGroup) { for cli, group := range groups { if group != nil && group.failed { diff --git a/internal/seckill/proxy_groups_test.go b/internal/seckill/proxy_groups_test.go index 8b2674f..6dd70b3 100644 --- a/internal/seckill/proxy_groups_test.go +++ b/internal/seckill/proxy_groups_test.go @@ -85,14 +85,26 @@ func TestAccountClientsRespectExpiryAndSettingsRevision(t *testing.T) { accountClients: make(map[string]*client.DouyuClient), } groupClient := &client.DouyuClient{} + group := &proxyRuntimeGroup{ + groupIndex: 1, + accountNames: []string{"a"}, + activeURL: "http://127.0.0.1:9000", + } scheduler.replaceAccountClients(preparedProxyPool{ accountClients: map[string]*client.DouyuClient{"a": groupClient}, activeClients: []*client.DouyuClient{groupClient}, + groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group}, expiresAt: time.Now().Add(time.Minute), + standbyTarget: 5, }) if !scheduler.accountClientsCover([]string{"a"}) { - t.Fatal("未过期代理组应可复用") + t.Fatal("备用不足不应使已覆盖账号的活动代理失效") } + group.failed = true + if scheduler.accountClientsCover([]string{"a"}) { + t.Fatal("失败代理组不应被判定为可复用") + } + group.failed = false scheduler.UpdateProxySettings(types.Config{ ProxyEnabled: true, AutoFetchProxyOnSchedule: true, @@ -264,16 +276,30 @@ func TestStrictProxyModeDoesNotSwitchDirect(t *testing.T) { url: group.activeURL, client: groupClient, }}) + scheduler.setProxyReadiness(true, "") proxyErr := errors.New("read tcp: connection reset by peer") scheduler.recordProxyResult(groupClient, proxyErr) scheduler.recordProxyResult(groupClient, proxyErr) - if scheduler.clientForAccount("a") == scheduler.directClient { - t.Fatal("strict proxy mode must not switch to direct") + if scheduler.clientForAccount("a") != nil { + t.Fatal("failed strict proxy group should pause account requests") } if !group.failed { t.Fatal("failed group should remain tracked for standby refill") } + if !scheduler.accountProxyPaused("a") { + t.Fatal("account should be reported as paused") + } + if ok, _ := scheduler.ProxyRunGateFor(map[string]string{"a": "goods-1"}); ok { + t.Fatal("run gate should reject a failed proxy group") + } + res := scheduler.runAccountAttempt(types.ExchangeAttempt{ + Account: types.Account{Name: "a"}, + Product: types.Product{CommodityName: "测试商品"}, + }, types.SeckillModeAggressive, nil) + if !res.paused || res.result.Message != "等待备用代理" { + t.Fatalf("unexpected paused attempt result: %+v", res) + } } func TestProxyStatusReportsGroupAndAccountMapping(t *testing.T) { @@ -324,6 +350,66 @@ func TestProxyStatusReportsGroupAndAccountMapping(t *testing.T) { } } +func TestProxyRunGateAllowsStandbyShortageWhenActiveGroupsCoverAccounts(t *testing.T) { + act := *activity.Get("peace") + base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "") + scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) + scheduler.UpdateProxySettings(types.Config{ + ProxyEnabled: true, + ProxyRequired: true, + AccountsPerProxy: 5, + ProxySparePercent: 50, + }) + groupClient := &client.DouyuClient{} + group := &proxyRuntimeGroup{ + groupIndex: 1, + accountNames: []string{"a"}, + activeURL: "http://127.0.0.1:9000", + } + scheduler.replaceAccountClients(preparedProxyPool{ + accountClients: map[string]*client.DouyuClient{"a": groupClient}, + activeClients: []*client.DouyuClient{groupClient}, + groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group}, + allClients: []*client.DouyuClient{groupClient}, + standbyTarget: 5, + expiresAt: time.Now().Add(time.Minute), + }) + scheduler.setProxyReadiness(true, "") + + if ok, reason := scheduler.ProxyRunGateFor(map[string]string{"a": "goods-1"}); !ok { + t.Fatalf("standby shortage should not block covered active groups: %s", reason) + } +} + +func TestStandbyHealthProbeRemovesRepeatedlyFailingProxy(t *testing.T) { + act := *activity.Get("peace") + act.StoreHomeURL = "http://douyu.test/time" + base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "") + badClient, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, "http://127.0.0.1:1") + if err != nil { + t.Fatal(err) + } + scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) + scheduler.proxyStandbys = []validatedProxy{{ + url: "http://127.0.0.1:1", + client: badClient, + }} + scheduler.proxyClientPool = []*client.DouyuClient{badClient} + scheduler.proxyStandbyTarget = 1 + + for attempt := 0; attempt < proxyStandbyFailLimit; attempt++ { + if !scheduler.probeProxyStandbys(scheduler.proxyPoolGeneration) { + t.Fatal("standby probe should keep monitoring current generation") + } + } + if len(scheduler.proxyStandbys) != 0 { + t.Fatalf("standby count=%d, want 0", len(scheduler.proxyStandbys)) + } + if len(scheduler.proxyClientPool) != 0 { + t.Fatalf("client pool count=%d, want 0", len(scheduler.proxyClientPool)) + } +} + func TestStrictPrepareFailureBlocksRun(t *testing.T) { apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") diff --git a/internal/seckill/schedule_timer.go b/internal/seckill/schedule_timer.go index 3b9be59..6087fe9 100644 --- a/internal/seckill/schedule_timer.go +++ b/internal/seckill/schedule_timer.go @@ -163,7 +163,7 @@ func (s *Scheduler) scheduleTimers(accountProducts map[string]string, targetTime estimatedServer.In(constants.CST).Format("15:04:05.000"), calibration.Offset.Milliseconds(), calibration.ErrorBound.Milliseconds()) - if ok, reason := s.ProxyRunGate(); !ok { + if ok, reason := s.ProxyRunGateFor(accountProducts); !ok { logger.LogEvent("代理", "error", "严格代理模式未就绪,本次定时抢购不启动: %s", reason) return } diff --git a/internal/seckill/scheduler.go b/internal/seckill/scheduler.go index 1da9a56..bc40fdf 100644 --- a/internal/seckill/scheduler.go +++ b/internal/seckill/scheduler.go @@ -30,6 +30,8 @@ const ( resyncBeforeTrigger = 8 * time.Second proxyFetchBeforeTrigger = 120 * time.Second proxyReadyDeadline = 15 * time.Second + proxyPausedRetryDelay = 500 * time.Millisecond + proxyRefillRetryDelay = 2 * time.Second calibrationHistorySize = 3 maxCooldownCount = 5 xiaodianDefaultDelay = 500 * time.Millisecond @@ -46,6 +48,7 @@ type roundOutcome struct { cooldownAccounts map[string]time.Time results map[string]types.ExchangeResult attempts int + pausedAccounts int } type runSettings struct { @@ -77,6 +80,7 @@ type Scheduler struct { proxyStandbyTarget int proxyPoolGeneration uint64 proxyRefillInFlight bool + proxyRefillRetryAt time.Time proxyFallbackActive bool paths types.AppPaths font *truetype.Font @@ -104,6 +108,7 @@ type Scheduler struct { running bool stopRun chan struct{} stopRequested bool + proxyMonitor chan struct{} stateMu sync.RWMutex onStateChange func(string) // pendingClient 保存运行中收到的客户端更新,当前任务结束后自动应用。 @@ -244,11 +249,22 @@ func (s *Scheduler) clientForAccount(accountName string) *client.DouyuClient { return s.directClient } if cli := s.accountClients[accountName]; cli != nil { + if group := s.proxyGroups[cli]; group != nil && group.failed { + return nil + } return cli } return s.client } +func (s *Scheduler) accountProxyPaused(accountName string) bool { + s.clientMu.RLock() + defer s.clientMu.RUnlock() + cli := s.accountClients[accountName] + group := s.proxyGroups[cli] + return group != nil && group.failed +} + // UpdateClient 热替换底层 HTTP 客户端(用于活动切换)。 // 若有抢购任务正在运行则暂存新客户端,当前任务结束后自动应用,并返回 false。 func (s *Scheduler) UpdateClient(newClient *client.DouyuClient) bool { @@ -430,7 +446,13 @@ func (s *Scheduler) TryStartRun() bool { s.running = true s.stopRequested = false s.stopRun = make(chan struct{}) + s.proxyMonitor = make(chan struct{}) + monitor := s.proxyMonitor s.runMu.Unlock() + s.clientMu.RLock() + generation := s.proxyPoolGeneration + s.clientMu.RUnlock() + go s.monitorProxyStandbys(monitor, generation) s.emitState("running") return true } @@ -440,10 +462,18 @@ func (s *Scheduler) finishRun() { s.running = false s.stopRequested = false s.stopRun = nil + proxyMonitor := s.proxyMonitor + s.proxyMonitor = nil pendingClient := s.pendingClient s.pendingClient = nil s.runMu.Unlock() + if proxyMonitor != nil { + close(proxyMonitor) + } + s.clientMu.Lock() + s.proxyRefillRetryAt = time.Time{} + s.clientMu.Unlock() if pendingClient != nil { s.replaceClient(pendingClient) logger.LogEvent("配置", "success", "抢购任务结束,已应用等待中的客户端配置") @@ -474,19 +504,52 @@ func (s *Scheduler) IsRunning() bool { // ProxyRunGate reports whether a run may start under strict proxy mode. func (s *Scheduler) ProxyRunGate() (bool, string) { + return s.ProxyRunGateFor(nil) +} + +// ProxyRunGateFor additionally verifies that the current proxy pool covers the selected accounts. +func (s *Scheduler) ProxyRunGateFor(accountProducts map[string]string) (bool, string) { s.proxyMu.RLock() - defer s.proxyMu.RUnlock() if !s.proxyEnabled || !s.proxyRequired { + s.proxyMu.RUnlock() return true, "" } if s.proxyReady { - return true, "" + s.proxyMu.RUnlock() + } else { + reason := s.proxyBlockedReason + s.proxyMu.RUnlock() + if reason == "" { + reason = "代理尚未就绪" + } + return false, reason } - reason := s.proxyBlockedReason - if reason == "" { - reason = "代理尚未就绪" + s.clientMu.RLock() + dirty := s.accountClientsDirty + expired := !s.accountClientsExpireAt.IsZero() && + !time.Now().Before(s.accountClientsExpireAt.Add(-30*time.Second)) + hasFailedGroup := false + for _, group := range s.proxyGroups { + if group != nil && group.failed { + hasFailedGroup = true + break + } } - return false, reason + s.clientMu.RUnlock() + if dirty { + return false, "代理配置已变更,等待重新准备" + } + if expired { + return false, "代理池即将过期或已过期" + } + if hasFailedGroup { + return false, "代理池存在失败组,等待备用代理" + } + names := selectedAccountNames(accountProducts) + if len(names) > 0 && !s.accountClientsCover(names) { + return false, "代理池未覆盖当前账号、已失效或存在失败组" + } + return true, "" } func (s *Scheduler) stopRunChan() <-chan struct{} { @@ -543,7 +606,7 @@ func (s *Scheduler) RunNow(accountProducts map[string]string) { logger.LogEvent("代理", "error", "分组代理准备失败,本次抢购不执行: %v", err) return } - if ok, reason := s.ProxyRunGate(); !ok { + if ok, reason := s.ProxyRunGateFor(accountProductsForTasks(tasks)); !ok { logger.LogEvent("代理", "error", "严格代理模式未就绪,本次抢购不执行: %s", reason) return } @@ -878,6 +941,7 @@ func (s *Scheduler) runAccountLoop(task types.ExchangeAttempt, settings runSetti lastFailure := "" lastStockOut := false lastThrottled := false + pausedWarned := false for { if ctl.stopped() { @@ -910,6 +974,25 @@ func (s *Scheduler) runAccountLoop(task types.ExchangeAttempt, settings runSetti out.kind, out.reason = outcomeStopped, "已收到停止指令" return out } + if res.paused { + attempts-- + s.setResult(resultKey(task), "等待备用代理") + if !pausedWarned { + pausedWarned = true + logger.LogAccountEvent("代理", "warn", out.account, + "代理组故障且暂无备用,账号暂停请求并等待补提取: 账号=%s", out.account) + } + if !ctl.sleep(proxyPausedRetryDelay) { + if ctl.stopped() { + out.kind, out.reason = outcomeStopped, "已收到停止指令" + } else { + out.kind, out.reason = outcomeExpired, "抢购窗口已结束(代理组始终未恢复)" + s.setResult(resultKey(task), "失败|"+out.reason) + } + return out + } + continue + } if res.limitReached { // "已达本次活动兑换上限"= 这个账号在本次活动里已经换到过了。 // 它不是失败:2026-10-23 日志里这些账号紧接着就报了"积分已扣减,兑换确认"。 @@ -1222,7 +1305,7 @@ func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, del logger.LogEvent("代理", "error", "分组代理准备失败,本次和平小店抢购不执行: %v", err) return } - if ok, reason := s.ProxyRunGate(); !ok { + if ok, reason := s.ProxyRunGateFor(accountProductsForProxy); !ok { logger.LogEvent("代理", "error", "严格代理模式未就绪,本次和平小店抢购不执行: %s", reason) return } @@ -1306,6 +1389,13 @@ func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, del if len(stoppedAccounts) >= len(accounts) { break } + if outcome.pausedAccounts >= len(roundAttempts) { + if s.interruptibleSleep(proxyPausedRetryDelay) { + logger.LogEvent("抢购", "warn", "收到停止指令,和平小店抢购正在结束") + break + } + continue + } round++ waitBeforeRound = true } @@ -1410,6 +1500,9 @@ func (s *Scheduler) executeRoundWithRunner( if result.OK { outcome.successAccounts[attempt.Account.Name] = true } else { + if strings.Contains(result.Message, "等待备用代理") { + outcome.pausedAccounts++ + } if cooldown := retryCooldownDuration(result.Message); cooldown > 0 { outcome.cooldownAccounts[attempt.Account.Name] = time.Now().Add(jitterDuration(cooldown)) } @@ -1451,6 +1544,9 @@ func jitterDuration(base time.Duration) time.Duration { } func retryCooldownDuration(message string) time.Duration { + if strings.Contains(message, "等待备用代理") { + return proxyPausedRetryDelay + } if exchangeRateLimited(message) { return exchangeRateCooldown }