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

proposal: time: add Reset api #33184

Open
zero-go opened this issue Jul 19, 2019 · 2 comments
Open

proposal: time: add Reset api #33184

zero-go opened this issue Jul 19, 2019 · 2 comments
Labels
Projects
Milestone

Comments

@zero-go
Copy link

@zero-go zero-go commented Jul 19, 2019

What version of Go are you using (go version)?

go 1.12

Does this issue reproduce with the latest release?

yes.

What operating system and processor architecture are you using (go env)?

linux amd64

What did you do?

I added the api reset function.

What did you expect to see?

I hope that the proposal will take effect,because sometimes we need to adjust the time dynamically.
This is not a problem,this is just a suggestion.
Please read the following.thanks.

api:

// Reset duration to reset a ticker.
func (t *Ticker) Reset(d Duration) bool {
	if t.r.f == nil {
		panic("time: Reset called on uninitialized Ticker")
	}
	w := when(d)
	active := stopTimer(&t.r)
	t.r.when = w
	t.r.period = int64(d)
	startTimer(&t.r)
	return active
}
@agnivade agnivade changed the title update time/tick.go add api proposal: time: add Reset api Jul 19, 2019
@gopherbot gopherbot added this to the Proposal milestone Jul 19, 2019
@gopherbot gopherbot added the Proposal label Jul 19, 2019
@rsc

This comment has been minimized.

Copy link
Contributor

@rsc rsc commented Aug 20, 2019

It seems reasonable to be able to change the duration on a ticker.
But the return value from time.Timer.Reset was a mistake and is impossible to use correctly, and this API would provide the same return value. It seems like better options would be to have no return value from Reset and either:

  1. Require that the ticker is stopped; panic if not.
  2. If the ticker is not stopped, first stop it and drain any existing value from the channel.

The second seems defensible to me and useful in code that is doing something like:

t := time.NewTicker(d)
for range t.C {
    if ... { t.Reset(newD) }
}

In general, if the Reset is happening separate from the channel receive, you'd want to do t.Stop, then synchronize with the receiver so it knows to expect a new duration, then t.Reset. But the pattern above does not need that because the receiver and the Reset caller are the same, and that seems likely to be common.

So I would lean toward option 2. Thoughts?

@zero-go

This comment has been minimized.

Copy link
Author

@zero-go zero-go commented Aug 21, 2019

@rsc
I agree that Reset has no return value,but I don't know what stopTimer returns error under what conditions,at first I just wanted to separate out two apis,like this:

func NewTicker(d Duration) *Ticker {
	...
	//startTimer(&t.r)
	return t
}
func (t *Ticker) StartTicker() {c
	startTimer(&t.r)
}

This way developers can control their own startup time,later added api Reset,using like this:

ticker := time.NewTicker(d)
go func() {
	for _ = range ticker.C {
		...
	}
}()
...
...
...
if ... {
	ticker.Reset(newD)
}

But this does have a situation where stopTimer returns an error,do you have a better solution?

@rsc rsc added this to Incoming in Proposals Dec 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Proposals
Incoming
3 participants
You can’t perform that action at this time.