The Timer type represents a single event in the timing wheel. When the timer expires, the associated task function is executed.
You can use Stop() to prevent a timer from firing. Stop() returns true if the timer was successfully stopped, and false if it has already expired or was already stopped.
Note on Concurrency: If the timer has already expired and the task has started in its own goroutine, Stop() does not wait for the task to complete. If you need to ensure the task is finished, you must implement your own coordination mechanism.
// Example concept of a Timer (Note: Timer is typically managed by the timing wheel)
timer := &timingwheel.Timer{
task: func() {
fmt.Println("Timer expired!")
},
}
// Stop the timer
if stopped := timer.Stop(); stopped {
fmt.Println("Timer stopped successfully")
} else {
fmt.Println("Timer could not be stopped (already expired or stopped)")
}