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:
yml2213
2026-10-24 19:26:16 +08:00
parent 288109de2f
commit bc366fd6b9
12 changed files with 889 additions and 33 deletions
+38
View File
@@ -7,6 +7,7 @@ import (
"sync/atomic"
"time"
"douyu_go/internal/client"
"douyu_go/internal/constants"
"douyu_go/internal/logger"
"douyu_go/internal/types"
@@ -46,6 +47,39 @@ func (s *Scheduler) ScheduleRun(req types.SeckillRequest) (types.ScheduleResult,
s.scheduleTimers(ap, targetTime, triggerTime, calibration, req.Attempts, req.DelayMs, false)
s.proxyMu.RLock()
autoFetch := s.autoFetchProxyOnSchedule
apiURL := s.proxyAPI
s.proxyMu.RUnlock()
if autoFetch && apiURL != "" {
proxyFetchRemain := triggerTime.Sub(serverTime) - proxyFetchBeforeTrigger
if proxyFetchRemain < 0 {
proxyFetchRemain = 0
}
s.scheduleMu.Lock()
s.replaceTimerLocked(&s.proxyFetchSchedule, proxyFetchRemain, func() {
if s.IsRunning() {
return
}
logger.LogEvent("代理", "info", "定时任务触发前,自动从 API 提取最新独立代理...")
res, err := client.FetchProxyFromAPI(apiURL)
if err != nil {
logger.LogEvent("代理", "warn", "定时自动提取代理失败: %v,继续使用现有网络通道", err)
return
}
cli := s.clientSnapshot()
if cli != nil {
if err := cli.SetProxy(res.ProxyURL); err != nil {
logger.LogEvent("代理", "warn", "应用提取代理失败: %v", err)
} else {
logger.LogEvent("代理", "success", "定时自动应用最新代理: %s (IP=%s 归属=%s)", res.ProxyURL, res.IP, res.Region)
}
}
})
s.scheduleMu.Unlock()
}
resyncRemain := triggerTime.Sub(serverTime) - resyncBeforeTrigger
if resyncRemain < 0 {
resyncRemain = 0
@@ -213,4 +247,8 @@ func (s *Scheduler) stopSchedulesLocked() {
s.resyncSchedule.Stop()
s.resyncSchedule = nil
}
if s.proxyFetchSchedule != nil {
s.proxyFetchSchedule.Stop()
s.proxyFetchSchedule = nil
}
}
+32 -14
View File
@@ -21,15 +21,16 @@ import (
)
const (
warmupBeforeTrigger = 2 * time.Second
warmupMaxAge = 30 * time.Second
qualifiedMaxAge = 30 * time.Second
timeCalibrateRounds = 3
timeCalibrateWait = 1200 * time.Millisecond
resyncBeforeTrigger = 3 * time.Second
calibrationHistorySize = 3
maxCooldownCount = 5
xiaodianDefaultDelay = 500 * time.Millisecond
warmupBeforeTrigger = 2 * time.Second
warmupMaxAge = 30 * time.Second
qualifiedMaxAge = 30 * time.Second
timeCalibrateRounds = 3
timeCalibrateWait = 1200 * time.Millisecond
resyncBeforeTrigger = 8 * time.Second
proxyFetchBeforeTrigger = 15 * time.Second
calibrationHistorySize = 3
maxCooldownCount = 5
xiaodianDefaultDelay = 500 * time.Millisecond
)
// seckillMaxDuration 是手册类活动的抢购窗口:超过后不再发起新的锁单。
@@ -89,11 +90,18 @@ type Scheduler struct {
// pendingClient 保存运行中收到的客户端更新,当前任务结束后自动应用。
pendingClient *client.DouyuClient
scheduleMu sync.Mutex
schedule *time.Timer
warmupSchedule *time.Timer
resyncSchedule *time.Timer
warmupGen int64
scheduleMu sync.Mutex
schedule *time.Timer
warmupSchedule *time.Timer
resyncSchedule *time.Timer
proxyFetchSchedule *time.Timer
warmupGen int64
proxyMu sync.RWMutex
proxyEnabled bool
proxyURL string
proxyAPI string
autoFetchProxyOnSchedule bool
warmupsMu sync.RWMutex
warmups map[string]types.WarmupData
@@ -238,6 +246,16 @@ func (s *Scheduler) UpdateSettings(concurrency, attempts int, retryStages []type
s.seckillMode = normalizeSeckillMode(seckillMode)
}
// UpdateProxySettings 运行时更新代理配置。
func (s *Scheduler) UpdateProxySettings(enabled bool, proxyURL, proxyAPI string, autoFetch bool) {
s.proxyMu.Lock()
defer s.proxyMu.Unlock()
s.proxyEnabled = enabled
s.proxyURL = proxyURL
s.proxyAPI = proxyAPI
s.autoFetchProxyOnSchedule = autoFetch
}
func (s *Scheduler) settingsSnapshot() runSettings {
s.settingsMu.RLock()
settings := runSettings{
+1 -1
View File
@@ -72,7 +72,7 @@ func newOrderTestScheduler(t *testing.T, handler http.HandlerFunc) *Scheduler {
act.CSRFURL = server.URL + "/csrf"
act.ScoreURL = server.URL + "/score"
act.ScoreMethod = "POST"
cli := client.New(&act, "", &trace.Recorder{})
cli := client.New(&act, "", &trace.Recorder{}, "")
cli.HTTPClient = server.Client()
return New(cli, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
}