-
Notifications
You must be signed in to change notification settings - Fork 1
/
ticker.go
204 lines (193 loc) · 4.81 KB
/
ticker.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package pool
import (
"context"
"fmt"
"strconv"
"strings"
"sync"
"time"
"goa.design/pulse/pulse"
"goa.design/pulse/rmap"
)
type (
// Ticker represents a clock that periodically sends ticks to one of the pool nodes
// which created a ticker with the same name.
Ticker struct {
C <-chan time.Time
c chan time.Time
name string
lock sync.Mutex
tickerMap *rmap.Map
timer *time.Timer
next string // serialized next tick time in unix micro and duration
mapch <-chan rmap.EventKind
wg *sync.WaitGroup
logger pulse.Logger
}
)
// NewTicker returns a new Ticker that behaves similarly to time.Ticker, but
// instead delivers the current time on the channel to only one of the nodes
// that invoked NewTicker with the same name.
func (node *Node) NewTicker(ctx context.Context, name string, d time.Duration, opts ...TickerOption) (*Ticker, error) {
if node.clientOnly {
return nil, fmt.Errorf("cannot create ticker on client-only node")
}
name = node.Name + ":" + name
o := parseTickerOptions(opts...)
logger := o.logger
if logger == nil {
logger = pulse.NoopLogger()
}
c := make(chan time.Time)
t := &Ticker{
C: c,
c: c,
name: name,
tickerMap: node.tickerMap,
mapch: node.tickerMap.Subscribe(),
wg: &sync.WaitGroup{},
logger: logger,
}
if current, ok := node.tickerMap.Get(name); ok {
t.next = current
} else {
next := serialize(time.Now().Add(d), d)
if _, err := t.tickerMap.Set(ctx, t.name, next); err != nil {
return nil, fmt.Errorf("failed to store tick and duration: %s", err)
}
t.next = next
}
t.initTimer()
t.wg.Add(1)
go t.handleEvents()
return t, nil
}
// Reset stops a ticker and resets its period to the specified duration. The
// next tick will arrive after the new period elapses. The duration d must be
// greater than zero; if not, Reset will panic.
func (t *Ticker) Reset(d time.Duration) {
t.lock.Lock()
defer t.lock.Unlock()
t.next = serialize(time.Now().Add(d), d)
_, err := t.tickerMap.Set(context.Background(), t.name, t.next)
if err != nil {
t.logger.Error(err, "msg", "failed to reset ticker")
return
}
t.initTimer()
if t.mapch == nil {
// ticker was previously stopped, restart it.
t.mapch = t.tickerMap.Subscribe()
t.wg.Add(1)
go t.handleEvents()
}
}
// Stop turns off a ticker. After Stop, no more ticks will be sent. Stop does
// not close the channel, to prevent a concurrent goroutine reading from the
// channel from seeing an erroneous "tick".
func (t *Ticker) Stop() {
t.lock.Lock()
t.timer.Stop()
t.tickerMap.Delete(context.Background(), t.name)
t.tickerMap.Unsubscribe(t.mapch)
t.mapch = nil
t.lock.Unlock()
t.wg.Wait()
}
// handleEvents handles events from the ticker timer and map.
func (t *Ticker) handleEvents() {
defer t.wg.Done()
ch := t.mapch
for {
select {
case _, ok := <-ch:
if !ok {
t.logger.Info("stopped locally")
return
}
next, ok := t.tickerMap.Get(t.name)
if !ok {
t.logger.Info("stopped remotely")
t.lock.Lock()
t.tickerMap.Unsubscribe(t.mapch)
t.mapch = nil
t.lock.Unlock()
return
}
t.lock.Lock()
if next == t.next {
// No change.
t.lock.Unlock()
continue
}
t.next = next
t.initTimer()
t.lock.Unlock()
case <-t.timer.C:
t.handleTick()
}
}
}
// handleTick sends the current time on the channel.
func (t *Ticker) handleTick() {
t.lock.Lock()
defer t.lock.Unlock()
ts, d := deserialize(t.next)
ts = ts.Add(d)
for ts.Before(time.Now()) {
ts = ts.Add(d)
}
next := serialize(ts, d)
prev, err := t.tickerMap.TestAndSet(context.Background(), t.name, t.next, next)
if err != nil {
t.logger.Error(err, "msg", "failed to update next tick")
return
}
if prev != t.next {
// Another node already updated the ticker, restart the timer.
t.next = prev
t.initTimer()
return
}
t.next = next
t.initTimer()
select {
case t.c <- time.Now():
default:
}
}
// initTimer sets the timer to fire at the next tick.
func (t *Ticker) initTimer() {
next, _ := deserialize(t.next)
d := next.Sub(time.Now())
if d < 0 {
d = 0
}
if t.timer == nil {
t.timer = time.NewTimer(d)
return
}
if !t.timer.Stop() {
select {
case <-t.timer.C:
default:
}
}
t.timer.Reset(d)
}
// serialize returns a serialized representation of the given time and duration.
func serialize(t time.Time, d time.Duration) string {
ts := strconv.FormatInt(t.UnixMicro(), 10)
ds := d.String()
return ts + "|" + ds
}
// deserialize returns the time and duration represented by the given serialized
// string. s must be a value returned by serialize, the behavior is undefined
// otherwise.
func deserialize(s string) (time.Time, time.Duration) {
parts := strings.Split(s, "|")
ts, _ := strconv.ParseInt(parts[0], 10, 64)
t := time.UnixMicro(ts)
d, _ := time.ParseDuration(parts[1])
return t, d
}