time.Ticker's behavior

789 views
Skip to first unread message

Kai Zhang

unread,
May 25, 2020, 12:14:24 AM5/25/20
to golang-nuts
Hello,
      I'm using ticker to do some batch works.But I found why tickers runs not as expect.why the second ticker is 100 Millisecond.code is here
thanks,
zhangkai
2020-05-25

Brian Candler

unread,
May 25, 2020, 7:09:44 AM5/25/20
to golang-nuts
You asked for ticks at 100ms intervals, but your receiver has a time.Sleep(1*time.Second).

"It adjusts the intervals or drops ticks to make up for slow receivers."

Jake Montgomery

unread,
May 25, 2020, 7:22:15 AM5/25/20
to golang-nuts
I'll take a crack at it. The behavior you see is one tick about 100ms after start, a second one marked as 200ms after start, then one 1200ms, one at 2200, and another at 3200ms after start.

The key is in the documentation for NewTicker: "It adjusts the intervals or drops ticks to make up for slow receivers."
If you add a bit more info to your app it becomes clearer: https://play.golang.org/p/gHZXgyIuYw7
Here is what is happening:

100ms - The ticker fires and sends a tick timestamped 100ms.
100ms - Your goroutine wakes, receives the tick timestamped 100ms.
100+ms - Your goroutine goes to sleep at line 30 (for one second).
200ms - The ticker fires again, but your goroutine is asleep.
300ms - Since the last tick has not been processed, the ticker waits, as described in the docs: "It adjusts the intervals or drops ticks to make up for slow receivers."
1100ms - Your goroutine wakes, receives the tick timestamped 200ms.
1100+ms Your goroutine goes to sleep at line 30 (for one second).
1200ms - Since the tick was processed the ticker sends its next tick.
1300ms - Since the last tick has not been processed, the ticker waits, as described in the docs: "It adjusts the intervals or drops ticks to make up for slow receivers."
2100ms - Your goroutine wakes, receives the tick timestamped 1200ms
2100ms - Your goroutine goes to sleep at line 30 (for one second).
2200ms - Since the tick was processed the ticker sends its next tick.
2300ms - Since the last tick has not been processed, the ticker waits, as described in the docs: "It adjusts the intervals or drops ticks to make up for slow receivers."

It continues on like that until the program exits after 3600ms.

Hope this helps.

Kai Zhang

unread,
May 26, 2020, 8:11:16 AM5/26/20
to golang-nuts
Hello,
    It really help,I scan the go source and I can see the ticker.C is 1-element channel.maybe I should just treate it as a normal channel.but the value in channel is Time.Now I am seek how the later ticker be dropped.because the source code said 
// NewTicker returns a new Ticker containing a channel that will send the
// time with a period specified by the duration argument.
// It adjusts the intervals or drops ticks to make up for slow receivers.
// The duration d must be greater than zero; if not, NewTicker will panic.
// Stop the ticker to release associated resources.
func
NewTicker(d Duration) *Ticker {
       
if d <= 0 {
                panic
(errors.New("non-positive interval for NewTicker"))
       
}
       
// Give the channel a 1-element time buffer.
       
// If the client falls behind while reading, we drop ticks
       
// on the floor until the client catches up.
        c
:= make(chan Time, 1)
        t
:= &Ticker{
                C
: c,
                r
: runtimeTimer{
                       
when:   when(d),
                        period
: int64(d),
                        f
:      sendTime,
                        arg
:    c,
               
},
       
}
        startTimer
(&t.r)
       
return t
}



在 2020年5月25日星期一 UTC+8下午7:22:15,Jake Montgomery写道:
Reply all
Reply to author
Forward
0 new messages