Implement something like Timer.Reset, to reuse an existing timer and reset its duration.
Solutions
There are three candidate solutions:
1. Add Timer.Reset
func (t *Timer) Reset(d time.Duration) {
...
}
Pros:
The API is similar to Timer.Reset, which is simple and intuitive.
Cons:
Since Reset needs to add the timer back into the timing wheel, the timer must know which timing wheel it belongs to.
To achieve this goal, we need to add a new field (e.g. tw *timingwheel.TimingWheel) into the Timer struct. And this is a waste of memory since the total amount of timers is often large.
2. Add TimingWheel.ResetTimer
func (tw *TimingWheel) ResetTimer(t *Timer, d time.Duration) {
...
}
Pros:
There is no waste of memory.
Cons:
The API is simple but not as intuitive as Timer.Reset.
3. Modify TimingWheel.AfterFunc
Add one more argument named t to provide an existing timer. If t is not nil, we reuse it. Otherwise, we create a new timer.
func (tw *TimingWheel) AfterFunc(d time.Duration, f func(), t *Timer) *Timer {
...
}
Pros:
There is no waste of memory.
There is only one API AfterFunc, no need to add a new one.
Motivation
Implement something like Timer.Reset, to reuse an existing timer and reset its duration.
Solutions
There are three candidate solutions:
1. Add Timer.Reset
Pros:
Cons:
tw *timingwheel.TimingWheel
) into the Timer struct. And this is a waste of memory since the total amount of timers is often large.2. Add TimingWheel.ResetTimer
Pros:
Cons:
3. Modify TimingWheel.AfterFunc
Add one more argument named
t
to provide an existing timer. If t is not nil, we reuse it. Otherwise, we create a new timer.Pros:
AfterFunc
, no need to add a new one.Cons:
AfterFunc
becomes more complex.