-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtick.go
62 lines (56 loc) · 1.36 KB
/
tick.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package tick
import (
"time"
)
// A Ticker holds a channel that delivers `ticks' of a clock at intervals.
type Ticker struct {
C <-chan time.Time // The channel on which the ticks are delivered.
quit chan struct{}
}
// NewTicker returns a new Ticker containing a channel that will send the
// time with a period specified by the duration argument. Times are rounded
// to the period, and no ticks are dropped. Ticks are not guaranteed to be
// on time.
func NewTicker(d time.Duration) *Ticker {
var (
c = make(chan time.Time)
t = &Ticker{
quit: make(chan struct{}),
C: c,
}
)
go t.run(d, c)
return t
}
// Stop turns off a ticker. After Stop, no more ticks will be sent.
// Stop does not close the channel, to prevent a read from the channel
// succeeding incorrectly.
func (t *Ticker) Stop() {
close(t.quit)
}
func (t *Ticker) run(d time.Duration, c chan<- time.Time) {
var (
start = time.Now()
first = start.Add(d).Truncate(d)
)
time.Sleep(first.Sub(start))
ticker := time.NewTicker(d)
c <- first
next := first.Add(d)
for {
select {
case x := <-ticker.C:
for ; next.Before(x); next = next.Add(d) {
c <- next
}
case <-t.quit:
ticker.Stop()
return
}
}
}
// Tick is a convenience wrapper for NewTicker providing access to the
// ticking channel only.
func Tick(d time.Duration) <-chan time.Time {
return NewTicker(d).C
}