I have nothing to comment about timer.Reset() but your pseudo code made me wonder if there's an interval timer available in the library, and of course it is.
In the example you aim to do some work periodically, until another type of work succeeds. That's not a plain meaning of a timeout. In addition you may be interested in regular intervals not just "no sooner than x" so NewTicker sounds about right here. If occasionally the timeout preference may change, it's ok to start a new ticker. timeout := <-timeoutConfigC ticker := time.NewTicker(timeout) defer ticker.Stop() for { select { case timeout = <-timeoutConfigC: ticker.Stop() ticker = time.NewTicker(timeout) // if the user's new preference "just misses" this interval, oh well, next time then case <-ticker.C: handleTimeout() //periodic work 'a' case work := <-workC: if done := do(work); done { return } } } On Wednesday, April 1, 2020 at 11:17:14 PM UTC+2, Neil Schellenberger wrote: > > Thank you very much for confirming that, Ian! > > FWIW the scenario is very roughly along the lines of a "best effort > re-configurable" timeout for a work loop: > > timeout := <-timeoutConfigC > timer := time.NewTimer(timeout) > defer timer.Stop() > for { > select { > case timeout = <-timeoutConfigC: > timer.Reset(timeout) // if the user's new preference "just misses" > this interval, oh well, next time then > case <-timer.C: > handleTimeout() > timer.Reset(timeout) > case work := <-workC: > if done := do(work); done { > return > } > } > } > > The actual use case is a protocol state machine with various user tunable > intervals. It's fine if a new value doesn't get "applied" until the "next > time around". > > Obviously there are plenty of other ways to handle this, but it got me > wondering if I understood "Timer.Reset()" properly or not. And/or I may be > using an anti-pattern. Do please let me know if I am :-) > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/30d9914c-4321-4309-b50f-6aac6749fbd4%40googlegroups.com.