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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
package sched
import (
"context"
"sort"
"sync"
"sync/atomic"
"time"
"unsafe"
"codeberg.org/gruf/go-runners"
)
// Precision is the maximum time we can
// offer scheduler run-time precision down to.
const Precision = 2 * time.Millisecond
var (
// neverticks is a timer channel
// that never ticks (it's starved).
neverticks = make(chan time.Time)
// alwaysticks is a timer channel
// that always ticks (it's closed).
alwaysticks = func() chan time.Time {
ch := make(chan time.Time)
close(ch)
return ch
}()
)
// Scheduler provides a means of running jobs at specific times and
// regular intervals, all while sharing a single underlying timer.
type Scheduler struct {
svc runners.Service // svc manages the main scheduler routine
jobs []*Job // jobs is a list of tracked Jobs to be executed
jch atomic_channel // jch accepts either Jobs or job IDs to notify new/removed jobs
jid atomic.Uint64 // jid is used to iteratively generate unique IDs for jobs
}
// Start will attempt to start the Scheduler. Immediately returns false
// if the Service is already running, and true after completed run.
func (sch *Scheduler) Start() bool {
var wait sync.WaitGroup
// Use waiter to synchronize between started
// goroutine and ourselves, to ensure that
// we don't return before Scheduler init'd.
wait.Add(1)
ok := sch.svc.GoRun(func(ctx context.Context) {
// Prepare new channel.
ch := new(channel)
ch.ctx = ctx.Done()
ch.ch = make(chan interface{})
sch.jch.Store(ch)
// Release
// start fn
wait.Done()
// Main loop
sch.run(ch)
})
if ok {
// Wait on
// goroutine
wait.Wait()
} else {
// Release
wait.Done()
}
return ok
}
// Stop will attempt to stop the Scheduler. Immediately returns false
// if not running, and true only after Scheduler is fully stopped.
func (sch *Scheduler) Stop() bool {
return sch.svc.Stop()
}
// Running will return whether Scheduler is running (i.e. NOT stopped / stopping).
func (sch *Scheduler) Running() bool {
return sch.svc.Running()
}
// Done returns a channel that's closed when Scheduler.Stop() is called.
func (sch *Scheduler) Done() <-chan struct{} {
return sch.svc.Done()
}
// Schedule will add provided Job to the Scheduler, returning a cancel function.
func (sch *Scheduler) Schedule(job *Job) (cancel func()) {
if job == nil {
panic("nil job")
}
// Load job channel.
ch := sch.jch.Load()
if ch == nil {
panic("not running")
}
// Calculate next job ID.
job.id = sch.jid.Add(1)
// Pass job
// to channel.
if !ch.w(job) {
panic("not running")
}
// Return cancel function for job
return func() { ch.w(job.id) }
}
// run is the main scheduler run routine, which runs for as long as ctx is valid.
func (sch *Scheduler) run(ch *channel) {
defer ch.close()
if ch == nil {
panic("nil channel")
} else if sch == nil {
panic("nil scheduler")
}
var (
// now stores the current time, and will only be
// set when the timer channel is set to be the
// 'alwaysticks' channel. this allows minimizing
// the number of calls required to time.Now().
now time.Time
// timerset represents whether timer was running
// for a particular run of the loop. false means
// that tch == neverticks || tch == alwaysticks.
timerset bool
// timer tick channel (or always / never ticks).
tch <-chan time.Time
// timer notifies this main routine to wake when
// the job queued needs to be checked for executions.
timer *time.Timer
// stopdrain will stop and drain the timer
// if it has been running (i.e. timerset == true).
stopdrain = func() {
if timerset && !timer.Stop() {
<-timer.C
}
}
)
// Create a stopped timer.
timer = time.NewTimer(1)
<-timer.C
for {
// Reset timer state.
timerset = false
if len(sch.jobs) > 0 {
// Get now time.
now = time.Now()
// Sort by next occurring.
sort.Sort(byNext(sch.jobs))
// Get next job time.
next := sch.jobs[0].Next()
// If this job is *just* about to be ready, we don't bother
// sleeping. It's wasted cycles only sleeping for some obscenely
// tiny amount of time we can't guarantee precision for.
if until := next.Sub(now); until <= Precision/1e3 {
// This job is behind,
// set to always tick.
tch = alwaysticks
} else {
// Reset timer to period.
timer.Reset(until)
timerset = true
tch = timer.C
}
} else {
// Unset timer
tch = neverticks
}
select {
// Scheduler stopped
case <-ch.done():
stopdrain()
return
// Timer ticked,
// run scheduled.
case t, ok := <-tch:
if !ok {
// 'alwaysticks' returns zero
// times, BUT 'now' will have
// been set during above sort.
t = now
}
sch.schedule(t)
// Received update,
// handle job/id.
case v := <-ch.r():
sch.handle(v)
stopdrain()
}
}
}
// handle takes an interfaces received from Scheduler.jch and handles either:
// - Job --> new job to add.
// - uint64 --> job ID to remove.
func (sch *Scheduler) handle(v interface{}) {
switch v := v.(type) {
// New job added
case *Job:
// Get current time.
now := time.Now()
// Update next call time.
next := v.timing.Next(now)
v.next.Store(next)
// Append this job to queued/
sch.jobs = append(sch.jobs, v)
// Job removed
case uint64:
for i := 0; i < len(sch.jobs); i++ {
if sch.jobs[i].id == v {
// This is the job we're looking for! Drop this.
sch.jobs = append(sch.jobs[:i], sch.jobs[i+1:]...)
return
}
}
}
}
// schedule will iterate through the scheduler jobs and
// execute those necessary, updating their next call time.
func (sch *Scheduler) schedule(now time.Time) {
for i := 0; i < len(sch.jobs); {
// Scope our own var.
job := sch.jobs[i]
// We know these jobs are ordered by .Next(), so as soon
// as we reach one with .Next() after now, we can return.
if job.Next().After(now) {
return
}
// Run the job.
go job.Run(now)
// Update the next call time.
next := job.timing.Next(now)
job.next.Store(next)
if next.IsZero() {
// Zero time, this job is done and can be dropped.
sch.jobs = append(sch.jobs[:i], sch.jobs[i+1:]...)
continue
}
// Iter
i++
}
}
// byNext is an implementation of sort.Interface
// to sort Jobs by their .Next() time.
type byNext []*Job
func (by byNext) Len() int {
return len(by)
}
func (by byNext) Less(i int, j int) bool {
return by[i].Next().Before(by[j].Next())
}
func (by byNext) Swap(i int, j int) {
by[i], by[j] = by[j], by[i]
}
// atomic_channel wraps a *channel{} with atomic store / load.
type atomic_channel struct{ p unsafe.Pointer }
func (c *atomic_channel) Load() *channel {
if p := atomic.LoadPointer(&c.p); p != nil {
return (*channel)(p)
}
return nil
}
func (c *atomic_channel) Store(v *channel) {
atomic.StorePointer(&c.p, unsafe.Pointer(v))
}
// channel wraps both a context done
// channel and a generic interface channel
// to support safe writing to an underlying
// channel that correctly fails after close.
type channel struct {
ctx <-chan struct{}
ch chan interface{}
}
// done returns internal context channel.
func (ch *channel) done() <-chan struct{} {
return ch.ctx
}
// r returns internal channel for read.
func (ch *channel) r() chan interface{} {
return ch.ch
}
// w writes 'v' to channel, or returns false if closed.
func (ch *channel) w(v interface{}) bool {
select {
case <-ch.ctx:
return false
case ch.ch <- v:
return true
}
}
// close closes underlying channel.
func (ch *channel) close() {
close(ch.ch)
}
|