From c97f71a6fae9c7e05bd9525c2096cf1cbf0666d1 Mon Sep 17 00:00:00 2001 From: redhat <2292650292@qq.com> Date: Wed, 11 Jun 2025 09:19:49 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9=E5=91=A8=E6=9C=9F?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=B8=8D=E5=87=86=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- timewheel.go | 8 +++----- timewheel_test.go | 5 +++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/timewheel.go b/timewheel.go index 3aaebe4..b548269 100644 --- a/timewheel.go +++ b/timewheel.go @@ -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) diff --git a/timewheel_test.go b/timewheel_test.go index 2fe57d6..6aba960 100644 --- a/timewheel_test.go +++ b/timewheel_test.go @@ -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) }