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

- 备用不足不再触发活动代理池重建\n- 白名单 verify 限频错误保留消息并允许重试\n- 严格模式失败组暂停请求并持续补提取\n- 增加备用代理健康探测与运行门禁校验
This commit is contained in:
yml
2026-11-21 22:01:48 +08:00
parent d37fd50233
commit 5b2dc22eb5
9 changed files with 440 additions and 26 deletions
+89 -3
View File
@@ -85,14 +85,26 @@ func TestAccountClientsRespectExpiryAndSettingsRevision(t *testing.T) {
accountClients: make(map[string]*client.DouyuClient),
}
groupClient := &client.DouyuClient{}
group := &proxyRuntimeGroup{
groupIndex: 1,
accountNames: []string{"a"},
activeURL: "http://127.0.0.1:9000",
}
scheduler.replaceAccountClients(preparedProxyPool{
accountClients: map[string]*client.DouyuClient{"a": groupClient},
activeClients: []*client.DouyuClient{groupClient},
groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group},
expiresAt: time.Now().Add(time.Minute),
standbyTarget: 5,
})
if !scheduler.accountClientsCover([]string{"a"}) {
t.Fatal("未过期代理组应可复用")
t.Fatal("备用不足不应使已覆盖账号的活动代理失效")
}
group.failed = true
if scheduler.accountClientsCover([]string{"a"}) {
t.Fatal("失败代理组不应被判定为可复用")
}
group.failed = false
scheduler.UpdateProxySettings(types.Config{
ProxyEnabled: true,
AutoFetchProxyOnSchedule: true,
@@ -264,16 +276,30 @@ func TestStrictProxyModeDoesNotSwitchDirect(t *testing.T) {
url: group.activeURL,
client: groupClient,
}})
scheduler.setProxyReadiness(true, "")
proxyErr := errors.New("read tcp: connection reset by peer")
scheduler.recordProxyResult(groupClient, proxyErr)
scheduler.recordProxyResult(groupClient, proxyErr)
if scheduler.clientForAccount("a") == scheduler.directClient {
t.Fatal("strict proxy mode must not switch to direct")
if scheduler.clientForAccount("a") != nil {
t.Fatal("failed strict proxy group should pause account requests")
}
if !group.failed {
t.Fatal("failed group should remain tracked for standby refill")
}
if !scheduler.accountProxyPaused("a") {
t.Fatal("account should be reported as paused")
}
if ok, _ := scheduler.ProxyRunGateFor(map[string]string{"a": "goods-1"}); ok {
t.Fatal("run gate should reject a failed proxy group")
}
res := scheduler.runAccountAttempt(types.ExchangeAttempt{
Account: types.Account{Name: "a"},
Product: types.Product{CommodityName: "测试商品"},
}, types.SeckillModeAggressive, nil)
if !res.paused || res.result.Message != "等待备用代理" {
t.Fatalf("unexpected paused attempt result: %+v", res)
}
}
func TestProxyStatusReportsGroupAndAccountMapping(t *testing.T) {
@@ -324,6 +350,66 @@ func TestProxyStatusReportsGroupAndAccountMapping(t *testing.T) {
}
}
func TestProxyRunGateAllowsStandbyShortageWhenActiveGroupsCoverAccounts(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{}
group := &proxyRuntimeGroup{
groupIndex: 1,
accountNames: []string{"a"},
activeURL: "http://127.0.0.1:9000",
}
scheduler.replaceAccountClients(preparedProxyPool{
accountClients: map[string]*client.DouyuClient{"a": groupClient},
activeClients: []*client.DouyuClient{groupClient},
groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group},
allClients: []*client.DouyuClient{groupClient},
standbyTarget: 5,
expiresAt: time.Now().Add(time.Minute),
})
scheduler.setProxyReadiness(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 TestStandbyHealthProbeRemovesRepeatedlyFailingProxy(t *testing.T) {
act := *activity.Get("peace")
act.StoreHomeURL = "http://douyu.test/time"
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
badClient, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, "http://127.0.0.1:1")
if err != nil {
t.Fatal(err)
}
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
scheduler.proxyStandbys = []validatedProxy{{
url: "http://127.0.0.1:1",
client: badClient,
}}
scheduler.proxyClientPool = []*client.DouyuClient{badClient}
scheduler.proxyStandbyTarget = 1
for attempt := 0; attempt < proxyStandbyFailLimit; attempt++ {
if !scheduler.probeProxyStandbys(scheduler.proxyPoolGeneration) {
t.Fatal("standby probe should keep monitoring current generation")
}
}
if len(scheduler.proxyStandbys) != 0 {
t.Fatalf("standby count=%d, want 0", len(scheduler.proxyStandbys))
}
if len(scheduler.proxyClientPool) != 0 {
t.Fatalf("client pool count=%d, want 0", len(scheduler.proxyClientPool))
}
}
func TestStrictPrepareFailureBlocksRun(t *testing.T) {
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")