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

This commit is contained in:
yml
2026-11-22 14:52:46 +08:00
parent 70612a8f0c
commit e20907800d
20 changed files with 1916 additions and 40 deletions
+422 -1
View File
@@ -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)