Skip to content

时间轮TimingWheel有概率延迟一圈 #5510

@tinico

Description

@tinico

Describe the bug
添加任务时,如果任务定时的时间计算出的槽位刚好和当前指针槽位相同,无论圈数是多少,都会被延迟一圈执行。(除非task.delay < tw.interval立即执行)

概率≈1/槽位数

因为当前指针执行的时候会给槽位里的所有任务圈数减1,而新任务刚好添加到这个槽位里,却错过了圈数减1的逻辑

To Reproduce
Steps to reproduce the behavior, if applicable:

  1. The code is

// 计算圈数和槽位
func (tw *TimingWheel) getPositionAndCircle(d time.Duration) (pos, circle int) {
steps := int(d / tw.interval)
pos = (tw.tickedPos + steps) % tw.numSlots
circle = (steps - 1) / tw.numSlots

return 

}

// 处理当前指针所在槽位
func (tw *TimingWheel) onTick() {
tw.tickedPos = (tw.tickedPos + 1) % tw.numSlots
l := tw.slots[tw.tickedPos]
tw.scanAndRunTasks(l)
}

 2. The error is 
 **Expected behavior** 期望不要被延迟1圈 **Screenshots** If applicable, add screenshots to help explain your problem. **Environments (please complete the following information):** - OS: [e.g. Linux] - go-zero version 1.9.0 - goctl version [e.g. 1.2.1, optional] **More description** Add any other context about the problem here. 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions