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}
|
||||
}
|
||||
+11
-10
@@ -1207,7 +1207,7 @@ func (c *DouyuClient) generateCSRF(cookie string, forceRefresh bool) (string, er
|
||||
traceErr = api.Msg
|
||||
c.Trace.RecordResponse("生成CSRF", req, resp, resp.StatusCode, time.Since(start), reqBody, body, traceErr, api.Error, api.Msg)
|
||||
c.logNetworkAPIResult("生成CSRF", req, time.Since(start), api.Error, api.Msg)
|
||||
return "", fmt.Errorf("CSRF 接口失败: %s", api.Msg)
|
||||
return "", &OrderError{Kind: "CSRF接口失败", Code: api.Error, Msg: api.Msg}
|
||||
}
|
||||
for _, ck := range resp.Cookies() {
|
||||
if ck.Name == "cvl_csrf_token" && ck.Value != "" {
|
||||
@@ -1306,7 +1306,7 @@ func (c *DouyuClient) BatchExchangeLimit(cookie, commodityID string, csrf *strin
|
||||
return 0, err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
return 0, fmt.Errorf("批量限制接口失败: %s", result.Msg)
|
||||
return 0, &OrderError{Kind: "批量限制接口失败", Code: result.Error, Msg: result.Msg}
|
||||
}
|
||||
var data struct {
|
||||
Limit string `json:"limit"`
|
||||
@@ -1344,7 +1344,7 @@ func (c *DouyuClient) PreExchange(cookie, commodityID string, csrf *string) erro
|
||||
return err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
return fmt.Errorf("预兑换失败: %s", result.Msg)
|
||||
return &OrderError{Kind: "预兑换失败", Code: result.Error, Msg: result.Msg}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1362,10 +1362,14 @@ type OrderError struct {
|
||||
}
|
||||
|
||||
func (e *OrderError) Error() string {
|
||||
if strings.TrimSpace(e.Msg) == "" {
|
||||
return e.Kind
|
||||
kind := e.Kind
|
||||
if e.Code != 0 {
|
||||
kind += fmt.Sprintf("(error=%d)", e.Code)
|
||||
}
|
||||
return e.Kind + ": " + e.Msg
|
||||
if strings.TrimSpace(e.Msg) == "" {
|
||||
return kind
|
||||
}
|
||||
return kind + ": " + e.Msg
|
||||
}
|
||||
|
||||
// LockedExchangeOrder 是精英手册锁单结果。
|
||||
@@ -1581,10 +1585,7 @@ func (c *DouyuClient) Exchange(cookie, commodityID string, csrf *string) (string
|
||||
}
|
||||
}
|
||||
if result.Error != 0 {
|
||||
if strings.TrimSpace(result.Msg) == "" {
|
||||
return "", errors.New("兑换失败,接口未返回原因")
|
||||
}
|
||||
return "", fmt.Errorf("兑换失败: %s", result.Msg)
|
||||
return "", &OrderError{Kind: "兑换失败", Code: result.Error, Msg: result.Msg}
|
||||
}
|
||||
var data struct {
|
||||
ExchangeID int `json:"exchangeId"`
|
||||
|
||||
@@ -16,6 +16,18 @@ import (
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
func TestOrderErrorIncludesCode(t *testing.T) {
|
||||
err := (&OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: 1644400007,
|
||||
Msg: "活动火爆,请稍后再试",
|
||||
}).Error()
|
||||
want := "锁定兑换商品失败(error=1644400007): 活动火爆,请稍后再试"
|
||||
if err != want {
|
||||
t.Fatalf("OrderError.Error()=%q, want %q", err, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateCSRFUseCTNReusesCookie(t *testing.T) {
|
||||
var called atomic.Bool
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -181,6 +181,9 @@ func (s *Scheduler) runPeaceLockPay(attempt types.ExchangeAttempt, ctl *runContr
|
||||
if csrf == "" {
|
||||
token, err := cli.GenerateCSRF(attempt.Account.Cookie)
|
||||
if err != nil {
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
}
|
||||
out.result.Message = "失败|生成鉴权token失败: " + err.Error()
|
||||
return out
|
||||
}
|
||||
@@ -214,6 +217,9 @@ func (s *Scheduler) runPeaceLockPay(attempt types.ExchangeAttempt, ctl *runContr
|
||||
if err != nil {
|
||||
out.result.Message = "失败|" + err.Error()
|
||||
kind := lockErrorKind(err)
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
}
|
||||
out.stockOut = kind == orderErrStockOut
|
||||
out.throttled = kind == orderErrThrottled
|
||||
out.terminal = kind == orderErrTerminal
|
||||
@@ -318,6 +324,12 @@ func (s *Scheduler) payLockedOrder(cli *client.DouyuClient, attempt types.Exchan
|
||||
if err == nil {
|
||||
return info, nil
|
||||
}
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
if next := s.clientForAccount(attempt.Account.Name); next != nil {
|
||||
cli = next
|
||||
}
|
||||
}
|
||||
|
||||
kind := payErrorKind(err)
|
||||
if kind == orderErrTerminal {
|
||||
@@ -394,6 +406,9 @@ func (s *Scheduler) runDirectExchange(attempt types.ExchangeAttempt, reuseCache
|
||||
if csrf == "" {
|
||||
token, err := cli.GenerateCSRF(attempt.Account.Cookie)
|
||||
if err != nil {
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
}
|
||||
out.result.Message = "失败|生成鉴权token失败: " + err.Error()
|
||||
return out
|
||||
}
|
||||
@@ -422,6 +437,9 @@ func (s *Scheduler) runDirectExchange(attempt types.ExchangeAttempt, reuseCache
|
||||
out.result.Message = "失败|" + errMsg
|
||||
out.terminal = isTerminalAccountMessage(errMsg)
|
||||
kind := lockErrorKind(err)
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
}
|
||||
out.stockOut = kind == orderErrStockOut
|
||||
out.throttled = kind == orderErrThrottled
|
||||
if isLimitReachedMessage(errMsg) {
|
||||
@@ -525,6 +543,9 @@ func (s *Scheduler) runXiaodianAttempt(attempt types.ExchangeAttempt) types.Exch
|
||||
attempt.Account.Name, attempt.Product.CommodityName, attempt.Index)
|
||||
exchangeInfo, err := cli.ExchangeProduct(attempt.Account.Cookie, attempt.Product, nil)
|
||||
if err != nil {
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
}
|
||||
result.Message = "失败|" + err.Error()
|
||||
return result
|
||||
}
|
||||
@@ -613,6 +634,13 @@ func lockErrorKind(err error) orderErrorKind {
|
||||
return orderErrTransient
|
||||
}
|
||||
|
||||
func isProxyRiskBusinessError(err error) bool {
|
||||
code, msg := orderErrorInfo(err)
|
||||
return code == apiActivityHot ||
|
||||
strings.Contains(msg, "1644400007") ||
|
||||
strings.Contains(msg, "活动火爆")
|
||||
}
|
||||
|
||||
// stockOutHint 判断服务端是否明确表示该商品已售罄。
|
||||
// 只有这一类才允许判定"商品当前不可抢"。
|
||||
func stockOutHint(message string) bool {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -130,10 +131,14 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.StoreHomeURL = timeAPIURL
|
||||
act.BatchLimitURL = timeAPIURL + "/batchExchangeLimit"
|
||||
base := client.New(&act, timeAPIURL, &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
paths := writeAccountFile(t, "a", "b", "c", "d", "e")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
ProxyAPI: apiServer.URL,
|
||||
AutoFetchProxyOnSchedule: true,
|
||||
AccountsPerProxy: 2,
|
||||
@@ -302,6 +307,395 @@ func TestStrictProxyModeDoesNotSwitchDirect(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestActivityHotSwitchesProxyButClickTooFastDoesNot(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
groupClient := &client.DouyuClient{}
|
||||
standbyClient := &client.DouyuClient{}
|
||||
group := &proxyRuntimeGroup{
|
||||
groupIndex: 1,
|
||||
accountNames: []string{"a"},
|
||||
activeURL: "http://127.0.0.1:9000",
|
||||
activeIP: "1.1.1.1",
|
||||
}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": groupClient},
|
||||
activeClients: []*client.DouyuClient{groupClient},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group},
|
||||
standbys: []validatedProxy{{
|
||||
url: "http://127.0.0.1:9100",
|
||||
client: standbyClient,
|
||||
ip: "2.2.2.2",
|
||||
}},
|
||||
allClients: []*client.DouyuClient{groupClient, standbyClient},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
|
||||
scheduler.handleProxyBusinessRisk("a", groupClient, &client.OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: apiClickTooFast,
|
||||
Msg: "您点击太快,请稍等再试",
|
||||
})
|
||||
if scheduler.clientForAccount("a") != groupClient {
|
||||
t.Fatal("click-too-fast should not switch the proxy")
|
||||
}
|
||||
|
||||
scheduler.handleProxyBusinessRisk("a", groupClient, &client.OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: apiActivityHot,
|
||||
Msg: "活动火爆,请稍后再试",
|
||||
})
|
||||
if scheduler.clientForAccount("a") != standbyClient {
|
||||
t.Fatal("activity-hot should immediately switch to standby")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyRiskBusinessErrorMatchesCodeOrMessage(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
err error
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "code only",
|
||||
err: &client.OrderError{Code: apiActivityHot},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "message only",
|
||||
err: errors.New("锁定兑换商品失败: 活动火爆,请稍后再试"),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "numeric code only",
|
||||
err: errors.New("接口失败 error=1644400007"),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "click too fast",
|
||||
err: &client.OrderError{Code: apiClickTooFast, Msg: "您点击太快,请稍等再试"},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := isProxyRiskBusinessError(tc.err); got != tc.want {
|
||||
t.Fatalf("isProxyRiskBusinessError(%v)=%v, want %v", tc.err, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectActivityHotDoesNotSwitchProxy(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
directClient := &client.DouyuClient{}
|
||||
standbyClient := &client.DouyuClient{}
|
||||
group := &proxyRuntimeGroup{
|
||||
groupIndex: 1,
|
||||
accountNames: []string{"direct"},
|
||||
activeURL: "http://127.0.0.1:9000",
|
||||
activeIP: "1.1.1.1",
|
||||
}
|
||||
scheduler.directClient = directClient
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"direct": directClient},
|
||||
activeClients: []*client.DouyuClient{directClient},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{directClient: group},
|
||||
standbys: []validatedProxy{{
|
||||
url: "http://127.0.0.1:9100",
|
||||
client: standbyClient,
|
||||
ip: "2.2.2.2",
|
||||
}},
|
||||
allClients: []*client.DouyuClient{directClient, standbyClient},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
|
||||
scheduler.handleProxyBusinessRisk("direct", directClient, &client.OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: apiActivityHot,
|
||||
Msg: "活动火爆,请稍后再试",
|
||||
})
|
||||
if scheduler.clientForAccount("direct") != directClient {
|
||||
t.Fatal("direct activity-hot must remain direct")
|
||||
}
|
||||
scheduler.clientMu.RLock()
|
||||
standbyCount := len(scheduler.proxyStandbys)
|
||||
scheduler.clientMu.RUnlock()
|
||||
if standbyCount != 1 {
|
||||
t.Fatalf("direct activity-hot consumed standby proxy: remaining=%d", standbyCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestActivityHotSwitchesAllGroupsSharingEgressIP(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
g1, g2 := &client.DouyuClient{}, &client.DouyuClient{}
|
||||
s1, s2 := &client.DouyuClient{}, &client.DouyuClient{}
|
||||
group1 := &proxyRuntimeGroup{groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://127.0.0.1:9001", activeIP: "1.1.1.1"}
|
||||
group2 := &proxyRuntimeGroup{groupIndex: 2, accountNames: []string{"b"}, activeURL: "http://127.0.0.1:9002", activeIP: "1.1.1.1"}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": g1, "b": g2},
|
||||
activeClients: []*client.DouyuClient{g1, g2},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{
|
||||
g1: group1,
|
||||
g2: group2,
|
||||
},
|
||||
standbys: []validatedProxy{
|
||||
{url: "http://127.0.0.1:9101", client: s1, ip: "2.2.2.2"},
|
||||
{url: "http://127.0.0.1:9102", client: s2, ip: "3.3.3.3"},
|
||||
},
|
||||
allClients: []*client.DouyuClient{g1, g2, s1, s2},
|
||||
standbyTarget: 2,
|
||||
})
|
||||
scheduler.handleProxyBusinessRisk("a", g1, &client.OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: apiActivityHot,
|
||||
Msg: "活动火爆,请稍后再试",
|
||||
})
|
||||
if scheduler.clientForAccount("a") == g1 || scheduler.clientForAccount("b") == g2 {
|
||||
t.Fatal("all groups sharing the risky egress IP should switch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyPreflightSwitchesOnActivityHot(t *testing.T) {
|
||||
var requests int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if atomic.AddInt32(&requests, 1) == 1 {
|
||||
fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.BaseURL = server.URL
|
||||
act.BatchLimitURL = server.URL + "/limit"
|
||||
base := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
proxyClient := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
standbyClient := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
group := &proxyRuntimeGroup{groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://127.0.0.1:9000", activeIP: "1.1.1.1"}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": proxyClient},
|
||||
activeClients: []*client.DouyuClient{proxyClient},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{proxyClient: group},
|
||||
standbys: []validatedProxy{{
|
||||
url: "http://127.0.0.1:9100",
|
||||
client: standbyClient,
|
||||
ip: "2.2.2.2",
|
||||
}},
|
||||
allClients: []*client.DouyuClient{proxyClient, standbyClient},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
|
||||
if err := scheduler.preflightProxyBusinessRiskUntil(
|
||||
map[string]string{"a": "goods-1"},
|
||||
time.Now().Add(3*time.Second),
|
||||
); err != nil {
|
||||
t.Fatalf("preflightProxyBusinessRiskUntil() error = %v", err)
|
||||
}
|
||||
if scheduler.clientForAccount("a") != standbyClient {
|
||||
t.Fatal("preflight activity-hot should switch to a different-IP standby")
|
||||
}
|
||||
if !scheduler.proxyBusinessRiskVerifiedSnapshot() {
|
||||
t.Fatal("switched proxy should be rechecked and marked verified")
|
||||
}
|
||||
if got := atomic.LoadInt32(&requests); got != 2 {
|
||||
t.Fatalf("batch limit requests=%d, want 2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyPreflightRechecksEveryGroupAfterSwitch(t *testing.T) {
|
||||
var requests int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if atomic.AddInt32(&requests, 1) <= 2 {
|
||||
fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.BaseURL = server.URL
|
||||
act.BatchLimitURL = server.URL + "/limit"
|
||||
base := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a", "b")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 1,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
active1 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
active2 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
standby1 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
standby2 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
group1 := &proxyRuntimeGroup{
|
||||
groupIndex: 1, accountNames: []string{"a"},
|
||||
activeURL: "http://127.0.0.1:9001", activeIP: "1.1.1.1",
|
||||
}
|
||||
group2 := &proxyRuntimeGroup{
|
||||
groupIndex: 2, accountNames: []string{"b"},
|
||||
activeURL: "http://127.0.0.1:9002", activeIP: "2.2.2.2",
|
||||
}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": active1, "b": active2},
|
||||
activeClients: []*client.DouyuClient{active1, active2},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{
|
||||
active1: group1,
|
||||
active2: group2,
|
||||
},
|
||||
standbys: []validatedProxy{
|
||||
{url: "http://127.0.0.1:9101", client: standby1, ip: "3.3.3.3"},
|
||||
{url: "http://127.0.0.1:9102", client: standby2, ip: "4.4.4.4"},
|
||||
},
|
||||
allClients: []*client.DouyuClient{active1, active2, standby1, standby2},
|
||||
standbyTarget: 2,
|
||||
})
|
||||
|
||||
if err := scheduler.preflightProxyBusinessRiskUntil(
|
||||
map[string]string{"a": "goods-1", "b": "goods-1"},
|
||||
time.Now().Add(3*time.Second),
|
||||
); err != nil {
|
||||
t.Fatalf("preflightProxyBusinessRiskUntil() error = %v", err)
|
||||
}
|
||||
if scheduler.clientForAccount("a") == active1 || scheduler.clientForAccount("b") == active2 {
|
||||
t.Fatal("both activity-hot groups should switch before startup")
|
||||
}
|
||||
if !scheduler.proxyBusinessRiskVerifiedSnapshot() {
|
||||
t.Fatal("both replacement groups should be rechecked and verified")
|
||||
}
|
||||
if got := atomic.LoadInt32(&requests); got != 4 {
|
||||
t.Fatalf("batch limit requests=%d, want 4", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyPreflightExtractsReplacementUntilHealthy(t *testing.T) {
|
||||
safeProxyServers := make([]*httptest.Server, 0, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
safeProxyServers = append(safeProxyServers, httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if strings.Contains(r.URL.Path, "batchExchangeLimit") {
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"goods-1","commodityName":"测试商品"}]}}`)
|
||||
},
|
||||
)))
|
||||
}
|
||||
defer func() {
|
||||
for _, server := range safeProxyServers {
|
||||
server.Close()
|
||||
}
|
||||
}()
|
||||
hotProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if strings.Contains(r.URL.Path, "batchExchangeLimit") {
|
||||
fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"goods-1","commodityName":"测试商品"}]}}`)
|
||||
}))
|
||||
defer hotProxy.Close()
|
||||
|
||||
var apiCalls int32
|
||||
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
index := int(atomic.AddInt32(&apiCalls, 1)-1) % len(safeProxyServers)
|
||||
fmt.Fprint(w, safeProxyServers[index].URL)
|
||||
}))
|
||||
defer apiServer.Close()
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.StoreHomeURL = "http://douyu.test/product"
|
||||
act.BatchLimitURL = "http://douyu.test/batchExchangeLimit"
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
ProxyAPI: apiServer.URL,
|
||||
AccountsPerProxy: 1,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
active1, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, hotProxy.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
standby1, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, hotProxy.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
group := &proxyRuntimeGroup{
|
||||
groupIndex: 1, accountNames: []string{"a"},
|
||||
activeURL: hotProxy.URL, activeIP: "1.1.1.1",
|
||||
}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": active1},
|
||||
activeClients: []*client.DouyuClient{active1},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{active1: group},
|
||||
standbys: []validatedProxy{{
|
||||
url: hotProxy.URL, client: standby1, ip: "2.2.2.2",
|
||||
}},
|
||||
allClients: []*client.DouyuClient{active1, standby1},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
|
||||
if err := scheduler.preflightProxyBusinessRiskUntil(
|
||||
map[string]string{"a": "goods-1"},
|
||||
time.Now().Add(10*time.Second),
|
||||
); err != nil {
|
||||
t.Fatalf("preflightProxyBusinessRiskUntil() error = %v", err)
|
||||
}
|
||||
if scheduler.clientForAccount("a") == active1 || scheduler.clientForAccount("a") == standby1 {
|
||||
t.Fatal("preflight should keep replacing activity-hot proxies until a healthy proxy is active")
|
||||
}
|
||||
if !scheduler.proxyBusinessRiskVerifiedSnapshot() {
|
||||
t.Fatal("replacement proxy should be verified before startup")
|
||||
}
|
||||
if got := atomic.LoadInt32(&apiCalls); got < 2 {
|
||||
t.Fatalf("proxy extraction calls=%d, want at least 2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyStatusReportsGroupAndAccountMapping(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
@@ -375,12 +769,35 @@ func TestProxyRunGateAllowsStandbyShortageWhenActiveGroupsCoverAccounts(t *testi
|
||||
expiresAt: time.Now().Add(time.Minute),
|
||||
})
|
||||
scheduler.setProxyReadiness(true, "")
|
||||
scheduler.setProxyBusinessRiskVerified(true)
|
||||
|
||||
if ok, reason := scheduler.ProxyRunGateFor(map[string]string{"a": "goods-1"}); !ok {
|
||||
t.Fatalf("standby shortage should not block covered active groups: %s", reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyRunGateRequiresBusinessRiskPreflight(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
scheduler.replaceAccountClients(preparedProxyPool{})
|
||||
scheduler.setProxyReadiness(true, "")
|
||||
|
||||
if ok, reason := scheduler.ProxyRunGate(); ok {
|
||||
t.Fatalf("unverified proxy pool should block startup, reason=%q", reason)
|
||||
}
|
||||
scheduler.setProxyBusinessRiskVerified(true)
|
||||
if ok, reason := scheduler.ProxyRunGate(); !ok {
|
||||
t.Fatalf("verified proxy pool should pass startup gate: %s", reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStandbyHealthProbeRemovesRepeatedlyFailingProxy(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
act.StoreHomeURL = "http://douyu.test/time"
|
||||
@@ -445,6 +862,10 @@ func newTimeProxyServer(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if strings.Contains(r.URL.Path, "batchExchangeLimit") {
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"bag-1","commodityName":"测试商品"}]}}`)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
@@ -82,8 +82,11 @@ type Scheduler struct {
|
||||
proxyRefillInFlight bool
|
||||
proxyRefillRetryAt time.Time
|
||||
proxyFallbackActive bool
|
||||
paths types.AppPaths
|
||||
font *truetype.Font
|
||||
// proxyBusinessRiskVerified 表示当前活动代理池已逐组通过只读业务风控预检。
|
||||
// 代理切换、配置变更或池重建后必须重新验证。
|
||||
proxyBusinessRiskVerified bool
|
||||
paths types.AppPaths
|
||||
font *truetype.Font
|
||||
|
||||
successImages *image.SuccessImageSaver
|
||||
|
||||
@@ -150,6 +153,11 @@ type Scheduler struct {
|
||||
|
||||
calibrationMu sync.Mutex
|
||||
calibrationOffsets []time.Duration // 最近N次校准偏移,用于中位数平滑
|
||||
|
||||
drillMu sync.Mutex
|
||||
drillStatus types.SeckillDrillStatus
|
||||
drillCancel chan struct{}
|
||||
drillTrigger time.Time
|
||||
}
|
||||
|
||||
// SetStateChangeCallback 设置运行状态变化回调。
|
||||
@@ -212,6 +220,7 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
|
||||
|
||||
// Close 关闭调度器的后台资源。
|
||||
func (s *Scheduler) Close() {
|
||||
s.CancelSeckillDrill()
|
||||
s.closeAccountClients()
|
||||
s.clientMu.Lock()
|
||||
directClient := s.directClient
|
||||
@@ -449,6 +458,7 @@ func (s *Scheduler) TryStartRun() bool {
|
||||
s.proxyMonitor = make(chan struct{})
|
||||
monitor := s.proxyMonitor
|
||||
s.runMu.Unlock()
|
||||
s.stopSeckillDrillForRealRun()
|
||||
s.clientMu.RLock()
|
||||
generation := s.proxyPoolGeneration
|
||||
s.clientMu.RUnlock()
|
||||
@@ -528,6 +538,7 @@ func (s *Scheduler) ProxyRunGateFor(accountProducts map[string]string) (bool, st
|
||||
dirty := s.accountClientsDirty
|
||||
expired := !s.accountClientsExpireAt.IsZero() &&
|
||||
!time.Now().Before(s.accountClientsExpireAt.Add(-30*time.Second))
|
||||
businessRiskVerified := s.proxyBusinessRiskVerified
|
||||
hasFailedGroup := false
|
||||
for _, group := range s.proxyGroups {
|
||||
if group != nil && group.failed {
|
||||
@@ -545,6 +556,9 @@ func (s *Scheduler) ProxyRunGateFor(accountProducts map[string]string) (bool, st
|
||||
if hasFailedGroup {
|
||||
return false, "代理池存在失败组,等待备用代理"
|
||||
}
|
||||
if !businessRiskVerified {
|
||||
return false, "代理业务风控预检未完成或代理已切换"
|
||||
}
|
||||
names := selectedAccountNames(accountProducts)
|
||||
if len(names) > 0 && !s.accountClientsCover(names) {
|
||||
return false, "代理池未覆盖当前账号、已失效或存在失败组"
|
||||
|
||||
@@ -0,0 +1,352 @@
|
||||
package seckill
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"douyu_go/internal/constants"
|
||||
"douyu_go/internal/logger"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
seckillDrillDelay = 2 * time.Minute
|
||||
seckillDrillWarmupLead = 2 * time.Second
|
||||
)
|
||||
|
||||
// StartSeckillDrill schedules a read-only rehearsal at the simulated T0.
|
||||
func (s *Scheduler) StartSeckillDrill(accountProducts map[string]string) (types.SeckillDrillStatus, error) {
|
||||
return s.startSeckillDrill(accountProducts, seckillDrillDelay)
|
||||
}
|
||||
|
||||
func (s *Scheduler) startSeckillDrill(accountProducts map[string]string, delay time.Duration) (types.SeckillDrillStatus, error) {
|
||||
if delay <= 0 {
|
||||
delay = seckillDrillDelay
|
||||
}
|
||||
if len(accountProducts) == 0 {
|
||||
return types.SeckillDrillStatus{}, errors.New("请选择账号和商品")
|
||||
}
|
||||
if s.IsRunning() {
|
||||
return types.SeckillDrillStatus{}, errors.New("真实抢购正在运行,不能启动抢购预演")
|
||||
}
|
||||
|
||||
tasks := s.buildTasks(accountProducts)
|
||||
if len(tasks) == 0 {
|
||||
return types.SeckillDrillStatus{}, errors.New("没有可执行的预演账号")
|
||||
}
|
||||
|
||||
trigger := time.Now().Add(delay)
|
||||
s.drillMu.Lock()
|
||||
if s.drillCancel != nil {
|
||||
s.drillMu.Unlock()
|
||||
return types.SeckillDrillStatus{}, errors.New("抢购预演已在等待或运行")
|
||||
}
|
||||
cancel := make(chan struct{})
|
||||
s.drillCancel = cancel
|
||||
s.drillStatus = types.SeckillDrillStatus{
|
||||
Active: true,
|
||||
State: "preparing",
|
||||
TotalAccounts: len(tasks),
|
||||
Message: "正在准备预演代理池",
|
||||
}
|
||||
s.drillMu.Unlock()
|
||||
|
||||
if err := s.prepareProxyGroups(accountProducts, false); err != nil {
|
||||
s.finishSeckillDrill("failed", "预演代理准备失败: "+err.Error())
|
||||
return types.SeckillDrillStatus{}, fmt.Errorf("预演代理准备失败: %w", err)
|
||||
}
|
||||
if ok, reason := s.ProxyRunGateFor(accountProducts); !ok {
|
||||
s.finishSeckillDrill("failed", "预演代理未就绪: "+reason)
|
||||
return types.SeckillDrillStatus{}, fmt.Errorf("预演代理未就绪: %s", reason)
|
||||
}
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return types.SeckillDrillStatus{}, errors.New("真实抢购已启动,预演自动终止")
|
||||
}
|
||||
if s.seckillDrillStopped(cancel) {
|
||||
s.finishSeckillDrill("cancelled", "抢购预演已取消")
|
||||
return types.SeckillDrillStatus{}, errors.New("抢购预演已取消")
|
||||
}
|
||||
if !time.Now().Before(trigger) {
|
||||
s.finishSeckillDrill("failed", "代理准备耗时过长,预演触发时间已到")
|
||||
return types.SeckillDrillStatus{}, errors.New("代理准备耗时过长,预演触发时间已到")
|
||||
}
|
||||
|
||||
status := types.SeckillDrillStatus{
|
||||
Active: true,
|
||||
State: "scheduled",
|
||||
TriggerTime: trigger.In(constants.CST).Format("2006-01-02 15:04:05.000"),
|
||||
RemainingSeconds: max(1, int(time.Until(trigger).Round(time.Second)/time.Second)),
|
||||
TotalAccounts: len(tasks),
|
||||
Message: "已准备代理,等待模拟抢购触发",
|
||||
}
|
||||
|
||||
s.drillMu.Lock()
|
||||
s.drillTrigger = trigger
|
||||
s.drillStatus = status
|
||||
s.drillMu.Unlock()
|
||||
|
||||
logger.LogEvent("预演", "success",
|
||||
"抢购预演已设置: 账号=%d,触发=%s;T-120s 代理准备完成",
|
||||
len(tasks), status.TriggerTime)
|
||||
go s.runSeckillDrill(tasks, trigger, cancel)
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (s *Scheduler) GetSeckillDrillStatus() types.SeckillDrillStatus {
|
||||
s.drillMu.Lock()
|
||||
defer s.drillMu.Unlock()
|
||||
status := s.drillStatus
|
||||
if status.Active && !s.drillTrigger.IsZero() {
|
||||
remaining := time.Until(s.drillTrigger)
|
||||
if remaining < 0 {
|
||||
remaining = 0
|
||||
}
|
||||
status.RemainingSeconds = int((remaining + time.Second - 1) / time.Second)
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
func (s *Scheduler) CancelSeckillDrill() bool {
|
||||
s.drillMu.Lock()
|
||||
cancel := s.drillCancel
|
||||
if cancel == nil {
|
||||
s.drillMu.Unlock()
|
||||
return false
|
||||
}
|
||||
s.drillCancel = nil
|
||||
s.drillTrigger = time.Time{}
|
||||
s.drillStatus.Active = false
|
||||
s.drillStatus.State = "cancelled"
|
||||
s.drillStatus.RemainingSeconds = 0
|
||||
s.drillStatus.Message = "抢购预演已取消"
|
||||
close(cancel)
|
||||
s.drillMu.Unlock()
|
||||
logger.LogEvent("预演", "warn", "抢购预演已取消")
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Scheduler) stopSeckillDrillForRealRun() {
|
||||
s.drillMu.Lock()
|
||||
cancel := s.drillCancel
|
||||
if cancel == nil {
|
||||
s.drillMu.Unlock()
|
||||
return
|
||||
}
|
||||
s.drillCancel = nil
|
||||
s.drillTrigger = time.Time{}
|
||||
s.drillStatus.Active = false
|
||||
s.drillStatus.State = "failed"
|
||||
s.drillStatus.RemainingSeconds = 0
|
||||
s.drillStatus.Message = "真实抢购已启动,预演自动终止"
|
||||
close(cancel)
|
||||
s.drillMu.Unlock()
|
||||
logger.LogEvent("预演", "warn", "真实抢购已启动,预演自动终止")
|
||||
}
|
||||
|
||||
func (s *Scheduler) runSeckillDrill(tasks []types.ExchangeAttempt, trigger time.Time, cancel <-chan struct{}) {
|
||||
warmupAt := trigger.Add(-seckillDrillWarmupLead)
|
||||
if !s.waitSeckillDrillUntil(warmupAt, cancel) {
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return
|
||||
}
|
||||
s.finishSeckillDrill("cancelled", "抢购预演已取消")
|
||||
return
|
||||
}
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return
|
||||
}
|
||||
|
||||
s.setSeckillDrillState("warming", "正在执行账号预热")
|
||||
logger.LogEvent("预演", "info", "抢购预演开始预热: 账号=%d", len(tasks))
|
||||
s.warmupAttempts(tasks)
|
||||
|
||||
if !s.waitSeckillDrillUntil(trigger, cancel) {
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return
|
||||
}
|
||||
s.finishSeckillDrill("cancelled", "抢购预演已取消")
|
||||
return
|
||||
}
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return
|
||||
}
|
||||
|
||||
s.setSeckillDrillState("running", "正在按正式并发执行只读抢购请求")
|
||||
logger.LogEvent("预演", "info", "抢购预演触发: 开始执行 %d 个账号", len(tasks))
|
||||
s.runSeckillDrillAccounts(tasks, cancel)
|
||||
}
|
||||
|
||||
func (s *Scheduler) runSeckillDrillAccounts(tasks []types.ExchangeAttempt, cancel <-chan struct{}) {
|
||||
settings := s.settingsSnapshot()
|
||||
limit := make(chan struct{}, max(1, settings.concurrency))
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
mu sync.Mutex
|
||||
)
|
||||
for _, task := range tasks {
|
||||
wg.Add(1)
|
||||
go func(task types.ExchangeAttempt) {
|
||||
defer wg.Done()
|
||||
if s.seckillDrillStopped(cancel) {
|
||||
return
|
||||
}
|
||||
limit <- struct{}{}
|
||||
defer func() { <-limit }()
|
||||
if s.seckillDrillStopped(cancel) {
|
||||
return
|
||||
}
|
||||
|
||||
err := s.runSeckillDrillAccount(task)
|
||||
mu.Lock()
|
||||
status := s.updateSeckillDrillProgress(err)
|
||||
mu.Unlock()
|
||||
if err != nil {
|
||||
logger.LogAccountEvent("预演", "warn", task.Account.Name,
|
||||
"抢购预演失败: 账号=%s 商品=%s err=%v",
|
||||
task.Account.Name, task.Product.CommodityName, err)
|
||||
return
|
||||
}
|
||||
logger.LogAccountEvent("预演", "success", task.Account.Name,
|
||||
"抢购预演通过: 账号=%s 商品=%s 进度=%d/%d",
|
||||
task.Account.Name, task.Product.CommodityName,
|
||||
status.CompletedAccounts, status.TotalAccounts)
|
||||
}(task)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return
|
||||
}
|
||||
if s.seckillDrillStopped(cancel) {
|
||||
s.finishSeckillDrill("cancelled", "抢购预演已取消")
|
||||
return
|
||||
}
|
||||
status := s.GetSeckillDrillStatus()
|
||||
message := fmt.Sprintf("预演完成: 成功 %d/%d,失败 %d",
|
||||
status.SuccessAccounts, status.TotalAccounts, status.FailedAccounts)
|
||||
s.finishSeckillDrill("completed", message)
|
||||
}
|
||||
|
||||
func (s *Scheduler) runSeckillDrillAccount(task types.ExchangeAttempt) error {
|
||||
cli := s.clientForAccount(task.Account.Name)
|
||||
if cli == nil {
|
||||
return errors.New("没有可用代理客户端")
|
||||
}
|
||||
_, csrf := s.cachedAuth(task)
|
||||
switch {
|
||||
case cli.Activity.IsXiaodian:
|
||||
_, err := cli.XiaodianRole(task.Account.Cookie)
|
||||
if err != nil && isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(task.Account.Name, cli, err)
|
||||
}
|
||||
return err
|
||||
case cli.Activity.UseCTN:
|
||||
if _, err := cli.FetchCTN(task.Account.Cookie); err != nil {
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(task.Account.Name, cli, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
_, err := cli.RoleName(task.Account.Cookie)
|
||||
if err != nil && isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(task.Account.Name, cli, err)
|
||||
}
|
||||
return err
|
||||
default:
|
||||
if strings.TrimSpace(csrf) == "" {
|
||||
token, err := cli.GenerateCSRF(task.Account.Cookie)
|
||||
if err != nil {
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(task.Account.Name, cli, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
csrf = token
|
||||
}
|
||||
_, err := cli.BatchExchangeLimit(task.Account.Cookie, task.Product.CommodityID, &csrf)
|
||||
if err != nil && isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(task.Account.Name, cli, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) updateSeckillDrillProgress(err error) types.SeckillDrillStatus {
|
||||
s.drillMu.Lock()
|
||||
defer s.drillMu.Unlock()
|
||||
s.drillStatus.CompletedAccounts++
|
||||
if err != nil {
|
||||
s.drillStatus.FailedAccounts++
|
||||
} else {
|
||||
s.drillStatus.SuccessAccounts++
|
||||
}
|
||||
return s.drillStatus
|
||||
}
|
||||
|
||||
func (s *Scheduler) setSeckillDrillState(state, message string) {
|
||||
s.drillMu.Lock()
|
||||
if s.drillCancel == nil {
|
||||
s.drillMu.Unlock()
|
||||
return
|
||||
}
|
||||
s.drillStatus.State = state
|
||||
s.drillStatus.Message = message
|
||||
s.drillMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Scheduler) finishSeckillDrill(state, message string) {
|
||||
s.drillMu.Lock()
|
||||
if s.drillStatus.Active {
|
||||
s.drillStatus.Active = false
|
||||
s.drillStatus.State = state
|
||||
s.drillStatus.Message = message
|
||||
s.drillStatus.RemainingSeconds = 0
|
||||
}
|
||||
s.drillCancel = nil
|
||||
s.drillTrigger = time.Time{}
|
||||
status := s.drillStatus
|
||||
s.drillMu.Unlock()
|
||||
level := "info"
|
||||
switch {
|
||||
case state == "failed" || status.FailedAccounts > 0:
|
||||
level = "warn"
|
||||
case state == "cancelled":
|
||||
level = "warn"
|
||||
case state == "completed":
|
||||
level = "success"
|
||||
}
|
||||
logger.LogEvent("预演", level, "%s", message)
|
||||
}
|
||||
|
||||
func (s *Scheduler) waitSeckillDrillUntil(target time.Time, cancel <-chan struct{}) bool {
|
||||
wait := time.Until(target)
|
||||
if wait <= 0 {
|
||||
return !s.seckillDrillStopped(cancel)
|
||||
}
|
||||
timer := time.NewTimer(wait)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-cancel:
|
||||
return false
|
||||
case <-timer.C:
|
||||
return !s.seckillDrillStopped(cancel)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) seckillDrillStopped(cancel <-chan struct{}) bool {
|
||||
select {
|
||||
case <-cancel:
|
||||
return true
|
||||
default:
|
||||
return s.IsRunning()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
package seckill
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"douyu_go/internal/activity"
|
||||
"douyu_go/internal/client"
|
||||
"douyu_go/internal/trace"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
func TestSeckillDrillUsesReadOnlyAccountChecks(t *testing.T) {
|
||||
var limitCalls int
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/bind":
|
||||
fmt.Fprint(w, `{"error":0,"data":{"gameRole":{"roleName":"测试角色"}}}`)
|
||||
case "/limit":
|
||||
limitCalls++
|
||||
fmt.Fprint(w, `{"error":0,"data":{"limit":"1"}}`)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.BaseURL = server.URL
|
||||
act.BindInfoURL = server.URL + "/bind"
|
||||
act.BatchLimitURL = server.URL + "/limit"
|
||||
base := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProducts([]types.Product{{
|
||||
CommodityID: "goods-1",
|
||||
CommodityName: "测试商品",
|
||||
}})
|
||||
|
||||
status, err := scheduler.startSeckillDrill(map[string]string{"a": "goods-1"}, 120*time.Millisecond)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !status.Active || status.TotalAccounts != 1 {
|
||||
t.Fatalf("unexpected initial drill status: %+v", status)
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
status = scheduler.GetSeckillDrillStatus()
|
||||
if !status.Active {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if status.Active || status.State != "completed" {
|
||||
t.Fatalf("unexpected final drill status: %+v", status)
|
||||
}
|
||||
if status.SuccessAccounts != 1 || status.FailedAccounts != 0 {
|
||||
t.Fatalf("unexpected drill counts: %+v", status)
|
||||
}
|
||||
if limitCalls != 1 {
|
||||
t.Fatalf("limitCalls=%d, want 1", limitCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeckillDrillSwitchesActivityHotProxyBeforeStart(t *testing.T) {
|
||||
hotProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.URL.Path == "/limit" {
|
||||
fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"data":{"gameRole":{"roleName":"热门代理角色"}}}`)
|
||||
}))
|
||||
defer hotProxy.Close()
|
||||
safeProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/limit":
|
||||
fmt.Fprint(w, `{"error":0,"data":{"limit":"1"}}`)
|
||||
case "/bind":
|
||||
fmt.Fprint(w, `{"error":0,"data":{"gameRole":{"roleName":"备用代理角色"}}}`)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer safeProxy.Close()
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.BindInfoURL = "http://douyu.test/bind"
|
||||
act.BatchLimitURL = "http://douyu.test/limit"
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProducts([]types.Product{{
|
||||
CommodityID: "goods-1",
|
||||
CommodityName: "测试商品",
|
||||
}})
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 1,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
hotClient, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, hotProxy.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
safeClient, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, safeProxy.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
group := &proxyRuntimeGroup{
|
||||
groupIndex: 1, accountNames: []string{"a"},
|
||||
activeURL: hotProxy.URL, activeIP: "1.1.1.1",
|
||||
}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": hotClient},
|
||||
activeClients: []*client.DouyuClient{hotClient},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{hotClient: group},
|
||||
standbys: []validatedProxy{{
|
||||
url: safeProxy.URL, client: safeClient, ip: "2.2.2.2",
|
||||
}},
|
||||
allClients: []*client.DouyuClient{hotClient, safeClient},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
|
||||
status, err := scheduler.startSeckillDrill(map[string]string{"a": "goods-1"}, 120*time.Millisecond)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !status.Active {
|
||||
t.Fatalf("unexpected initial drill status: %+v", status)
|
||||
}
|
||||
if scheduler.clientForAccount("a") != safeClient {
|
||||
t.Fatal("drill preparation should switch activity-hot proxy to standby")
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
status = scheduler.GetSeckillDrillStatus()
|
||||
if !status.Active {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if status.Active || status.State != "completed" || status.FailedAccounts != 0 {
|
||||
t.Fatalf("unexpected final drill status: %+v", status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeckillDrillStopsWhenRealRunStarts(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProducts([]types.Product{{
|
||||
CommodityID: "goods-1",
|
||||
CommodityName: "测试商品",
|
||||
}})
|
||||
|
||||
if _, err := scheduler.startSeckillDrill(map[string]string{"a": "goods-1"}, 300*time.Millisecond); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !scheduler.TryStartRun() {
|
||||
t.Fatal("real run should start")
|
||||
}
|
||||
defer scheduler.finishRun()
|
||||
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
status := scheduler.GetSeckillDrillStatus()
|
||||
if !status.Active {
|
||||
if status.State != "failed" || !strings.Contains(status.Message, "真实抢购已启动") {
|
||||
t.Fatalf("unexpected drill status: %+v", status)
|
||||
}
|
||||
return
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("drill did not stop after real run started")
|
||||
}
|
||||
@@ -27,6 +27,7 @@ type ProxyDrillItem struct {
|
||||
type ProxyDrillResult struct {
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Warning string `json:"warning,omitempty"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
SimulatedAccounts int `json:"simulatedAccounts"`
|
||||
AccountsPerProxy int `json:"accountsPerProxy"`
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package types
|
||||
|
||||
// SeckillDrillStatus is the local read-only seckill rehearsal state.
|
||||
type SeckillDrillStatus struct {
|
||||
Active bool `json:"active"`
|
||||
State string `json:"state"`
|
||||
TriggerTime string `json:"triggerTime,omitempty"`
|
||||
RemainingSeconds int `json:"remainingSeconds"`
|
||||
TotalAccounts int `json:"totalAccounts"`
|
||||
CompletedAccounts int `json:"completedAccounts"`
|
||||
SuccessAccounts int `json:"successAccounts"`
|
||||
FailedAccounts int `json:"failedAccounts"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user