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写道:
>
> 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 NewTicke
> <https://golang.org/pkg/time/#NewTicker>r: "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.
>
> On Monday, May 25, 2020 at 12:14:24 AM UTC-4, Kai Zhang wrote:
>>
>> 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
>> <https://play.golang.org/p/18o0wIAPRzP>
>> thanks,
>> zhangkai
>> 2020-05-25
>>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/cd971803-c068-493c-9195-412ba3d5291a%40googlegroups.com.