feat(seckill): 增加只读预演并强化代理风控切换
This commit is contained in:
@@ -56,7 +56,11 @@ func (a *App) FetchProxy(apiURL string) types.ProxyFetchResult {
|
||||
}
|
||||
|
||||
func extractProxyEndpoints(cfg types.Config, apiURL string, count int) ([]proxyprovider.Endpoint, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
return extractProxyEndpointsWithTimeout(cfg, apiURL, count, 30*time.Second)
|
||||
}
|
||||
|
||||
func extractProxyEndpointsWithTimeout(cfg types.Config, apiURL string, count int, timeout time.Duration) ([]proxyprovider.Endpoint, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
service := proxyprovider.NewService()
|
||||
endpoints, err := service.ExtractWithWhitelist(ctx, apiURL, count, proxyprovider.WhitelistConfig{
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -29,6 +29,39 @@ func TestProxyDrillPlanAndTargets(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateProxyDrillMatchesRuntimeGate(t *testing.T) {
|
||||
plan := proxyDrillPlan{active: 4, standby: 2}
|
||||
cases := []struct {
|
||||
name string
|
||||
healthyActive int
|
||||
healthyStandby int
|
||||
extracted int
|
||||
wantSuccess bool
|
||||
wantWarning bool
|
||||
}{
|
||||
{name: "all healthy", healthyActive: 4, healthyStandby: 2, extracted: 6, wantSuccess: true},
|
||||
{name: "standby shortage still starts", healthyActive: 4, healthyStandby: 1, extracted: 5, wantSuccess: true, wantWarning: true},
|
||||
{name: "active failure blocks", healthyActive: 3, healthyStandby: 2, extracted: 6, wantSuccess: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
success, errMessage, warning := evaluateProxyDrill(plan, tc.healthyActive, tc.healthyStandby, tc.extracted)
|
||||
if success != tc.wantSuccess {
|
||||
t.Fatalf("success=%v, want %v", success, tc.wantSuccess)
|
||||
}
|
||||
if tc.wantSuccess && errMessage != "" {
|
||||
t.Fatalf("unexpected error: %s", errMessage)
|
||||
}
|
||||
if !tc.wantSuccess && errMessage == "" {
|
||||
t.Fatal("active failure should return an error")
|
||||
}
|
||||
if (warning != "") != tc.wantWarning {
|
||||
t.Fatalf("warning=%q, wantWarning=%v", warning, tc.wantWarning)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractProxyDrillPoolDeduplicates(t *testing.T) {
|
||||
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
for i := 0; i < 8; i++ {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
// StartSeckillDrill schedules the one-minute read-only seckill rehearsal.
|
||||
func (a *App) StartSeckillDrill(accountProducts map[string]string) types.MessageResult {
|
||||
scheduler := a.getScheduler()
|
||||
if scheduler == nil {
|
||||
return types.MessageResult{Error: "调度器未初始化"}
|
||||
}
|
||||
status, err := scheduler.StartSeckillDrill(accountProducts)
|
||||
if err != nil {
|
||||
return types.MessageResult{Error: err.Error()}
|
||||
}
|
||||
return types.MessageResult{
|
||||
Success: true,
|
||||
Msg: fmt.Sprintf("抢购预演已设置,触发时间 %s", status.TriggerTime),
|
||||
}
|
||||
}
|
||||
|
||||
// GetSeckillDrillStatus returns the current read-only rehearsal state.
|
||||
func (a *App) GetSeckillDrillStatus() types.SeckillDrillStatus {
|
||||
scheduler := a.getScheduler()
|
||||
if scheduler == nil {
|
||||
return types.SeckillDrillStatus{State: "idle"}
|
||||
}
|
||||
status := scheduler.GetSeckillDrillStatus()
|
||||
if status.State == "" {
|
||||
status.State = "idle"
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
// CancelSeckillDrill cancels a pending or running rehearsal.
|
||||
func (a *App) CancelSeckillDrill() types.SimpleResult {
|
||||
scheduler := a.getScheduler()
|
||||
if scheduler == nil || !scheduler.CancelSeckillDrill() {
|
||||
return types.SimpleResult{Success: true}
|
||||
}
|
||||
return types.SimpleResult{Success: true}
|
||||
}
|
||||
Reference in New Issue
Block a user