fix(seckill): 代理失败时快速回退直连,发布 0.5.3

This commit is contained in:
yml
2026-11-20 13:37:00 +08:00
parent f4b3cfa486
commit 79b503d1a6
5 changed files with 224 additions and 21 deletions
+65
View File
@@ -153,6 +153,71 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
}
}
func TestPrepareProxyGroupsFallsBackDirectAndSkipsRepeatedWait(t *testing.T) {
var requests int32
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&requests, 1)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"code":-1,"status":200,"message":"secret密匙错误"}`)
}))
defer apiServer.Close()
act := *activity.Get("peace")
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "http://127.0.0.1:1")
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
scheduler.UpdateProxySettings(types.Config{
ProxyEnabled: true,
ProxyAPI: apiServer.URL,
AccountsPerProxy: 5,
ProxySparePercent: 25,
})
accountProducts := map[string]string{"a": "goods-1"}
if err := scheduler.prepareProxyGroups(accountProducts, false); err != nil {
t.Fatalf("prepareProxyGroups() error = %v", err)
}
if !scheduler.proxyFallbackSnapshot() {
t.Fatal("代理失败后应启用直连回退")
}
if scheduler.clientForAccount("a") != scheduler.directClient {
t.Fatal("代理失败后账号应路由到真正的直连客户端")
}
firstRequests := atomic.LoadInt32(&requests)
if firstRequests == 0 {
t.Fatal("首次代理准备应尝试提取")
}
if err := scheduler.prepareProxyGroups(accountProducts, false); err != nil {
t.Fatalf("second prepareProxyGroups() error = %v", err)
}
if got := atomic.LoadInt32(&requests); got != firstRequests {
t.Fatalf("直连回退后不应重复等待提取,requests=%d want %d", got, firstRequests)
}
}
func TestProxyGroupSwitchesDirectWhenStandbyUnavailable(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)
groupClient := &client.DouyuClient{}
group := &proxyRuntimeGroup{
accountNames: []string{"a"},
activeURL: "http://user:pass@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},
})
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("备用代理为空时,故障组应立即切换到直连")
}
}
func newTimeProxyServer(t *testing.T) *httptest.Server {
t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {