-
Notifications
You must be signed in to change notification settings - Fork 390
/
cycle.go
219 lines (179 loc) · 4.74 KB
/
cycle.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package sync2
import (
"context"
"sync"
"sync/atomic"
"time"
"golang.org/x/sync/errgroup"
)
// Cycle implements a controllable recurring event.
//
// Cycle control methods PANICS after Close has been called and don't have any
// effect after Stop has been called.
//
// Start or Run (only of of them, not both) must be only called once.
type Cycle struct {
stopsent int32
runexec int32
interval time.Duration
ticker *time.Ticker
control chan interface{}
stopping chan struct{}
stopped chan struct{}
init sync.Once
}
type (
// cycle control messages
cyclePause struct{}
cycleContinue struct{}
cycleChangeInterval struct{ Interval time.Duration }
cycleTrigger struct{ done chan struct{} }
)
// NewCycle creates a new cycle with the specified interval.
func NewCycle(interval time.Duration) *Cycle {
cycle := &Cycle{}
cycle.SetInterval(interval)
return cycle
}
// SetInterval allows to change the interval before starting.
func (cycle *Cycle) SetInterval(interval time.Duration) {
cycle.interval = interval
}
func (cycle *Cycle) initialize() {
cycle.init.Do(func() {
cycle.stopped = make(chan struct{})
cycle.stopping = make(chan struct{})
cycle.control = make(chan interface{})
})
}
// Start runs the specified function with an errgroup.
func (cycle *Cycle) Start(ctx context.Context, group *errgroup.Group, fn func(ctx context.Context) error) {
atomic.CompareAndSwapInt32(&cycle.runexec, 0, 1)
group.Go(func() error {
return cycle.Run(ctx, fn)
})
}
// Run runs the specified in an interval.
//
// Every interval `fn` is started.
// When `fn` is not fast enough, it may skip some of those executions.
//
// Run PANICS if it's called after Stop has been called.
func (cycle *Cycle) Run(ctx context.Context, fn func(ctx context.Context) error) error {
atomic.CompareAndSwapInt32(&cycle.runexec, 0, 1)
cycle.initialize()
defer close(cycle.stopped)
currentInterval := cycle.interval
cycle.ticker = time.NewTicker(currentInterval)
defer cycle.ticker.Stop()
if err := fn(ctx); err != nil {
return err
}
for {
// prioritize stopping messages
select {
case <-cycle.stopping:
return nil
case <-ctx.Done():
// handle control messages
return ctx.Err()
default:
}
// handle other messages as well
select {
case message := <-cycle.control:
// handle control messages
switch message := message.(type) {
case cycleChangeInterval:
currentInterval = message.Interval
cycle.ticker.Stop()
cycle.ticker = time.NewTicker(currentInterval)
case cyclePause:
cycle.ticker.Stop()
// ensure we don't have ticks left
select {
case <-cycle.ticker.C:
default:
}
case cycleContinue:
cycle.ticker.Stop()
cycle.ticker = time.NewTicker(currentInterval)
case cycleTrigger:
// trigger the function
if err := fn(ctx); err != nil {
return err
}
if message.done != nil {
close(message.done)
}
}
case <-cycle.stopping:
return nil
case <-ctx.Done():
// handle control messages
return ctx.Err()
case <-cycle.ticker.C:
// trigger the function
if err := fn(ctx); err != nil {
return err
}
}
}
}
// Close closes all resources associated with it.
//
// It MUST NOT be called concurrently.
func (cycle *Cycle) Close() {
cycle.Stop()
if atomic.LoadInt32(&cycle.runexec) == 1 {
<-cycle.stopped
}
close(cycle.control)
}
// sendControl sends a control message
func (cycle *Cycle) sendControl(message interface{}) {
cycle.initialize()
select {
case cycle.control <- message:
case <-cycle.stopped:
}
}
// Stop stops the cycle permanently
func (cycle *Cycle) Stop() {
cycle.initialize()
if atomic.CompareAndSwapInt32(&cycle.stopsent, 0, 1) {
close(cycle.stopping)
}
if atomic.LoadInt32(&cycle.runexec) == 1 {
<-cycle.stopped
}
}
// ChangeInterval allows to change the ticker interval after it has started.
func (cycle *Cycle) ChangeInterval(interval time.Duration) {
cycle.sendControl(cycleChangeInterval{interval})
}
// Pause pauses the cycle.
func (cycle *Cycle) Pause() {
cycle.sendControl(cyclePause{})
}
// Restart restarts the ticker from 0.
func (cycle *Cycle) Restart() {
cycle.sendControl(cycleContinue{})
}
// Trigger ensures that the loop is done at least once.
// If it's currently running it waits for the previous to complete and then runs.
func (cycle *Cycle) Trigger() {
cycle.sendControl(cycleTrigger{})
}
// TriggerWait ensures that the loop is done at least once and waits for completion.
// If it's currently running it waits for the previous to complete and then runs.
func (cycle *Cycle) TriggerWait() {
done := make(chan struct{})
cycle.sendControl(cycleTrigger{done})
select {
case <-done:
case <-cycle.stopped:
}
}