diff --git a/core.go b/core.go index 526b21b..7a1de66 100644 --- a/core.go +++ b/core.go @@ -244,13 +244,13 @@ func (tw *TimeWheel) getPosAndCircleByInterval(d time.Duration) (int, int) { // Date : 13:47 2023/8/4 func (tw *TimeWheel) getPosAndCircleByCreatedTime(createdTime time.Time, d time.Duration, key any) (int, int) { - passedTime := time.Since(createdTime) - passedSeconds := int(passedTime.Seconds()) - delaySeconds := int(d.Seconds()) - intervalSeconds := int(tw.interval.Seconds()) + passedTime := time.Since(createdTime) // 计算从创建时间到现在经过了多少时间 + passedSeconds := int(passedTime.Seconds()) // 从开始到现在经历的秒数 + delaySeconds := int(d.Seconds()) // 多久之后执行 + intervalSeconds := int(tw.interval.Seconds()) // 时间轮盘的间隔秒数 - circle := delaySeconds / intervalSeconds / tw.slotCount - pos := (tw.currentPosition + (delaySeconds-(passedSeconds%delaySeconds))/intervalSeconds) % tw.slotCount + circle := delaySeconds / intervalSeconds / tw.slotCount // 计算需要走多少圈 + pos := (tw.currentPosition + (delaySeconds-(passedSeconds%delaySeconds))/intervalSeconds) % tw.slotCount // 计算下次执行的位置 // 特殊case,当计算的位置和当前位置重叠时,因为当前位置已经走过了,所以circle需要减一 if pos == tw.currentPosition && circle != 0 {