dynamic frequency ticker

393 views
Skip to first unread message

seb....@gmail.com

unread,
Aug 30, 2016, 1:46:23 PM8/30/16
to golang-nuts
In my application I select on a ticker channel, but sometimes need to have the waiting time vary a bit. For not so frequent changes I could make a new ticker everytime, but I have the feeling this is not the best solution for higher frequencies and many rate changes. Best would be if I could tell my existing ticker "from next tick on please use an interval of x". In fact what I want is that the frequency changes over time.

Any tips how to achieve that?

Aaron Cannon

unread,
Aug 30, 2016, 3:42:20 PM8/30/16
to seb....@gmail.com, golang-nuts
How about creating a custom ticker that uses time.Sleep. There might
be some hidden caveats when using time.Sleep verses a real ticker that
I am unaware of, but it might meet your needs. You could then add a
custom method, or inbound channel, which you could use to tweak its
intervals on the fly.

Aaron
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

Edward Muller

unread,
Aug 30, 2016, 3:46:46 PM8/30/16
to Aaron Cannon, seb....@gmail.com, golang-nuts
How about something like this? https://play.golang.org/p/U50n3cqIXg

seb....@gmail.com

unread,
Aug 31, 2016, 12:45:32 AM8/31/16
to golang-nuts, can...@fireantproductions.com, seb....@gmail.com

Interesting. But it would give me two interfering frequencies at the same time, which is not what I want. Also it feels inflexible that for every possible frequency I would have to hardcode a separate select branch. Thanks anyway.

seb....@gmail.com

unread,
Aug 31, 2016, 12:58:37 AM8/31/16
to golang-nuts, seb....@gmail.com
The problem I have with time.Sleep is that I am totally relying on the "exact" time guarantee a Ticker gives me. What I mean is that with a Ticker I can be sure that if it fires every 50ms, no matter what my code does (, after 10 minutes it ran exactly 12000 times. I don't know how I would do that properly with time.Sleep.

(Before somebody says now that realtime-like things won't work well with go or any garbage collected language: So far it has worked just good enough for my purposes, and I don't need more than a few ms accuracy)

dja...@gmail.com

unread,
Aug 31, 2016, 2:25:21 AM8/31/16
to golang-nuts, seb....@gmail.com

yann....@gmail.com

unread,
Aug 31, 2016, 8:26:10 AM8/31/16
to golang-nuts, seb....@gmail.com, dja...@gmail.com
Hi there,

maybe you should take a look on this one :
https://play.golang.org/p/SpTNuGG-ni

Marvin Renich

unread,
Aug 31, 2016, 10:06:58 AM8/31/16
to golang-nuts
> > On Tuesday, August 30, 2016 at 8:46:23 PM UTC+3, seb....@gmail.com wrote:
> >>
> >> In my application I select on a ticker channel, but sometimes need to
> >> have the waiting time vary a bit. For not so frequent changes I could make
> >> a new ticker everytime, but I have the feeling this is not the best
> >> solution for higher frequencies and many rate changes. Best would be if I
> >> could tell my existing ticker "from next tick on please use an interval of
> >> x". In fact what I want is that the frequency changes over time.
> >>
> >> Any tips how to achieve that?

Does this do what you want?

https://play.golang.org/p/302HPhYn89

With the current code, t.C can be passed around, but SetDuration must
always be called from the same goroutine. With a little work (and a
mutex) you could make SetDuration safe for concurrent use.

...Marvin

seb....@gmail.com

unread,
Sep 1, 2016, 4:48:30 PM9/1/16
to golang-nuts, mr...@renich.org

This works great, thank you! And with minimal changes to my existing code (that's why I didn't like so much the other proposals that use time.After or time.Timer, but thanks anyway!).
Reply all
Reply to author
Forward
0 new messages