feat(seckill): 增加只读预演并强化代理风控切换

This commit is contained in:
yml
2026-11-22 14:52:46 +08:00
parent 70612a8f0c
commit e20907800d
20 changed files with 1916 additions and 40 deletions
+39 -5
View File
@@ -19,6 +19,7 @@ const (
maxProxyDrillAccounts = 200
proxyDrillExtractRounds = 3
proxyDrillMaxRounds = 20
proxyDrillTotalTimeout = 2 * time.Minute
)
type proxyDrillPlan struct {
@@ -133,9 +134,14 @@ func (a *App) RunProxyDrill(req types.ProxyDrillRequest) types.ProxyDrillResult
result.HealthyStandby++
}
}
result.Success = result.HealthyActive >= plan.active && result.HealthyStandby >= plan.standby
if !result.Success {
result.Error = "活动代理或备用代理未全部通过演练"
result.Success, result.Error, result.Warning = evaluateProxyDrill(
plan,
result.HealthyActive,
result.HealthyStandby,
len(endpoints),
)
if err != nil && result.Warning == "" {
result.Warning = err.Error()
}
result.DurationMs = time.Since(startedAt).Milliseconds()
logger.LogEvent("代理", "success", "代理池演练完成: 成功=%v,活动代理=%d/%d,备用代理=%d/%d,耗时=%s",
@@ -144,6 +150,24 @@ func (a *App) RunProxyDrill(req types.ProxyDrillRequest) types.ProxyDrillResult
return result
}
func evaluateProxyDrill(plan proxyDrillPlan, healthyActive, healthyStandby, extracted int) (bool, string, string) {
if healthyActive < plan.active {
return false, fmt.Sprintf("活动代理未全部通过演练: %d/%d", healthyActive, plan.active), ""
}
var warnings []string
if extracted < plan.active+plan.standby {
warnings = append(warnings, fmt.Sprintf(
"备用代理提取不足: %d/%d", max(0, extracted-plan.active), plan.standby,
))
}
if healthyStandby < plan.standby {
warnings = append(warnings, fmt.Sprintf(
"备用代理健康数不足: %d/%d", healthyStandby, plan.standby,
))
}
return true, "", strings.Join(warnings, ";")
}
func proxyDrillStandbyCount(activeCount, sparePercent int) int {
if activeCount <= 0 {
return 0
@@ -173,8 +197,15 @@ func (a *App) extractProxyDrillPool(cfg types.Config, desired int) ([]proxyprovi
seen := make(map[string]struct{}, desired)
endpoints := make([]proxyprovider.Endpoint, 0, desired)
var lastErr error
deadline := time.Now().Add(proxyDrillTotalTimeout)
for round := 0; round < rounds && len(endpoints) < desired; round++ {
batch, err := extractProxyEndpoints(cfg, cfg.ProxyAPI, desired-len(endpoints))
remaining := time.Until(deadline)
if remaining <= 0 {
lastErr = fmt.Errorf("代理池演练总时长超过 %s", proxyDrillTotalTimeout)
break
}
requestTimeout := min(30*time.Second, remaining)
batch, err := extractProxyEndpointsWithTimeout(cfg, cfg.ProxyAPI, desired-len(endpoints), requestTimeout)
if err != nil {
lastErr = err
if proxyprovider.IsFatal(err) {
@@ -195,7 +226,10 @@ func (a *App) extractProxyDrillPool(cfg types.Config, desired int) ([]proxyprovi
}
}
if round < rounds-1 {
time.Sleep(1200 * time.Millisecond)
wait := min(1200*time.Millisecond, time.Until(deadline))
if wait > 0 {
time.Sleep(wait)
}
}
}
if len(endpoints) < desired {