feat: 完善代理池与抢购链路,发布 0.5.0
This commit is contained in:
+100
-27
@@ -26,10 +26,11 @@ import (
|
||||
type App struct {
|
||||
ctx context.Context
|
||||
|
||||
clientMu sync.RWMutex
|
||||
client *client.DouyuClient
|
||||
scheduler *seckill.Scheduler
|
||||
logStore *logger.MemoryLogStore
|
||||
clientMu sync.RWMutex
|
||||
client *client.DouyuClient
|
||||
schedulerMu sync.RWMutex
|
||||
scheduler *seckill.Scheduler
|
||||
logStore *logger.MemoryLogStore
|
||||
|
||||
traceMu sync.RWMutex
|
||||
trace *trace.Recorder
|
||||
@@ -81,6 +82,18 @@ func (a *App) setClient(c *client.DouyuClient) {
|
||||
a.clientMu.Unlock()
|
||||
}
|
||||
|
||||
func (a *App) getScheduler() *seckill.Scheduler {
|
||||
a.schedulerMu.RLock()
|
||||
defer a.schedulerMu.RUnlock()
|
||||
return a.scheduler
|
||||
}
|
||||
|
||||
func (a *App) setScheduler(s *seckill.Scheduler) {
|
||||
a.schedulerMu.Lock()
|
||||
a.scheduler = s
|
||||
a.schedulerMu.Unlock()
|
||||
}
|
||||
|
||||
// setCfg 替换配置(写锁保护)。
|
||||
func (a *App) setCfg(c types.Config) {
|
||||
a.cfgMu.Lock()
|
||||
@@ -167,53 +180,113 @@ func (a *App) Startup(ctx context.Context) {
|
||||
logger.LogEvent("配置", "success", "已迁移旧配置到应用内部目录: %s", a.paths.ConfigFile)
|
||||
}
|
||||
|
||||
// 加载配置并应用运行时设置
|
||||
a.cfg = config.LoadConfig(a.paths.ConfigFile)
|
||||
logger.SetMinLevel(a.cfg.LogLevel)
|
||||
// 优先初始化调度器和账号库,避免前端首屏请求早于启动网络探测时读到 nil。
|
||||
cfg := config.LoadConfig(a.paths.ConfigFile)
|
||||
a.setCfg(cfg)
|
||||
logger.SetMinLevel(cfg.LogLevel)
|
||||
a.replaceTrace(cfg)
|
||||
|
||||
a.replaceTrace(a.cfg)
|
||||
|
||||
// 解析当前活动:配置中的 activity ID 决定使用哪套接口参数
|
||||
act := activity.Get(a.cfg.Activity)
|
||||
act := activity.Get(cfg.Activity)
|
||||
if act == nil {
|
||||
act = activity.Default()
|
||||
logger.LogEvent("活动", "warn", "配置中的活动 %q 未识别,已回退到默认活动 %s", a.cfg.Activity, act.DisplayName)
|
||||
logger.LogEvent("活动", "warn", "配置中的活动 %q 未识别,已回退到默认活动 %s", cfg.Activity, act.DisplayName)
|
||||
}
|
||||
proxyURL := a.effectiveProxyURL(a.cfg)
|
||||
a.client, _ = client.NewWithProxy(act, a.cfg.TimeAPIURL, a.getTrace(), proxyURL)
|
||||
if a.client == nil {
|
||||
|
||||
proxyURL := a.effectiveProxyURL(cfg)
|
||||
initialClient, _ := client.NewWithProxy(act, cfg.TimeAPIURL, a.getTrace(), proxyURL)
|
||||
if initialClient == nil {
|
||||
logger.LogEvent("代理", "error", "启动时代理配置无效,已回退直连: %s", client.MaskProxyURL(proxyURL))
|
||||
a.client, _ = client.NewWithProxy(act, a.cfg.TimeAPIURL, a.getTrace(), "")
|
||||
initialClient, _ = client.NewWithProxy(act, cfg.TimeAPIURL, a.getTrace(), "")
|
||||
}
|
||||
a.setClient(initialClient)
|
||||
if proxyURL != "" {
|
||||
logger.LogEvent("代理", "info", "已启用代理: %s", client.MaskProxyURL(proxyURL))
|
||||
}
|
||||
|
||||
a.font = imgpkg.LoadFont(a.paths.FontFiles)
|
||||
a.scheduler = seckill.New(a.client, a.paths, a.font, a.cfg.Concurrency, a.cfg.Attempts, a.cfg.RetryStages, a.cfg.SeckillMode)
|
||||
a.scheduler.UpdateProxySettings(a.cfg)
|
||||
a.scheduler.SetStateChangeCallback(func(state string) {
|
||||
scheduler := seckill.New(initialClient, a.paths, a.font, cfg.Concurrency, cfg.Attempts, cfg.RetryStages, cfg.SeckillMode)
|
||||
a.setScheduler(scheduler)
|
||||
scheduler.UpdateProxySettings(cfg)
|
||||
scheduler.SetStateChangeCallback(func(state string) {
|
||||
runtime.EventsEmit(a.ctx, seckillStateEvent, state)
|
||||
})
|
||||
a.scheduler.LoadAccounts()
|
||||
scheduler.LoadAccounts()
|
||||
|
||||
startupRefreshTried := false
|
||||
tryRefreshStartupProxy := func(reason string) bool {
|
||||
if startupRefreshTried {
|
||||
return false
|
||||
}
|
||||
startupRefreshTried = true
|
||||
|
||||
currentCfg := a.getCfg()
|
||||
refreshedCfg := currentCfg
|
||||
if err := a.refreshStartupProxy(&refreshedCfg, reason); err != nil {
|
||||
logger.LogEvent("代理", "warn", "启动自动刷新代理失败: %v", err)
|
||||
return false
|
||||
}
|
||||
candidate, err := client.NewWithProxy(act, refreshedCfg.TimeAPIURL, a.getTrace(), a.effectiveProxyURL(refreshedCfg))
|
||||
if err != nil {
|
||||
logger.LogEvent("代理", "warn", "启动自动刷新代理地址无效: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
oldClient := a.getClient()
|
||||
a.setClient(candidate)
|
||||
a.setCfg(refreshedCfg)
|
||||
scheduler.UpdateClient(candidate)
|
||||
scheduler.UpdateProxySettings(refreshedCfg)
|
||||
if oldClient != nil && oldClient.HTTPClient != nil {
|
||||
oldClient.HTTPClient.CloseIdleConnections()
|
||||
}
|
||||
if oldClient != nil && oldClient.TimeHTTPClient != nil {
|
||||
oldClient.TimeHTTPClient.CloseIdleConnections()
|
||||
}
|
||||
if err := config.SaveConfig(a.paths.ConfigFile, refreshedCfg); err != nil {
|
||||
logger.LogEvent("代理", "warn", "启动自动刷新代理已生效,但配置保存失败: %v", err)
|
||||
}
|
||||
logger.LogEvent("代理", "success", "启动自动刷新代理已生效: %s",
|
||||
client.MaskProxyURL(a.effectiveProxyURL(refreshedCfg)))
|
||||
return true
|
||||
}
|
||||
|
||||
if proxyNeedsStartupRefresh(a.getCfg(), time.Now()) {
|
||||
tryRefreshStartupProxy("当前代理已过期")
|
||||
}
|
||||
|
||||
proxyURL = a.effectiveProxyURL(a.getCfg())
|
||||
if proxyURL != "" {
|
||||
if _, err := a.getClient().ProbeServerTime(); err != nil {
|
||||
logger.LogEvent("代理", "warn", "启动代理探测失败: %v", err)
|
||||
currentCfg := a.getCfg()
|
||||
if currentCfg.ProxyEnabled &&
|
||||
strings.TrimSpace(currentCfg.ProxyAPI) != "" &&
|
||||
isStartupProxyFailure(err) {
|
||||
tryRefreshStartupProxy("当前代理连接失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
startupSample, startupErr := a.getClient().ServerTimeSample()
|
||||
|
||||
if err := os.MkdirAll(a.paths.SuccessDir, 0755); err != nil {
|
||||
logger.LogEvent("图片", "warn", "成功图片目录创建失败,图片保存功能可能不可用: %v", err)
|
||||
}
|
||||
|
||||
if sample, err := a.client.ServerTimeSample(); err == nil {
|
||||
logger.SetDouyuOffset(sample.Offset)
|
||||
logger.LogEvent("时间", "success", "服务端时间获取成功: %s", sample.EstimatedServerNow().In(constants.CST).Format("2006-01-02 15:04:05.000"))
|
||||
if startupErr == nil {
|
||||
logger.SetDouyuOffset(startupSample.Offset)
|
||||
logger.LogEvent("时间", "success", "服务端时间获取成功: %s", startupSample.EstimatedServerNow().In(constants.CST).Format("2006-01-02 15:04:05.000"))
|
||||
} else {
|
||||
logger.LogEvent("时间", "error", "服务端时间获取失败: %v", err)
|
||||
logger.LogEvent("时间", "error", "服务端时间获取失败: %v", startupErr)
|
||||
}
|
||||
|
||||
logger.LogEvent("系统", "success", "应用启动完成 | 账号: %d", a.scheduler.AccountCount())
|
||||
logger.LogEvent("系统", "success", "应用启动完成 | 账号: %d", scheduler.AccountCount())
|
||||
}
|
||||
|
||||
// Shutdown Wails 应用关闭时调用。
|
||||
func (a *App) Shutdown(ctx context.Context) {
|
||||
if a.scheduler != nil {
|
||||
a.scheduler.Close()
|
||||
if scheduler := a.getScheduler(); scheduler != nil {
|
||||
scheduler.Close()
|
||||
}
|
||||
a.traceMu.Lock()
|
||||
closeTrace := a.closeTrace
|
||||
|
||||
Reference in New Issue
Block a user