fix(proxy): 完善备用补提与失败组暂停

- 备用不足不再触发活动代理池重建\n- 白名单 verify 限频错误保留消息并允许重试\n- 严格模式失败组暂停请求并持续补提取\n- 增加备用代理健康探测与运行门禁校验
This commit is contained in:
yml
2026-11-21 22:01:48 +08:00
parent d37fd50233
commit 5b2dc22eb5
9 changed files with 440 additions and 26 deletions
+158 -6
View File
@@ -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 {