优化时间校准:避免预热重复、平滑偏移生效
- 复校时跳过预热定时器,初始调用已完成预热 - calibrateDouyuTime 返回平滑后的偏移值,定时器调度使用中位数过滤跳变
This commit is contained in:
@@ -916,10 +916,10 @@ func (s *Scheduler) calibrateDouyuTime(label string) types.TimeCalibration {
|
|||||||
}
|
}
|
||||||
smoothed := s.smoothedOffset()
|
smoothed := s.smoothedOffset()
|
||||||
logger.SetDouyuOffset(smoothed)
|
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,
|
label,
|
||||||
calibration.EstimatedServerNow().In(constants.CST).Format("2006-01-02 15:04:05.000"),
|
calibration.EstimatedServerNow().In(constants.CST).Format("2006-01-02 15:04:05.000"),
|
||||||
calibration.Offset.Milliseconds(),
|
|
||||||
smoothed.Milliseconds(),
|
smoothed.Milliseconds(),
|
||||||
calibration.ErrorBound.Milliseconds(),
|
calibration.ErrorBound.Milliseconds(),
|
||||||
calibration.RTT.Milliseconds(),
|
calibration.RTT.Milliseconds(),
|
||||||
@@ -1207,23 +1207,25 @@ func (s *Scheduler) scheduleTimers(accountProducts map[string]string, targetTime
|
|||||||
if remain < 0 {
|
if remain < 0 {
|
||||||
remain = 0
|
remain = 0
|
||||||
}
|
}
|
||||||
warmupRemain := remain - warmupBeforeTrigger
|
|
||||||
if warmupRemain < 0 {
|
|
||||||
warmupRemain = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
s.scheduleMu.Lock()
|
s.scheduleMu.Lock()
|
||||||
defer s.scheduleMu.Unlock()
|
defer s.scheduleMu.Unlock()
|
||||||
gen := atomic.AddInt64(&s.warmupGen, 1)
|
if !resync {
|
||||||
s.replaceTimerLocked(&s.warmupSchedule, warmupRemain, func() {
|
warmupRemain := remain - warmupBeforeTrigger
|
||||||
if atomic.LoadInt64(&s.warmupGen) != gen {
|
if warmupRemain < 0 {
|
||||||
return
|
warmupRemain = 0
|
||||||
}
|
}
|
||||||
logger.LogEvent("预热", "success", "预热触发: 本地=%s", time.Now().In(constants.CST).Format("15:04:05.000"))
|
gen := atomic.AddInt64(&s.warmupGen, 1)
|
||||||
settings := s.settingsSnapshot()
|
s.replaceTimerLocked(&s.warmupSchedule, warmupRemain, func() {
|
||||||
attempts := s.buildAttempts(accountProducts, settings.attempts)
|
if atomic.LoadInt64(&s.warmupGen) != gen {
|
||||||
s.warmupAttempts(attempts)
|
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() {
|
s.replaceTimerLocked(&s.schedule, remain, func() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
estimatedServer := now.Add(calibration.Offset)
|
estimatedServer := now.Add(calibration.Offset)
|
||||||
@@ -1239,14 +1241,13 @@ func (s *Scheduler) scheduleTimers(accountProducts map[string]string, targetTime
|
|||||||
s.RunNow(accountProducts)
|
s.RunNow(accountProducts)
|
||||||
})
|
})
|
||||||
if resync {
|
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"),
|
targetTime.In(constants.CST).Format("2006-01-02 15:04:05.000"),
|
||||||
triggerTime.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"),
|
serverTime.In(constants.CST).Format("2006-01-02 15:04:05.000"),
|
||||||
calibration.Offset.Milliseconds(),
|
calibration.Offset.Milliseconds(),
|
||||||
calibration.ErrorBound.Milliseconds(),
|
calibration.ErrorBound.Milliseconds(),
|
||||||
calibration.RTT.Milliseconds(),
|
calibration.RTT.Milliseconds())
|
||||||
time.Now().Add(warmupRemain).In(constants.CST).Format("15:04:05.000"))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user