timingwheel icon indicating copy to clipboard operation
timingwheel copied to clipboard

timewheel启动了很久,新插入一个定时器直接就过期

Open tangeping opened this issue 2 years ago • 1 comments

currentTime 只有在advanceClock中更新。假如 timewheel开了很久了,bucket 中没有定时器,新插入一个定时器,addOrRun读到currentTime 是不是直接就过期了?

func (tw *TimingWheel) advanceClock(expiration int64) {
	currentTime := atomic.LoadInt64(&tw.currentTime)
	if expiration >= currentTime+tw.tick {
		currentTime = truncate(expiration, tw.tick)
		atomic.StoreInt64(&tw.currentTime, currentTime)

		// Try to advance the clock of the overflow wheel if present
		overflowWheel := atomic.LoadPointer(&tw.overflowWheel)
		if overflowWheel != nil {
			(*TimingWheel)(overflowWheel).advanceClock(currentTime)
		}
	}
}

@RussellLuo

tangeping avatar Jan 10 '23 09:01 tangeping

时间轮的currentTime向前推进,是由DelayQueue中的堆顶元素过期的事件触发的。没有bucket到期就不会更新currentTime。

ch0ngsheng avatar Feb 11 '23 05:02 ch0ngsheng