2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package time
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
import "errors"
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
// A Ticker holds a channel that delivers `ticks' of a clock
|
2010-12-03 05:34:57 +01:00
|
|
|
// at intervals.
|
|
|
|
type Ticker struct {
|
2011-12-13 20:16:27 +01:00
|
|
|
C <-chan Time // The channel on which the ticks are delivered.
|
2011-12-13 00:40:51 +01:00
|
|
|
r runtimeTimer
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// NewTicker returns a new Ticker containing a channel that will send the
|
2012-01-25 21:56:26 +01:00
|
|
|
// time with a period specified by the duration argument.
|
2011-12-13 20:16:27 +01:00
|
|
|
// 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.
|
2014-07-19 10:53:52 +02:00
|
|
|
// Stop the ticker to release associated resources.
|
2011-12-13 20:16:27 +01:00
|
|
|
func NewTicker(d Duration) *Ticker {
|
|
|
|
if d <= 0 {
|
2011-12-03 03:17:34 +01:00
|
|
|
panic(errors.New("non-positive interval for NewTicker"))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
// 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.
|
2011-12-13 20:16:27 +01:00
|
|
|
c := make(chan Time, 1)
|
2011-01-21 19:19:03 +01:00
|
|
|
t := &Ticker{
|
2011-12-13 00:40:51 +01:00
|
|
|
C: c,
|
|
|
|
r: runtimeTimer{
|
2014-07-19 10:53:52 +02:00
|
|
|
when: when(d),
|
2011-12-13 20:16:27 +01:00
|
|
|
period: int64(d),
|
2011-12-13 00:40:51 +01:00
|
|
|
f: sendTime,
|
|
|
|
arg: c,
|
|
|
|
},
|
2011-01-21 19:19:03 +01:00
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
startTimer(&t.r)
|
2010-12-03 05:34:57 +01:00
|
|
|
return t
|
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
// Stop turns off a ticker. After Stop, no more ticks will be sent.
|
2018-09-24 23:46:21 +02:00
|
|
|
// Stop does not close the channel, to prevent a concurrent goroutine
|
|
|
|
// reading from the channel from seeing an erroneous "tick".
|
2011-12-13 00:40:51 +01:00
|
|
|
func (t *Ticker) Stop() {
|
|
|
|
stopTimer(&t.r)
|
|
|
|
}
|
|
|
|
|
2020-07-28 07:27:54 +02:00
|
|
|
// Reset stops a ticker and resets its period to the specified duration.
|
|
|
|
// The next tick will arrive after the new period elapses.
|
|
|
|
func (t *Ticker) Reset(d Duration) {
|
|
|
|
if t.r.f == nil {
|
|
|
|
panic("time: Reset called on uninitialized Ticker")
|
|
|
|
}
|
|
|
|
modTimer(&t.r, when(d), int64(d), t.r.f, t.r.arg, t.r.seq)
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:40:51 +01:00
|
|
|
// Tick is a convenience wrapper for NewTicker providing access to the ticking
|
2015-10-31 01:59:47 +01:00
|
|
|
// channel only. 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".
|
2016-07-22 20:15:38 +02:00
|
|
|
// Unlike NewTicker, Tick will return nil if d <= 0.
|
2011-12-13 20:16:27 +01:00
|
|
|
func Tick(d Duration) <-chan Time {
|
|
|
|
if d <= 0 {
|
2011-12-13 00:40:51 +01:00
|
|
|
return nil
|
|
|
|
}
|
2011-12-13 20:16:27 +01:00
|
|
|
return NewTicker(d).C
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|