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
+82 -16
View File
@@ -25,11 +25,12 @@ import (
// DouyuClient 斗鱼接口客户端。
type DouyuClient struct {
HTTPClient *http.Client
Activity *activity.Activity
TimeAPIURL string
Trace *trace.Recorder
ProxyURL string
HTTPClient *http.Client
TimeHTTPClient *http.Client
Activity *activity.Activity
TimeAPIURL string
Trace *trace.Recorder
ProxyURL string
xiaodianEmbedMu sync.Mutex
xiaodianEmbedCache map[string]xiaodianEmbedCacheItem
@@ -92,6 +93,7 @@ func NewWithProxy(act *activity.Activity, timeAPIURL string, tr *trace.Recorder,
TimeAPIURL: timeAPIURL,
Trace: tr,
ProxyURL: effectiveProxy,
TimeHTTPClient: newDirectHTTPClient(),
xiaodianEmbedCache: make(map[string]xiaodianEmbedCacheItem),
HTTPClient: &http.Client{
Timeout: 8 * time.Second,
@@ -100,6 +102,14 @@ func NewWithProxy(act *activity.Activity, timeAPIURL string, tr *trace.Recorder,
}, nil
}
func newDirectHTTPClient() *http.Client {
trsp, _ := newHTTPTransport("")
return &http.Client{
Timeout: 8 * time.Second,
Transport: trsp,
}
}
func newHTTPTransport(proxyURL string) (*http.Transport, error) {
trsp := &http.Transport{
MaxConnsPerHost: 120,
@@ -146,6 +156,12 @@ func (c *DouyuClient) ServerTime() (time.Time, error) {
return sample.EstimatedServerNow(), err
}
// ProbeServerTime 使用业务代理客户端访问时间接口,仅用于探测代理连通性。
// 正常时间同步走 TimeHTTPClient 直连,不受代理失效影响。
func (c *DouyuClient) ProbeServerTime() (types.TimeSample, error) {
return c.fetchServerTimeOnceWithClient(c.HTTPClient, "代理探测")
}
// ServerTimeSample 从斗鱼秒级 currentTime 估算毫秒级服务端时间。
func (c *DouyuClient) ServerTimeSample() (types.TimeSample, error) {
var lastErr error
@@ -264,23 +280,32 @@ func betterTimeSample(candidate, current types.TimeSample) bool {
}
func (c *DouyuClient) fetchServerTimeOnce() (types.TimeSample, error) {
return c.fetchServerTimeOnceWithClient(c.timeHTTPClient(), "服务时间")
}
func (c *DouyuClient) timeHTTPClient() *http.Client {
if c.TimeHTTPClient != nil {
return c.TimeHTTPClient
}
return c.HTTPClient
}
func (c *DouyuClient) fetchServerTimeOnceWithClient(httpClient *http.Client, label string) (types.TimeSample, error) {
url := utils.AppendCacheBuster(c.TimeAPIURL)
var result struct {
types.APIResponse
Data struct {
PreExchange struct {
CurrentTime string `json:"currentTime"`
} `json:"preExchange"`
} `json:"data"`
}
start := time.Now()
if err := c.getJSON(url, "", "服务时间", &result); err != nil {
if err := c.getJSONWithClient(url, "", label, &result, httpClient); err != nil {
return types.TimeSample{}, err
}
end := time.Now()
ts, err := strconv.ParseInt(result.Data.PreExchange.CurrentTime, 10, 64)
if err != nil || ts <= 0 {
return types.TimeSample{}, fmt.Errorf("currentTime 无效: %q", result.Data.PreExchange.CurrentTime)
if result.Error != 0 {
return types.TimeSample{}, fmt.Errorf("服务时间接口失败: %s", result.Msg)
}
ts, err := parseServerTimestamp(result.Data)
if err != nil {
return types.TimeSample{}, err
}
rtt := end.Sub(start)
@@ -295,6 +320,36 @@ func (c *DouyuClient) fetchServerTimeOnce() (types.TimeSample, error) {
}, nil
}
func parseServerTimestamp(raw json.RawMessage) (int64, error) {
raw = bytesTrimJSON(raw)
if len(raw) == 0 || string(raw) == "null" {
return 0, errors.New("服务时间响应缺少 data")
}
var timestamp int64
if err := json.Unmarshal(raw, &timestamp); err == nil && timestamp > 0 {
return timestamp, nil
}
var timestampText string
if err := json.Unmarshal(raw, &timestampText); err == nil {
timestamp, err = strconv.ParseInt(strings.TrimSpace(timestampText), 10, 64)
if err == nil && timestamp > 0 {
return timestamp, nil
}
}
var detail struct {
PreExchange struct {
CurrentTime string `json:"currentTime"`
} `json:"preExchange"`
}
if err := json.Unmarshal(raw, &detail); err == nil {
timestamp, err = strconv.ParseInt(strings.TrimSpace(detail.PreExchange.CurrentTime), 10, 64)
if err == nil && timestamp > 0 {
return timestamp, nil
}
}
return 0, fmt.Errorf("服务时间响应中没有有效的 Unix 秒时间戳: %s", utils.TrimForLog(string(raw), 120))
}
// Products 拉取当前活动的商品列表。
func (c *DouyuClient) Products(cookie string, xiaodianOptions types.XiaodianProductListOptions) ([]types.Product, error) {
if c.Activity.IsXiaodian {
@@ -1717,12 +1772,16 @@ func (c *DouyuClient) ExchangeList(cookie string) ([]types.ExchangeRecord, error
}
func (c *DouyuClient) getJSON(url, cookie, label string, out any) error {
return c.getJSONWithClient(url, cookie, label, out, c.HTTPClient)
}
func (c *DouyuClient) getJSONWithClient(url, cookie, label string, out any, httpClient *http.Client) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
c.setBrowserHeaders(req, cookie)
_, err = c.doJSON(req, out, label)
_, err = c.doJSONWithClient(req, out, label, httpClient)
return err
}
@@ -1783,9 +1842,16 @@ func (c *DouyuClient) postMultipart(url, cookie string, csrf *string, label stri
}
func (c *DouyuClient) doJSON(req *http.Request, out any, label string) (*http.Response, error) {
return c.doJSONWithClient(req, out, label, c.HTTPClient)
}
func (c *DouyuClient) doJSONWithClient(req *http.Request, out any, label string, httpClient *http.Client) (*http.Response, error) {
start := time.Now()
reqBody := c.Trace.RequestBody(req)
resp, err := c.HTTPClient.Do(req)
if httpClient == nil {
httpClient = c.HTTPClient
}
resp, err := httpClient.Do(req)
if err != nil {
c.Trace.Record(label, req, 0, time.Since(start), reqBody, nil, err.Error(), 0, "")
c.logNetworkError(label, req, time.Since(start), err)
+56
View File
@@ -2,6 +2,7 @@ package client
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"net/url"
@@ -36,6 +37,61 @@ func TestGenerateCSRFUseCTNReusesCookie(t *testing.T) {
}
}
func TestServerTimeSampleBypassesProxyClient(t *testing.T) {
timeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"error":0,"data":1795103912}`))
}))
defer timeServer.Close()
act := *activity.Get("peace")
cli := &DouyuClient{
HTTPClient: &http.Client{
Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
return nil, errors.New("Proxy Authentication Required")
}),
},
TimeHTTPClient: timeServer.Client(),
Activity: &act,
TimeAPIURL: timeServer.URL,
Trace: &trace.Recorder{},
}
sample, err := cli.ServerTimeSample()
if err != nil {
t.Fatalf("ServerTimeSample() error = %v", err)
}
if sample.RawCurrentTime != 1795103912 {
t.Fatalf("RawCurrentTime=%d, want 1795103912", sample.RawCurrentTime)
}
if _, err := cli.ProbeServerTime(); err == nil {
t.Fatal("ProbeServerTime() should use the proxied business client")
}
}
func TestParseServerTimestamp(t *testing.T) {
cases := []struct {
name string
raw string
want int64
}{
{name: "timestamp number", raw: `1795103912`, want: 1795103912},
{name: "timestamp string", raw: `"1795103912"`, want: 1795103912},
{name: "storedetail compatibility", raw: `{"preExchange":{"currentTime":"1795103912"}}`, want: 1795103912},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := parseServerTimestamp(json.RawMessage(tc.raw))
if err != nil {
t.Fatalf("parseServerTimestamp() error = %v", err)
}
if got != tc.want {
t.Fatalf("parseServerTimestamp() = %d, want %d", got, tc.want)
}
})
}
}
func TestPostMultipartUseCTNPreservesCSRFCookie(t *testing.T) {
var gotCookie string
var gotContentType string
+81
View File
@@ -0,0 +1,81 @@
package client
import (
"context"
"errors"
"io"
"net/http"
"sync"
)
// MonitorProxyHealth 为代理客户端安装传输层健康回调。
// 回调参数为 nil 表示一次完整传输成功;非 nil 表示连接、超时或响应体读取失败。
func MonitorProxyHealth(c *DouyuClient, handler func(error)) {
if c == nil || c.HTTPClient == nil || handler == nil {
return
}
if _, ok := c.HTTPClient.Transport.(*proxyHealthTransport); ok {
return
}
base := c.HTTPClient.Transport
if base == nil {
base = http.DefaultTransport
}
c.HTTPClient.Transport = &proxyHealthTransport{
base: base,
handler: handler,
}
}
// IsProxyTransportFailure 区分真实网络故障与主动取消。
func IsProxyTransportFailure(err error) bool {
if err == nil {
return false
}
return !errors.Is(err, context.Canceled)
}
type proxyHealthTransport struct {
base http.RoundTripper
handler func(error)
}
func (t *proxyHealthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := t.base.RoundTrip(req)
if err != nil {
if IsProxyTransportFailure(err) {
t.handler(err)
}
return nil, err
}
t.handler(nil)
if resp != nil && resp.Body != nil {
resp.Body = &proxyHealthBody{
ReadCloser: resp.Body,
handler: t.handler,
}
}
return resp, nil
}
func (t *proxyHealthTransport) CloseIdleConnections() {
if closer, ok := t.base.(interface{ CloseIdleConnections() }); ok {
closer.CloseIdleConnections()
}
}
type proxyHealthBody struct {
io.ReadCloser
once sync.Once
handler func(error)
}
func (b *proxyHealthBody) Read(p []byte) (int, error) {
n, err := b.ReadCloser.Read(p)
if err != nil && !errors.Is(err, io.EOF) && IsProxyTransportFailure(err) {
b.once.Do(func() {
b.handler(err)
})
}
return n, err
}
+52
View File
@@ -0,0 +1,52 @@
package client
import (
"context"
"errors"
"net/http"
"testing"
)
type roundTripFunc func(*http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
func TestIsProxyTransportFailure(t *testing.T) {
if IsProxyTransportFailure(nil) {
t.Fatal("nil error should not be treated as proxy failure")
}
if IsProxyTransportFailure(context.Canceled) {
t.Fatal("context cancellation should not trigger proxy failover")
}
if !IsProxyTransportFailure(errors.New("connection reset by peer")) {
t.Fatal("transport error should trigger proxy failover")
}
}
func TestMonitorProxyHealthReportsRoundTripFailure(t *testing.T) {
wantErr := errors.New("proxy connection refused")
cli := &DouyuClient{
HTTPClient: &http.Client{
Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
return nil, wantErr
}),
},
}
var gotErr error
MonitorProxyHealth(cli, func(err error) {
gotErr = err
})
req, err := http.NewRequest(http.MethodGet, "https://example.test", nil)
if err != nil {
t.Fatal(err)
}
if _, err := cli.HTTPClient.Do(req); !errors.Is(err, wantErr) {
t.Fatalf("HTTPClient.Do() error=%v, want %v", err, wantErr)
}
if !errors.Is(gotErr, wantErr) {
t.Fatalf("health handler error=%v, want %v", gotErr, wantErr)
}
}