49

I have a loop that iterates until a job is up and running:

ticker := time.NewTicker(time.Second * 2)
defer ticker.Stop()

started := time.Now()
for now := range ticker.C {
    job, err := client.Job(jobID)
    switch err.(type) {
    case DoesNotExistError:
        continue
    case InternalError:
        return err
    }

    if job.State == "running" {
        break
    }

    if now.Sub(started) > time.Minute*2 {
        return fmt.Errorf("timed out waiting for job")
    }
}

Works great in production. The only problem is that it makes my tests slow. They all wait at least 2 seconds before completing. Is there anyway to get time.Tick to tick immediately?

6
  • Whoops, thanks for the heads up.
    – Xavi
    Commented Sep 21, 2015 at 23:44
  • Set ticker and other durations to smaller values in tests. Commented Sep 22, 2015 at 0:12
  • These "jobs" are actually running on a remote service. Though it is possible to have this program run a server locally and modify the remote service to accept subscriptions, that solution is much too heavy handed imo.
    – Xavi
    Commented Sep 22, 2015 at 7:27
  • @JimB sorry for my ignorance, but what's the race condition you're referring to around job.State? Is the assumption that job is a shared object?
    – Xavi
    Commented Sep 22, 2015 at 7:29
  • 2
    @Xavi : wrap your code in a function, use variables for the ticker delay and the timeout value, set those variables to smaller values when testing.
    – LeGEC
    Commented Sep 22, 2015 at 15:19

9 Answers 9

77

Unfortunately, it seems that Go developers will not add such functionality in any foreseeable future, so we have to cope...

There are two common ways to use tickers:

for loop

Given something like this:

ticker := time.NewTicker(period)
defer ticker.Stop()
for <- ticker.C {
    ...
}

Use:

ticker := time.NewTicker(period)
defer ticker.Stop()
for ; true; <- ticker.C {
    ...
}

for-select loop

Given something like this:

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

ticker := time.NewTicker(period)
defer ticker.Stop()

loop:
for {
    select {
        case <- ticker.C: 
            f()
        case <- interrupt:
            break loop
    }
}

Use:

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

ticker := time.NewTicker(period)
defer ticker.Stop()

loop:
for {
    f()

    select {
        case <- ticker.C: 
            continue
        case <- interrupt:
            break loop
    }
}

Why not just use time.Tick()?

While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks".

https://golang.org/pkg/time/#Tick

2
  • 3
    Does <- time.Tick(period) create a Ticker on each iteration? According to document, the Ticker created by Tick cannot be garbage-collected. This terrifies me.
    – youfu
    Commented Oct 16, 2020 at 10:02
  • 1
    @youfu You are absolutely right! I have just edited my answer. Commented Oct 19, 2020 at 16:12
29
ticker := time.NewTicker(period)
for ; true; <-ticker.C {
    ...
}

https://github.com/golang/go/issues/17601

2
  • 7
    Just to add to this. If you don't want to lose the value you get from the chan you can do for t := time.Now(); true; t = <-ticker.C { Commented May 29, 2019 at 23:59
  • This solutions is also more elegant in cases you need to call continue, inside the for block
    – natenho
    Commented Aug 21, 2023 at 5:27
6

The actual implementation of Ticker internally is pretty complicated. But you can wrap it with a goroutine:

func NewTicker(delay, repeat time.Duration) *time.Ticker {
    ticker := time.NewTicker(repeat)
    oc := ticker.C
    nc := make(chan time.Time, 1)
    go func() {
        nc <- time.Now()
        for tm := range oc {
            nc <- tm
        }
    }()
    ticker.C = nc
    return ticker
}
4
  • Couldn't you just send to the ticker channel immediately ?
    – LenW
    Commented Sep 22, 2015 at 5:25
  • 2
    It's a receive only channel: type Ticker struct { C <-chan Time // The channel on which the ticks are delivered. // contains filtered or unexported fields }
    – Caleb
    Commented Sep 22, 2015 at 11:23
  • Why delay arg used here?
    – coanor
    Commented Oct 9, 2021 at 2:46
  • "delay" is not used. Also i want point out that this timer is not guaranteed to fire instantly, as the go func(){ } part is sheduled, but not executed for sure. But asides from that nitpicking this should be the accepted answer imho
    – simon
    Commented Dec 8, 2021 at 8:00
6

If you want to check the job right away, don't use the ticker as the condition in the for loop. For example:

ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()

started := time.Now()
for {
    job, err := client.Job(jobID)
    if err == InternalError {
        return err
    }

    if job.State == "running" {
        break
    }

    now := <-ticker.C
    if now.Sub(started) > 2*time.Minute {
        return fmt.Errorf("timed out waiting for job")
    }
}

If you do still need to check for DoesNotExistError, you want to make sure you do it after the ticker so you don't have a busy-wait.

1
3

I cooked up something like this

func main() {
    t := time.Now()
    callme := func() {
        // do somethign more
        fmt.Println("callme", time.Since(t))
    }
    ticker := time.NewTicker(10 * time.Second)
    first := make(chan bool, 1)
    first <- true
    for {
        select {
        case <-ticker.C:
            callme()
        case <-first:
            callme()
        }
        t = time.Now()
    }
    close(first)
}
1
  • Isn't it just easier and also clean to call callme() just beofre the for loop? What would be the advantage of complicating it? Commented Feb 28, 2022 at 20:10
3

How about using Timer instead of Ticker? Timer can be started with zero duration and then reset to the desired duration value:

timer := time.NewTimer(0)
defer timer.Stop()

for {
    select {
        case <-timer.C:
            timer.Reset(interval)
            job()
        case <-ctx.Done():
            break
    }
}
1

I think this might be an interesting alternative for the for-select loop, specially if the contents of the case are not a simple function:

Having:

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

ticker := time.NewTicker(period)
defer ticker.Stop()

loop:
for {
    select {
        case <- ticker.C: 
            f()
        case <- interrupt:
            break loop
    }
}

Use:

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

ticker := time.NewTicker(period)
defer ticker.Stop()
firstTick := false

// create a wrapper of the ticker that ticks the first time immediately
tickerChan := func() <-chan time.Time {
  if !firstTick {
    firstTick = true
    c := make(chan time.Time, 1)
    c <- time.Now()
    return c
  }

  return ticker.C
}

loop:
for {
    select {
        case <- tickerChan(): 
            f()
        case <- interrupt:
            break loop
    }
}
1

You can also drain the channel at the end of the loop:

t := time.NewTicker(period)
defer t.Stop()

for {
    ...
    
    <-t.C
}
1

I will expand on @Bora M. Alper's already good suggestions. If you are using for-select loop, it's better to refactor this:

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

ticker := time.NewTicker(period)
defer ticker.Stop()

loop:
for {
    select {
        case <- ticker.C: 
            f()
        case <- interrupt:
            break loop
    }
}

to this:

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

ticker := time.NewTicker(period)
defer ticker.Stop()

loop:
for ; true; <- ticker.C {
    select {
        case <- interrupt: 
            break loop
        default:
            f()
    }
}

This way you do not skip the initial check for interruption and you also gain the first instant tick advantage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.