feat: 完善代理池与抢购链路,发布 0.5.0

This commit is contained in:
yml
2026-11-20 00:48:21 +08:00
parent 2f087ac7f1
commit 98b3dc4b16
28 changed files with 1522 additions and 204 deletions
+50 -7
View File
@@ -1,6 +1,7 @@
package seckill
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
@@ -41,17 +42,35 @@ func TestProxyPoolExpiresAtUsesMinuteParameter(t *testing.T) {
}
}
func TestProxyStandbyCount(t *testing.T) {
cases := []struct {
active int
percent int
want int
}{
{active: 1, percent: 25, want: 1},
{active: 6, percent: 25, want: 2},
{active: 10, percent: 25, want: 3},
{active: 6, percent: 1, want: 1},
}
for _, tc := range cases {
if got := proxyStandbyCount(tc.active, tc.percent); got != tc.want {
t.Errorf("proxyStandbyCount(%d, %d)=%d, want %d", tc.active, tc.percent, got, tc.want)
}
}
}
func TestAccountClientsRespectExpiryAndSettingsRevision(t *testing.T) {
scheduler := &Scheduler{
client: &client.DouyuClient{},
accountClients: make(map[string]*client.DouyuClient),
}
groupClient := &client.DouyuClient{}
scheduler.replaceAccountClients(
map[string]*client.DouyuClient{"a": groupClient},
[]*client.DouyuClient{groupClient},
time.Now().Add(time.Minute),
)
scheduler.replaceAccountClients(preparedProxyPool{
accountClients: map[string]*client.DouyuClient{"a": groupClient},
activeClients: []*client.DouyuClient{groupClient},
expiresAt: time.Now().Add(time.Minute),
})
if !scheduler.accountClientsCover([]string{"a"}) {
t.Fatal("未过期代理组应可复用")
}
@@ -67,8 +86,8 @@ func TestAccountClientsRespectExpiryAndSettingsRevision(t *testing.T) {
func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
timeAPIURL := "http://douyu.test/time"
proxyServers := make([]*httptest.Server, 0, 3)
for i := 0; i < 3; i++ {
proxyServers := make([]*httptest.Server, 0, 5)
for i := 0; i < 5; i++ {
proxyServers = append(proxyServers, newTimeProxyServer(t))
}
var next int32
@@ -86,6 +105,7 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
ProxyAPI: apiServer.URL,
AutoFetchProxyOnSchedule: true,
AccountsPerProxy: 2,
ProxySparePercent: 25,
})
accountProducts := make(map[string]string)
@@ -108,6 +128,29 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
if scheduler.clientForAccount("e") == scheduler.clientForAccount("a") {
t.Fatal("e 应使用独立代理客户端")
}
if len(scheduler.proxyStandbys) != 1 {
t.Fatalf("备用代理=%d, want 1", len(scheduler.proxyStandbys))
}
standbyClient := scheduler.proxyStandbys[0].client
scheduler.UpdateProxySettings(types.Config{
ProxyEnabled: true,
ProxyURL: "http://127.0.0.1:1",
AccountsPerProxy: 2,
ProxySparePercent: 25,
})
proxyErr := errors.New("read tcp: connection reset by peer")
scheduler.recordProxyResult(scheduler.clientForAccount("a"), proxyErr)
if scheduler.clientForAccount("a") == standbyClient {
t.Fatal("单次网络异常不应立即切换备用代理")
}
scheduler.recordProxyResult(scheduler.clientForAccount("a"), proxyErr)
if scheduler.clientForAccount("a") != standbyClient || scheduler.clientForAccount("b") != standbyClient {
t.Fatal("连续网络异常后整组账号应切换到备用代理")
}
if scheduler.clientForAccount("c") == standbyClient {
t.Fatal("其他代理组不应被错误切换")
}
}
func newTimeProxyServer(t *testing.T) *httptest.Server {