-
- Notifications
You must be signed in to change notification settings - Fork 4.3k
Open
Description
Describe the bug
添加任务时,如果任务定时的时间计算出的槽位刚好和当前指针槽位相同,无论圈数是多少,都会被延迟一圈执行。(除非task.delay < tw.interval立即执行)
概率≈1/槽位数
因为当前指针执行的时候会给槽位里的所有任务圈数减1,而新任务刚好添加到这个槽位里,却错过了圈数减1的逻辑
To Reproduce
Steps to reproduce the behavior, if applicable:
-
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. Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels