优化一些功能
This commit is contained in:
+40
-13
@@ -28,9 +28,11 @@ type App struct {
|
||||
client *client.DouyuClient
|
||||
scheduler *seckill.Scheduler
|
||||
logStore *logger.MemoryLogStore
|
||||
trace *trace.Recorder
|
||||
paths types.AppPaths
|
||||
font *truetype.Font
|
||||
|
||||
traceMu sync.RWMutex
|
||||
trace *trace.Recorder
|
||||
paths types.AppPaths
|
||||
font *truetype.Font
|
||||
|
||||
cfgMu sync.RWMutex
|
||||
cfg types.Config
|
||||
@@ -50,6 +52,13 @@ func (a *App) getClient() *client.DouyuClient {
|
||||
return a.client
|
||||
}
|
||||
|
||||
// getTrace 返回当前 trace 记录器快照(读锁保护)。
|
||||
func (a *App) getTrace() *trace.Recorder {
|
||||
a.traceMu.RLock()
|
||||
defer a.traceMu.RUnlock()
|
||||
return a.trace
|
||||
}
|
||||
|
||||
// getCfg 返回当前配置的深拷贝(读锁保护)。
|
||||
func (a *App) getCfg() types.Config {
|
||||
a.cfgMu.RLock()
|
||||
@@ -71,6 +80,25 @@ func (a *App) setCfg(c types.Config) {
|
||||
a.cfgMu.Unlock()
|
||||
}
|
||||
|
||||
// replaceTrace 根据配置替换 trace 记录器,旧记录器在锁外关闭,避免阻塞读路径。
|
||||
func (a *App) replaceTrace(cfg types.Config) {
|
||||
traceCfg := types.TraceConfig{
|
||||
Enabled: cfg.TraceEnabled,
|
||||
MaxBodyBytes: cfg.TraceMaxBody,
|
||||
}
|
||||
tr, closeTrace := trace.InitTrace(a.paths.LogDir, traceCfg)
|
||||
|
||||
a.traceMu.Lock()
|
||||
oldClose := a.closeTrace
|
||||
a.trace = tr
|
||||
a.closeTrace = closeTrace
|
||||
a.traceMu.Unlock()
|
||||
|
||||
if oldClose != nil {
|
||||
oldClose()
|
||||
}
|
||||
}
|
||||
|
||||
// cloneConfig 深拷贝配置,避免 RetryStages 切片共享引用。
|
||||
func cloneConfig(c types.Config) types.Config {
|
||||
cp := c
|
||||
@@ -98,13 +126,7 @@ func (a *App) Startup(ctx context.Context) {
|
||||
a.cfg = config.LoadConfig(a.paths.ConfigFile)
|
||||
logger.SetMinLevel(a.cfg.LogLevel)
|
||||
|
||||
traceCfg := types.TraceConfig{
|
||||
Enabled: a.cfg.TraceEnabled,
|
||||
MaxBodyBytes: a.cfg.TraceMaxBody,
|
||||
}
|
||||
tr, closeTrace := trace.InitTrace(a.paths.LogDir, traceCfg)
|
||||
a.trace = tr
|
||||
a.closeTrace = closeTrace
|
||||
a.replaceTrace(a.cfg)
|
||||
|
||||
// 解析当前活动:配置中的 activity ID 决定使用哪套接口参数
|
||||
act := activity.Get(a.cfg.Activity)
|
||||
@@ -112,7 +134,7 @@ func (a *App) Startup(ctx context.Context) {
|
||||
act = activity.Default()
|
||||
logger.LogEvent("活动", "warn", "配置中的活动 %q 未识别,已回退到默认活动 %s", a.cfg.Activity, act.DisplayName)
|
||||
}
|
||||
a.client = client.New(act, a.cfg.TimeAPIURL, a.trace)
|
||||
a.client = client.New(act, a.cfg.TimeAPIURL, a.getTrace())
|
||||
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.LoadAccounts()
|
||||
@@ -136,8 +158,13 @@ func (a *App) Shutdown(ctx context.Context) {
|
||||
if a.scheduler != nil {
|
||||
a.scheduler.Close()
|
||||
}
|
||||
if a.closeTrace != nil {
|
||||
a.closeTrace()
|
||||
a.traceMu.Lock()
|
||||
closeTrace := a.closeTrace
|
||||
a.closeTrace = nil
|
||||
a.trace = nil
|
||||
a.traceMu.Unlock()
|
||||
if closeTrace != nil {
|
||||
closeTrace()
|
||||
}
|
||||
if a.closeLog != nil {
|
||||
a.closeLog()
|
||||
|
||||
Reference in New Issue
Block a user