Skip to content

Conversation

@jeryaiwei
Copy link

Added a fix to delete timers with short delays.

Added a fix to delete timers with short delays.
@codecov
Copy link

codecov bot commented Dec 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@jeryaiwei
Copy link
Author

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

  1. timer.item.removed = true:
    这是一个标记,用于指示任务已被逻辑删除。在 scanAndRunTasks 方法中,如果任务被标记为 removed,会从槽位列表中移除,避免重复执行。虽然在立即执行的情况下,任务没有重新插入槽位,但保留这个标记可以确保代码的一致性和健壮性,防止潜在的竞态条件或意外重复执行。
  2. tw.timers.Del(task.key):
    映射中删除键,确保映射中不保留无效条目。
    如果不删除,映射中会保留指向已执行任务的引用,可能导致内存泄漏(虽然 Go 的 GC 会处理,但不利于代码清晰)。
    在后续的 SetTimer 或 MoveTimer 操作中,如果键已存在但指向无效条目,可能引发逻辑错误或不一致的行为。
func TestTimingWheel_MoveTimerImmediateExecution(t *testing.T) {
   var executionCount int32
   ticker := timex.NewFakeTicker()
   tw, _ := NewTimingWheelWithTicker(testStep, 10, func(k, v any) {
   	atomic.AddInt32(&executionCount, 1)
   	assert.Equal(t, "test_key", k)
   	assert.Equal(t, "test_value", v)
   }, ticker)
   defer tw.Stop()

   // Set initial task
   tw.SetTimer("test_key", "test_value", testStep*5)

   // Move to short delay (less than interval), should execute immediately
   tw.MoveTimer("test_key", testStep>>1)

   // Wait for a while to ensure no extra execution
   time.Sleep(waitTime)

   // Verify executed only once
   assert.Equal(t, int32(1), atomic.LoadInt32(&executionCount))

   // Verify the map has been cleaned up
   _, exists := tw.timers.Get("test_key")
   assert.False(t, exists, "The map should not retain entries for executed tasks")
}

@jeryaiwei
Copy link
Author

@kevwan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant