1、修改周期定时不准问题。

This commit is contained in:
redhat 2025-06-11 09:19:49 +08:00
parent f4459ebec4
commit c97f71a6fa
2 changed files with 6 additions and 7 deletions

View File

@ -13,7 +13,6 @@ type taskSlice struct {
rawCycle int
mode TimeMode
task func()
tiny bool
}
type TimeWheel struct {
@ -77,7 +76,6 @@ func (t *TimeWheel) AddTask(id string, mode TimeMode, task func(), flower time.D
mode: mode,
rawCycle: cycle,
pos: pos,
tiny: int(flower) < t.slotLen*int(t.interval),
}
}
@ -164,8 +162,8 @@ func (t *TimeWheel) addTask(task *taskSlice) {
}
var list *list.List
if task.tiny {
task.cycle = (task.pos - t.curtSlot + t.slotLen) % t.slotLen
if task.mode == TimeTypeLoop {
task.cycle = task.cycle*t.slotLen + (task.pos-t.curtSlot+t.slotLen)%t.slotLen
task.rawCycle = task.cycle
list = t.tinyWheel
@ -187,7 +185,7 @@ func (t *TimeWheel) removeTask(key string) {
task := etask.Value.(*taskSlice)
if task.tiny {
if task.mode == TimeTypeLoop {
_ = t.tinyWheel.Remove(etask)
} else {
_ = t.slots[task.pos].Remove(etask)

View File

@ -11,7 +11,8 @@ func Test_timeWheel(t *testing.T) {
<-time.After(10 * time.Millisecond)
t.Errorf("test2, %v", time.Now())
t.Errorf("start time, %v", time.Now())
timeWheel.AddTask("test1", TimeTypeLoop, func() {
t.Errorf("test1, %v", time.Now())
}, time.Millisecond*300)
@ -22,5 +23,5 @@ func Test_timeWheel(t *testing.T) {
t.Errorf("test2, %v", time.Now())
}, time.Second)
<-time.After(6 * time.Second)
<-time.After(8 * time.Second)
}