Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for resetting a timer #10

Open
RussellLuo opened this issue Sep 21, 2019 · 0 comments
Open

Add support for resetting a timer #10

RussellLuo opened this issue Sep 21, 2019 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@RussellLuo
Copy link
Owner

RussellLuo commented Sep 21, 2019

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:

  • 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.

Cons:

  • AfterFunc becomes more complex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant