RussellLuo / timingwheel

Golang implementation of Hierarchical Timing Wheels.
MIT License
657 stars 123 forks source link

Add support for resetting a timer #10

Open RussellLuo opened 5 years ago

RussellLuo commented 5 years ago

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

func (t *Timer) Reset(d time.Duration) {
    ...
}

Pros:

Cons:

2. Add TimingWheel.ResetTimer

func (tw *TimingWheel) ResetTimer(t *Timer, d time.Duration) {
    ...
}

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.

func (tw *TimingWheel) AfterFunc(d time.Duration, f func(), t *Timer) *Timer {
    ...
}

Pros:

Cons: