fix(proxy): 完善备用补提与失败组暂停
- 备用不足不再触发活动代理池重建\n- 白名单 verify 限频错误保留消息并允许重试\n- 严格模式失败组暂停请求并持续补提取\n- 增加备用代理健康探测与运行门禁校验
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user