feat: 完善代理池与抢购链路,发布 0.5.0
This commit is contained in:
+82
-16
@@ -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, ×tamp); err == nil && timestamp > 0 {
|
||||
return timestamp, nil
|
||||
}
|
||||
var timestampText string
|
||||
if err := json.Unmarshal(raw, ×tampText); 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)
|
||||
|
||||
Reference in New Issue
Block a user