feat(seckill): 增加只读预演并强化代理风控切换
This commit is contained in:
@@ -19,18 +19,21 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
proxyFetchRounds = 3
|
||||
proxyFetchMaxRounds = 20
|
||||
proxyFetchRetryWait = 1200 * time.Millisecond
|
||||
proxyRequestTimeout = 15 * time.Second
|
||||
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
|
||||
proxyFetchRounds = 3
|
||||
proxyFetchMaxRounds = 20
|
||||
proxyFetchRetryWait = 1200 * time.Millisecond
|
||||
proxyRequestTimeout = 15 * time.Second
|
||||
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
|
||||
proxyRiskProbeWorkers = 3
|
||||
proxyRiskProbeMaxRounds = 20
|
||||
proxyRiskPreflightReserve = 15 * time.Second
|
||||
)
|
||||
|
||||
type proxySettingsSnapshot struct {
|
||||
@@ -156,6 +159,7 @@ func (s *Scheduler) UpdateProxySettings(cfg types.Config) {
|
||||
s.clientMu.Lock()
|
||||
s.accountClientsDirty = true
|
||||
s.proxyFallbackActive = false
|
||||
s.proxyBusinessRiskVerified = false
|
||||
s.clientMu.Unlock()
|
||||
}
|
||||
|
||||
@@ -215,6 +219,7 @@ func (s *Scheduler) closeAccountClients() {
|
||||
s.proxyRefillInFlight = false
|
||||
s.proxyRefillRetryAt = time.Time{}
|
||||
s.proxyFallbackActive = false
|
||||
s.proxyBusinessRiskVerified = false
|
||||
s.clientMu.Unlock()
|
||||
closeDouyuClientList(oldClients)
|
||||
}
|
||||
@@ -256,6 +261,7 @@ func (s *Scheduler) replaceAccountClients(pool preparedProxyPool) {
|
||||
s.proxyRefillInFlight = false
|
||||
s.proxyRefillRetryAt = time.Time{}
|
||||
s.proxyFallbackActive = false
|
||||
s.proxyBusinessRiskVerified = false
|
||||
s.clientMu.Unlock()
|
||||
closeDouyuClientList(oldClients)
|
||||
}
|
||||
@@ -400,6 +406,350 @@ func proxyDegradedReason(groups []types.ProxyGroupStatus, standbyCount, standbyT
|
||||
return ""
|
||||
}
|
||||
|
||||
type proxyBusinessRiskTarget struct {
|
||||
client *client.DouyuClient
|
||||
groupIndex int
|
||||
accountNames []string
|
||||
activeURL string
|
||||
activeIP string
|
||||
}
|
||||
|
||||
type proxyBusinessRiskProbeResult struct {
|
||||
target proxyBusinessRiskTarget
|
||||
accountName string
|
||||
err error
|
||||
definitive bool
|
||||
}
|
||||
|
||||
// preflightProxyBusinessRiskUntil 对每个活动代理组执行只读批量限制请求。
|
||||
// 命中活动火爆时切换备用代理,并对切换后的完整代理组重新检查;任何一组
|
||||
// 在截止时间前没有拿到明确业务响应,都不允许严格代理模式启动。
|
||||
func (s *Scheduler) preflightProxyBusinessRiskUntil(accountProducts map[string]string, deadline time.Time) error {
|
||||
base := s.clientSnapshot()
|
||||
if base == nil || base.Activity == nil {
|
||||
return errors.New("代理风控预检缺少活动配置")
|
||||
}
|
||||
if base.Activity.IsXiaodian || base.Activity.UseCTN {
|
||||
logger.LogEvent("代理", "warn",
|
||||
"当前活动没有批量限制接口,跳过1644400007启动前预检")
|
||||
s.setProxyBusinessRiskVerified(true)
|
||||
return nil
|
||||
}
|
||||
|
||||
for round := 1; round <= proxyRiskProbeMaxRounds; round++ {
|
||||
if !time.Now().Before(deadline) {
|
||||
return fmt.Errorf("代理风控预检超过截止时间")
|
||||
}
|
||||
targets, err := s.proxyBusinessRiskTargets(accountProducts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(targets) == 0 {
|
||||
return errors.New("代理风控预检没有可检查的活动代理组")
|
||||
}
|
||||
|
||||
results := make(chan proxyBusinessRiskProbeResult, len(targets))
|
||||
limit := make(chan struct{}, proxyRiskProbeWorkers)
|
||||
var wg sync.WaitGroup
|
||||
for _, target := range targets {
|
||||
wg.Add(1)
|
||||
go func(target proxyBusinessRiskTarget) {
|
||||
defer wg.Done()
|
||||
limit <- struct{}{}
|
||||
defer func() { <-limit }()
|
||||
accountName, probeErr, definitive := s.probeProxyBusinessRiskTarget(
|
||||
target,
|
||||
accountProducts,
|
||||
deadline,
|
||||
)
|
||||
results <- proxyBusinessRiskProbeResult{
|
||||
target: target,
|
||||
accountName: accountName,
|
||||
err: probeErr,
|
||||
definitive: definitive,
|
||||
}
|
||||
}(target)
|
||||
}
|
||||
wg.Wait()
|
||||
close(results)
|
||||
|
||||
var (
|
||||
hotTargets []proxyBusinessRiskProbeResult
|
||||
unverified []string
|
||||
)
|
||||
for result := range results {
|
||||
switch {
|
||||
case result.definitive && isProxyRiskBusinessError(result.err):
|
||||
hotTargets = append(hotTargets, result)
|
||||
case result.definitive:
|
||||
logger.LogEvent("代理", "success",
|
||||
"代理风控预检通过: 组=%d 账号=%s 出口=%s",
|
||||
result.target.groupIndex,
|
||||
result.accountName,
|
||||
client.MaskProxyURL(result.target.activeURL),
|
||||
)
|
||||
default:
|
||||
unverified = append(unverified, fmt.Sprintf(
|
||||
"G%d(%s): %v",
|
||||
result.target.groupIndex,
|
||||
client.MaskProxyURL(result.target.activeURL),
|
||||
result.err,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
if len(hotTargets) > 0 {
|
||||
for _, result := range hotTargets {
|
||||
logger.LogEvent("代理", "warn",
|
||||
"代理风控预检命中活动火爆/1644400007: 组=%d 账号=%s err=%v",
|
||||
result.target.groupIndex,
|
||||
result.accountName,
|
||||
result.err,
|
||||
)
|
||||
s.handleProxyBusinessRisk(result.accountName, result.target.client, result.err)
|
||||
}
|
||||
if err := s.refillProxyStandbysForPreflight(base, s.proxySettings(), deadline); err != nil {
|
||||
s.clientMu.RLock()
|
||||
remainingFailed := countFailedProxyGroups(s.proxyGroups)
|
||||
s.clientMu.RUnlock()
|
||||
if remainingFailed > 0 {
|
||||
return fmt.Errorf("代理风控预检无法恢复全部代理组: %w", err)
|
||||
}
|
||||
logger.LogEvent("代理", "warn",
|
||||
"代理活动组已切换,但备用代理补提取未完成: %v", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if len(unverified) > 0 {
|
||||
return fmt.Errorf("代理风控预检未通过: %s", strings.Join(unverified, ";"))
|
||||
}
|
||||
|
||||
s.setProxyBusinessRiskVerified(true)
|
||||
s.setProxyReadiness(true, "")
|
||||
if ok, reason := s.ProxyRunGateFor(accountProducts); !ok {
|
||||
s.setProxyBusinessRiskVerified(false)
|
||||
s.setProxyReadiness(false, reason)
|
||||
return fmt.Errorf("代理风控预检后门禁未通过: %s", reason)
|
||||
}
|
||||
logger.LogEvent("代理", "success",
|
||||
"代理风控预检完成: 活动组=%d,轮次=%d,全部未返回活动火爆/1644400007",
|
||||
len(targets), round)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("代理风控预检切换轮次超过上限%d", proxyRiskProbeMaxRounds)
|
||||
}
|
||||
|
||||
func (s *Scheduler) refillProxyStandbysForPreflight(
|
||||
base *client.DouyuClient,
|
||||
cfg proxySettingsSnapshot,
|
||||
deadline time.Time,
|
||||
) error {
|
||||
if strings.TrimSpace(cfg.proxyAPI) == "" {
|
||||
return errors.New("没有代理提取API,无法补充新的备用代理")
|
||||
}
|
||||
timeout := time.Until(deadline)
|
||||
if timeout <= 0 {
|
||||
return errors.New("代理风控预检已到截止时间,无法继续补提取")
|
||||
}
|
||||
|
||||
s.clientMu.RLock()
|
||||
generation := s.proxyPoolGeneration
|
||||
need := s.proxyStandbyTarget - len(s.proxyStandbys)
|
||||
if need < 0 {
|
||||
need = 0
|
||||
}
|
||||
need += countFailedProxyGroups(s.proxyGroups)
|
||||
excludedURLs := copyStringSet(s.proxyExcludedURLs)
|
||||
s.clientMu.RUnlock()
|
||||
if need <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.LogEvent("代理", "info",
|
||||
"代理风控预检正在补提取备用代理: 需要=%d,截止=%s",
|
||||
need, deadline.In(constants.CST).Format("15:04:05.000"))
|
||||
proxies, fetchErr := s.fetchAndValidateProxyPool(base, cfg, need, excludedURLs, timeout)
|
||||
|
||||
var extras []*client.DouyuClient
|
||||
var expiredClients []*client.DouyuClient
|
||||
var switches []proxySwitchEvent
|
||||
s.clientMu.Lock()
|
||||
if generation != s.proxyPoolGeneration {
|
||||
s.clientMu.Unlock()
|
||||
closeValidatedProxies(proxies)
|
||||
return errors.New("代理池已被重建,取消本次补提取")
|
||||
}
|
||||
currentNeed := s.proxyStandbyTarget - len(s.proxyStandbys)
|
||||
if currentNeed < 0 {
|
||||
currentNeed = 0
|
||||
}
|
||||
currentNeed += countFailedProxyGroups(s.proxyGroups)
|
||||
if len(proxies) > currentNeed {
|
||||
for _, proxy := range proxies[currentNeed:] {
|
||||
extras = append(extras, proxy.client)
|
||||
}
|
||||
proxies = proxies[:currentNeed]
|
||||
}
|
||||
for _, proxy := range proxies {
|
||||
proxyClient := proxy.client
|
||||
client.MonitorProxyHealth(proxyClient, func(err error) {
|
||||
s.recordProxyResult(proxyClient, err)
|
||||
})
|
||||
s.proxyStandbys = append(s.proxyStandbys, proxy)
|
||||
s.proxyClientPool = append(s.proxyClientPool, proxy.client)
|
||||
s.proxyExcludedURLs[proxy.url] = struct{}{}
|
||||
}
|
||||
for {
|
||||
pendingClient, pendingGroup := findFailedProxyGroup(s.proxyGroups)
|
||||
if pendingClient == nil {
|
||||
break
|
||||
}
|
||||
event, expired, switched := s.switchGroupToStandbyAvoidIPLocked(
|
||||
pendingClient,
|
||||
pendingGroup,
|
||||
pendingGroup.activeIP,
|
||||
)
|
||||
expiredClients = append(expiredClients, expired...)
|
||||
if !switched {
|
||||
break
|
||||
}
|
||||
switches = append(switches, event)
|
||||
}
|
||||
remainingFailed := countFailedProxyGroups(s.proxyGroups)
|
||||
standbyCount := len(s.proxyStandbys)
|
||||
standbyTarget := s.proxyStandbyTarget
|
||||
s.clientMu.Unlock()
|
||||
|
||||
closeDouyuClientList(extras)
|
||||
closeDouyuClientList(expiredClients)
|
||||
for _, event := range switches {
|
||||
logProxySwitch("代理风控补提取后切换", event)
|
||||
}
|
||||
if len(proxies) > 0 {
|
||||
logger.LogEvent("代理", "success",
|
||||
"代理风控预检补提取完成: 新增=%d,备用=%d/%d,剩余失败组=%d",
|
||||
len(proxies), standbyCount, standbyTarget, remainingFailed)
|
||||
}
|
||||
if remainingFailed > 0 {
|
||||
if fetchErr != nil {
|
||||
return fmt.Errorf("仍有%d个代理组等待备用代理,补提取失败: %w", remainingFailed, fetchErr)
|
||||
}
|
||||
return fmt.Errorf("仍有%d个代理组没有可用的不同出口备用代理", remainingFailed)
|
||||
}
|
||||
if fetchErr != nil && len(proxies) == 0 {
|
||||
logger.LogEvent("代理", "warn", "代理风控预检备用补提取未成功: %v", fetchErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scheduler) proxyBusinessRiskTargets(accountProducts map[string]string) ([]proxyBusinessRiskTarget, error) {
|
||||
names := selectedAccountNames(accountProducts)
|
||||
s.clientMu.RLock()
|
||||
targetsByGroup := make(map[int]proxyBusinessRiskTarget)
|
||||
var problems []string
|
||||
for _, name := range names {
|
||||
proxyClient := s.accountClients[name]
|
||||
if proxyClient == nil || proxyClient == s.directClient {
|
||||
problems = append(problems, name+"未分配代理")
|
||||
continue
|
||||
}
|
||||
group := s.proxyGroups[proxyClient]
|
||||
if group == nil || group.failed || strings.TrimSpace(group.activeURL) == "" {
|
||||
problems = append(problems, name+"所在代理组不可用")
|
||||
continue
|
||||
}
|
||||
if _, exists := targetsByGroup[group.groupIndex]; exists {
|
||||
continue
|
||||
}
|
||||
targetsByGroup[group.groupIndex] = proxyBusinessRiskTarget{
|
||||
client: proxyClient,
|
||||
groupIndex: group.groupIndex,
|
||||
accountNames: append([]string(nil), group.accountNames...),
|
||||
activeURL: group.activeURL,
|
||||
activeIP: group.activeIP,
|
||||
}
|
||||
}
|
||||
s.clientMu.RUnlock()
|
||||
if len(problems) > 0 {
|
||||
return nil, fmt.Errorf("代理风控预检分组不完整: %s", strings.Join(problems, ";"))
|
||||
}
|
||||
|
||||
targets := make([]proxyBusinessRiskTarget, 0, len(targetsByGroup))
|
||||
for _, target := range targetsByGroup {
|
||||
targets = append(targets, target)
|
||||
}
|
||||
sort.Slice(targets, func(i, j int) bool {
|
||||
return targets[i].groupIndex < targets[j].groupIndex
|
||||
})
|
||||
return targets, nil
|
||||
}
|
||||
|
||||
func (s *Scheduler) probeProxyBusinessRiskTarget(
|
||||
target proxyBusinessRiskTarget,
|
||||
accountProducts map[string]string,
|
||||
deadline time.Time,
|
||||
) (string, error, bool) {
|
||||
var lastErr error
|
||||
for _, accountName := range target.accountNames {
|
||||
if !time.Now().Before(deadline) {
|
||||
return accountName, errors.New("代理风控预检超过截止时间"), false
|
||||
}
|
||||
productID := strings.TrimSpace(accountProducts[accountName])
|
||||
if productID == "" {
|
||||
continue
|
||||
}
|
||||
cookie, ok := s.AccountCookieByName(accountName)
|
||||
if !ok || strings.TrimSpace(cookie) == "" {
|
||||
lastErr = errors.New("账号Cookie为空")
|
||||
continue
|
||||
}
|
||||
_, csrf := s.cachedAuth(types.ExchangeAttempt{
|
||||
Account: types.Account{Name: accountName},
|
||||
Product: types.Product{CommodityID: productID},
|
||||
})
|
||||
if strings.TrimSpace(csrf) == "" {
|
||||
token, err := target.client.GenerateCSRF(cookie)
|
||||
if err != nil {
|
||||
lastErr = fmt.Errorf("获取CSRF失败: %w", err)
|
||||
logger.LogEvent("代理", "debug",
|
||||
"代理风控预检账号不可用: 组=%d 账号=%s err=%v",
|
||||
target.groupIndex, accountName, err)
|
||||
continue
|
||||
}
|
||||
csrf = token
|
||||
}
|
||||
_, err := target.client.BatchExchangeLimit(cookie, productID, &csrf)
|
||||
if err == nil {
|
||||
return accountName, nil, true
|
||||
}
|
||||
var orderErr *client.OrderError
|
||||
if errors.As(err, &orderErr) {
|
||||
return accountName, err, true
|
||||
}
|
||||
lastErr = err
|
||||
logger.LogEvent("代理", "debug",
|
||||
"代理风控预检账号请求失败: 组=%d 账号=%s err=%v",
|
||||
target.groupIndex, accountName, err)
|
||||
}
|
||||
if lastErr == nil {
|
||||
lastErr = errors.New("组内没有可检查的账号")
|
||||
}
|
||||
return "", fmt.Errorf("组内账号均未取得明确业务响应: %w", lastErr), false
|
||||
}
|
||||
|
||||
func (s *Scheduler) setProxyBusinessRiskVerified(verified bool) {
|
||||
s.clientMu.Lock()
|
||||
s.proxyBusinessRiskVerified = verified
|
||||
s.clientMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Scheduler) proxyBusinessRiskVerifiedSnapshot() bool {
|
||||
s.clientMu.RLock()
|
||||
verified := s.proxyBusinessRiskVerified
|
||||
s.clientMu.RUnlock()
|
||||
return verified
|
||||
}
|
||||
|
||||
func closeDouyuClients(clients map[string]*client.DouyuClient) {
|
||||
items := make([]*client.DouyuClient, 0, len(clients))
|
||||
for _, cli := range clients {
|
||||
@@ -632,6 +982,14 @@ func (s *Scheduler) prepareProxyGroupsUntil(accountProducts map[string]string, f
|
||||
return nil
|
||||
}
|
||||
if !forceRefresh && s.accountClientsCover(names) {
|
||||
if s.proxyBusinessRiskVerifiedSnapshot() {
|
||||
s.setProxyReadiness(true, "")
|
||||
return nil
|
||||
}
|
||||
s.setProxyPreparing("代理已覆盖,正在补做业务风控预检")
|
||||
if err := s.preflightProxyBusinessRiskUntil(accountProducts, deadline); err != nil {
|
||||
return s.handleProxyPrepareFailure(cfg, names, err)
|
||||
}
|
||||
s.setProxyReadiness(true, "")
|
||||
return nil
|
||||
}
|
||||
@@ -649,7 +1007,11 @@ func (s *Scheduler) prepareProxyGroupsUntil(accountProducts map[string]string, f
|
||||
if timeout <= 0 {
|
||||
return s.handleProxyPrepareFailure(cfg, names, errors.New("代理准备截止时间已到"))
|
||||
}
|
||||
proxies, err := s.buildProxyPool(base, cfg, activeCount, activeCount+standbyTarget, nil, timeout)
|
||||
buildTimeout := timeout
|
||||
if buildTimeout > proxyRiskPreflightReserve*2 {
|
||||
buildTimeout -= proxyRiskPreflightReserve
|
||||
}
|
||||
proxies, err := s.buildProxyPool(base, cfg, activeCount, activeCount+standbyTarget, nil, buildTimeout)
|
||||
if err != nil {
|
||||
return s.handleProxyPrepareFailure(cfg, names, err)
|
||||
}
|
||||
@@ -720,6 +1082,10 @@ func (s *Scheduler) prepareProxyGroupsUntil(accountProducts map[string]string, f
|
||||
s.replaceProxyGroupHistory(groups, activeCount, proxies)
|
||||
logger.LogEvent("代理", "success", "代理池准备完成: 账号=%d,每组=%d,活动代理=%d,备用代理=%d/%d",
|
||||
len(names), cfg.accountsPerProxy, activeCount, len(standbys), standbyTarget)
|
||||
s.setProxyPreparing("代理池已建立,正在逐组执行活动火爆预检")
|
||||
if err := s.preflightProxyBusinessRiskUntil(accountProducts, deadline); err != nil {
|
||||
return s.handleProxyPrepareFailure(cfg, names, err)
|
||||
}
|
||||
s.setProxyReadiness(true, "")
|
||||
return nil
|
||||
}
|
||||
@@ -746,6 +1112,7 @@ func (s *Scheduler) setDirectFallbackAccounts(names []string) {
|
||||
s.accountClientOrder = []*client.DouyuClient{s.directClient}
|
||||
s.accountClientsDirty = false
|
||||
}
|
||||
s.proxyBusinessRiskVerified = false
|
||||
s.clientMu.Unlock()
|
||||
}
|
||||
|
||||
@@ -1103,6 +1470,7 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
|
||||
return
|
||||
}
|
||||
|
||||
s.proxyBusinessRiskVerified = false
|
||||
group.consecutiveFailures++
|
||||
group.lastError = err.Error()
|
||||
s.updateProxyGroupHistoryLocked(group)
|
||||
@@ -1152,8 +1520,100 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
|
||||
}
|
||||
}
|
||||
|
||||
// handleProxyBusinessRisk switches away from the egress IP used by an account
|
||||
// when the lock API returns the explicit "activity hot" risk-control code.
|
||||
func (s *Scheduler) handleProxyBusinessRisk(accountName string, proxyClient *client.DouyuClient, cause error) {
|
||||
if !isProxyRiskBusinessError(cause) {
|
||||
return
|
||||
}
|
||||
cfg := s.proxySettings()
|
||||
|
||||
type riskGroup struct {
|
||||
client *client.DouyuClient
|
||||
group *proxyRuntimeGroup
|
||||
}
|
||||
s.clientMu.Lock()
|
||||
if proxyClient == nil || proxyClient == s.directClient || s.proxyFallbackActive {
|
||||
s.clientMu.Unlock()
|
||||
return
|
||||
}
|
||||
initial := s.proxyGroups[proxyClient]
|
||||
if initial == nil || initial.failed {
|
||||
s.clientMu.Unlock()
|
||||
return
|
||||
}
|
||||
riskIP := strings.TrimSpace(initial.activeIP)
|
||||
targets := make([]riskGroup, 0, 1)
|
||||
seenGroups := make(map[int]struct{})
|
||||
for proxyClient, group := range s.proxyGroups {
|
||||
if group == nil {
|
||||
continue
|
||||
}
|
||||
if group != initial && (riskIP == "" || strings.TrimSpace(group.activeIP) != riskIP) {
|
||||
continue
|
||||
}
|
||||
if _, seen := seenGroups[group.groupIndex]; seen {
|
||||
continue
|
||||
}
|
||||
seenGroups[group.groupIndex] = struct{}{}
|
||||
targets = append(targets, riskGroup{client: proxyClient, group: group})
|
||||
}
|
||||
|
||||
var (
|
||||
expiredClients []*client.DouyuClient
|
||||
switchEvents []proxySwitchEvent
|
||||
failedGroups []*proxyRuntimeGroup
|
||||
needRefill bool
|
||||
)
|
||||
for _, target := range targets {
|
||||
target.group.lastError = cause.Error()
|
||||
s.updateProxyGroupHistoryLocked(target.group)
|
||||
event, expired, switched := s.switchGroupToStandbyAvoidIPLocked(target.client, target.group, riskIP)
|
||||
expiredClients = append(expiredClients, expired...)
|
||||
if switched {
|
||||
switchEvents = append(switchEvents, event)
|
||||
needRefill = needRefill || len(s.proxyStandbys) < s.proxyStandbyTarget
|
||||
continue
|
||||
}
|
||||
if !cfg.required && s.directClient != nil {
|
||||
event = s.switchGroupToDirectLocked(target.client, target.group)
|
||||
switchEvents = append(switchEvents, event)
|
||||
needRefill = true
|
||||
continue
|
||||
}
|
||||
target.group.failed = true
|
||||
s.proxyBusinessRiskVerified = false
|
||||
failedGroups = append(failedGroups, target.group)
|
||||
needRefill = true
|
||||
s.updateProxyGroupHistoryLocked(target.group)
|
||||
}
|
||||
needRefill = needRefill && !s.proxyRefillInFlight
|
||||
s.clientMu.Unlock()
|
||||
|
||||
closeDouyuClientList(expiredClients)
|
||||
for _, event := range switchEvents {
|
||||
logProxySwitch("代理风控切换", event)
|
||||
}
|
||||
for _, group := range failedGroups {
|
||||
logger.LogEvent("代理", "warn",
|
||||
"代理风控命中且无不同IP备用: 组=%d 账号=%d 出口=%s,严格模式暂停并等待补提取",
|
||||
group.groupIndex, len(group.accountNames), client.MaskProxyURL(group.activeURL))
|
||||
}
|
||||
logger.LogEvent("代理", "warn",
|
||||
"代理业务风控: 账号=%s 出口IP=%s 原因=%v 切换组数=%d",
|
||||
accountName, riskIP, cause, len(switchEvents))
|
||||
if needRefill {
|
||||
s.startProxyRefill()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, group *proxyRuntimeGroup) (proxySwitchEvent, []*client.DouyuClient, bool) {
|
||||
return s.switchGroupToStandbyAvoidIPLocked(oldClient, group, "")
|
||||
}
|
||||
|
||||
func (s *Scheduler) switchGroupToStandbyAvoidIPLocked(oldClient *client.DouyuClient, group *proxyRuntimeGroup, avoidIP string) (proxySwitchEvent, []*client.DouyuClient, bool) {
|
||||
expiredClients := make([]*client.DouyuClient, 0, 1)
|
||||
var skipped []validatedProxy
|
||||
for len(s.proxyStandbys) > 0 {
|
||||
standby := s.proxyStandbys[0]
|
||||
s.proxyStandbys = s.proxyStandbys[1:]
|
||||
@@ -1161,6 +1621,13 @@ func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, gr
|
||||
expiredClients = append(expiredClients, standby.client)
|
||||
continue
|
||||
}
|
||||
if avoidIP != "" && standby.ip == avoidIP {
|
||||
skipped = append(skipped, standby)
|
||||
continue
|
||||
}
|
||||
if len(skipped) > 0 {
|
||||
s.proxyStandbys = append(skipped, s.proxyStandbys...)
|
||||
}
|
||||
|
||||
event := proxySwitchEvent{
|
||||
accountNames: append([]string(nil), group.accountNames...),
|
||||
@@ -1179,6 +1646,7 @@ func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, gr
|
||||
group.consecutiveFailures = 0
|
||||
group.lastError = ""
|
||||
group.failed = false
|
||||
s.proxyBusinessRiskVerified = false
|
||||
for _, accountName := range group.accountNames {
|
||||
s.accountClients[accountName] = standby.client
|
||||
}
|
||||
@@ -1188,6 +1656,9 @@ func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, gr
|
||||
s.updateProxyGroupHistoryLocked(group)
|
||||
return event, expiredClients, true
|
||||
}
|
||||
if len(skipped) > 0 {
|
||||
s.proxyStandbys = append(skipped, s.proxyStandbys...)
|
||||
}
|
||||
return proxySwitchEvent{}, expiredClients, false
|
||||
}
|
||||
|
||||
@@ -1204,6 +1675,7 @@ func (s *Scheduler) switchGroupToDirectLocked(oldClient *client.DouyuClient, gro
|
||||
group.consecutiveFailures = 0
|
||||
group.lastError = ""
|
||||
group.failed = false
|
||||
s.proxyBusinessRiskVerified = false
|
||||
for _, accountName := range group.accountNames {
|
||||
s.accountClients[accountName] = s.directClient
|
||||
}
|
||||
@@ -1267,6 +1739,10 @@ func (s *Scheduler) startProxyRefill() {
|
||||
|
||||
s.clientMu.Lock()
|
||||
need := s.proxyStandbyTarget - len(s.proxyStandbys)
|
||||
if need < 0 {
|
||||
need = 0
|
||||
}
|
||||
need += countFailedProxyGroups(s.proxyGroups)
|
||||
if need <= 0 || s.proxyRefillInFlight {
|
||||
s.clientMu.Unlock()
|
||||
return
|
||||
@@ -1300,6 +1776,7 @@ func (s *Scheduler) refillProxyStandbys(base *client.DouyuClient, cfg proxySetti
|
||||
if currentNeed < 0 {
|
||||
currentNeed = 0
|
||||
}
|
||||
currentNeed += countFailedProxyGroups(s.proxyGroups)
|
||||
if len(proxies) > currentNeed {
|
||||
for _, proxy := range proxies[currentNeed:] {
|
||||
extras = append(extras, proxy.client)
|
||||
@@ -1321,7 +1798,11 @@ func (s *Scheduler) refillProxyStandbys(base *client.DouyuClient, cfg proxySetti
|
||||
if pendingClient == nil {
|
||||
break
|
||||
}
|
||||
event, expired, switched := s.switchGroupToStandbyLocked(pendingClient, pendingGroup)
|
||||
event, expired, switched := s.switchGroupToStandbyAvoidIPLocked(
|
||||
pendingClient,
|
||||
pendingGroup,
|
||||
pendingGroup.activeIP,
|
||||
)
|
||||
expiredClients = append(expiredClients, expired...)
|
||||
if !switched {
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user