feat(proxy): 支持多实例独立代理与短效API动态提取
- 实例配置独立:支持为每个实例单独配置 http/https/socks5 代理,避免单机多开挤兑触发斗鱼出口 IP 限流 - 动态 API 提取:原生支持星空代理等平台的 API 提取(兼容 JSON/文本格式),提供前端一键提取填入与连通性/延迟测速 - 定时前自动拉取:针对 3 分钟短效代理,支持在开抢前 15 秒自动提取并绑定最新 IP,并在预热前完成热连接与时间复校 - 修复复校竞态:将 resyncBeforeTrigger 从 3s 提升至 8s,确保三轮采样在开抢前顺利落盘,消除时钟漂移 - 验证方式:通过 go test ./... 全量测试与前端 vue-tsc/vite 编译,实测通过星空代理 API 解析测试用例
This commit is contained in:
@@ -29,6 +29,7 @@ type DouyuClient struct {
|
||||
Activity *activity.Activity
|
||||
TimeAPIURL string
|
||||
Trace *trace.Recorder
|
||||
ProxyURL string
|
||||
|
||||
xiaodianEmbedMu sync.Mutex
|
||||
xiaodianEmbedCache map[string]xiaodianEmbedCacheItem
|
||||
@@ -56,8 +57,9 @@ type xiaodianEmbedCacheItem struct {
|
||||
}
|
||||
|
||||
// New 创建斗鱼客户端。act 决定使用哪个活动的接口参数;
|
||||
// timeAPIURL 为空时回退到 constants.DefaultTimeAPI(全局共享的服务端时间接口)。
|
||||
func New(act *activity.Activity, timeAPIURL string, tr *trace.Recorder) *DouyuClient {
|
||||
// timeAPIURL 为空时回退到 constants.DefaultTimeAPI(全局共享的服务端时间接口);
|
||||
// proxyURL 为可选参数,为空时直连,非空时配置 http/https/socks5 代理。
|
||||
func New(act *activity.Activity, timeAPIURL string, tr *trace.Recorder, proxyURL ...string) *DouyuClient {
|
||||
if act == nil {
|
||||
act = activity.Default()
|
||||
}
|
||||
@@ -67,23 +69,56 @@ func New(act *activity.Activity, timeAPIURL string, tr *trace.Recorder) *DouyuCl
|
||||
if tr == nil {
|
||||
tr = &trace.Recorder{}
|
||||
}
|
||||
var effectiveProxy string
|
||||
if len(proxyURL) > 0 {
|
||||
effectiveProxy = proxyURL[0]
|
||||
}
|
||||
trsp := &http.Transport{
|
||||
MaxConnsPerHost: 120,
|
||||
MaxIdleConns: 120,
|
||||
MaxIdleConnsPerHost: 80,
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
}
|
||||
if effectiveProxy != "" {
|
||||
if u, err := ParseProxyURL(effectiveProxy); err == nil && u != nil {
|
||||
trsp.Proxy = http.ProxyURL(u)
|
||||
}
|
||||
}
|
||||
return &DouyuClient{
|
||||
Activity: act,
|
||||
TimeAPIURL: timeAPIURL,
|
||||
Trace: tr,
|
||||
ProxyURL: effectiveProxy,
|
||||
xiaodianEmbedCache: make(map[string]xiaodianEmbedCacheItem),
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: 8 * time.Second,
|
||||
Transport: &http.Transport{
|
||||
MaxConnsPerHost: 120,
|
||||
MaxIdleConns: 120,
|
||||
MaxIdleConnsPerHost: 80,
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
},
|
||||
Timeout: 8 * time.Second,
|
||||
Transport: trsp,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// SetProxy 运行时更新客户端使用的代理。
|
||||
func (c *DouyuClient) SetProxy(proxyURL string) error {
|
||||
var proxyFn func(*http.Request) (*url.URL, error)
|
||||
if strings.TrimSpace(proxyURL) != "" {
|
||||
u, err := ParseProxyURL(proxyURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proxyFn = http.ProxyURL(u)
|
||||
}
|
||||
|
||||
c.ProxyURL = proxyURL
|
||||
if c.HTTPClient != nil {
|
||||
c.HTTPClient.CloseIdleConnections()
|
||||
if trsp, ok := c.HTTPClient.Transport.(*http.Transport); ok {
|
||||
trsp.Proxy = proxyFn
|
||||
trsp.CloseIdleConnections()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ServerTime 从商品详情接口读取 data.preExchange.currentTime。
|
||||
func (c *DouyuClient) ServerTime() (time.Time, error) {
|
||||
sample, err := c.ServerTimeSample()
|
||||
|
||||
Reference in New Issue
Block a user