From 1996b3a3fc031d38bcc32972634c623c6f49c00f Mon Sep 17 00:00:00 2001 From: yml2213 Date: Sun, 28 Jun 2026 15:28:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=B6=E9=97=B4=E6=A0=A1?= =?UTF-8?q?=E5=87=86=EF=BC=9A=E9=81=BF=E5=85=8D=E9=A2=84=E7=83=AD=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E3=80=81=E5=B9=B3=E6=BB=91=E5=81=8F=E7=A7=BB=E7=94=9F?= =?UTF-8?q?=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 复校时跳过预热定时器,初始调用已完成预热 - calibrateDouyuTime 返回平滑后的偏移值,定时器调度使用中位数过滤跳变 --- internal/seckill/scheduler.go | 37 ++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/internal/seckill/scheduler.go b/internal/seckill/scheduler.go index df1067a..84345b8 100644 --- a/internal/seckill/scheduler.go +++ b/internal/seckill/scheduler.go @@ -916,10 +916,10 @@ func (s *Scheduler) calibrateDouyuTime(label string) types.TimeCalibration { } smoothed := s.smoothedOffset() logger.SetDouyuOffset(smoothed) - logger.LogEvent("时间", "success", "%s校准完成: 斗鱼估算=%s 偏移=%dms 平滑偏移=%dms 误差<=%dms RTT=%dms 采样=%d 来源=%s", + calibration.Offset = smoothed + logger.LogEvent("时间", "success", "%s校准完成: 斗鱼估算=%s 平滑偏移=%dms 误差<=%dms RTT=%dms 采样=%d 来源=%s", label, calibration.EstimatedServerNow().In(constants.CST).Format("2006-01-02 15:04:05.000"), - calibration.Offset.Milliseconds(), smoothed.Milliseconds(), calibration.ErrorBound.Milliseconds(), calibration.RTT.Milliseconds(), @@ -1207,23 +1207,25 @@ func (s *Scheduler) scheduleTimers(accountProducts map[string]string, targetTime if remain < 0 { remain = 0 } - warmupRemain := remain - warmupBeforeTrigger - if warmupRemain < 0 { - warmupRemain = 0 - } s.scheduleMu.Lock() defer s.scheduleMu.Unlock() - gen := atomic.AddInt64(&s.warmupGen, 1) - s.replaceTimerLocked(&s.warmupSchedule, warmupRemain, func() { - if atomic.LoadInt64(&s.warmupGen) != gen { - return + if !resync { + warmupRemain := remain - warmupBeforeTrigger + if warmupRemain < 0 { + warmupRemain = 0 } - logger.LogEvent("预热", "success", "预热触发: 本地=%s", time.Now().In(constants.CST).Format("15:04:05.000")) - settings := s.settingsSnapshot() - attempts := s.buildAttempts(accountProducts, settings.attempts) - s.warmupAttempts(attempts) - }) + gen := atomic.AddInt64(&s.warmupGen, 1) + s.replaceTimerLocked(&s.warmupSchedule, warmupRemain, func() { + if atomic.LoadInt64(&s.warmupGen) != gen { + return + } + logger.LogEvent("预热", "success", "预热触发: 本地=%s", time.Now().In(constants.CST).Format("15:04:05.000")) + settings := s.settingsSnapshot() + attempts := s.buildAttempts(accountProducts, settings.attempts) + s.warmupAttempts(attempts) + }) + } s.replaceTimerLocked(&s.schedule, remain, func() { now := time.Now() estimatedServer := now.Add(calibration.Offset) @@ -1239,14 +1241,13 @@ func (s *Scheduler) scheduleTimers(accountProducts map[string]string, targetTime s.RunNow(accountProducts) }) if resync { - logger.LogEvent("定时", "success", "临近复校已应用: 目标=%s 触发=%s 斗鱼估算=%s 本地偏移=%dms 误差<=%dms RTT=%dms 预热=%s", + logger.LogEvent("定时", "success", "临近复校已应用: 目标=%s 触发=%s 斗鱼估算=%s 偏移=%dms 误差<=%dms RTT=%dms", targetTime.In(constants.CST).Format("2006-01-02 15:04:05.000"), triggerTime.In(constants.CST).Format("2006-01-02 15:04:05.000"), serverTime.In(constants.CST).Format("2006-01-02 15:04:05.000"), calibration.Offset.Milliseconds(), calibration.ErrorBound.Milliseconds(), - calibration.RTT.Milliseconds(), - time.Now().Add(warmupRemain).In(constants.CST).Format("15:04:05.000")) + calibration.RTT.Milliseconds()) } }