This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
183 lines (160 loc) · 4.33 KB
/
scheduler.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
package xysched
import (
"fmt"
"time"
"github.com/xybor/xyplatform/xylock"
)
// future interface defines a schedulable object.
type future interface {
// run calls the future's function.
run()
// Return the next time this future will be run. Leave it as nil if you do
// not want to run anymore.
next() *time.Time
// Return callback future objects, they will be sent to scheduler once this
// future completes.
callbacks() []future
// stop returns the channel which is closed if the future early stops.
stop() <-chan any
}
// Scheduler is used for scheduling future objects.
type Scheduler struct {
name string
futureQ chan future
stop chan any
sem *xylock.Semaphore
}
// NewScheduler returns Scheduler associated with the name, if it has not yet
// existed, create a new one.
//
// Any Scheduler with a non-empty name will be associated with its name. Calling
// this function twice with the same name gives you the same Scheduler. If you
// want to create different Schedulers each call, use the empty name.
func NewScheduler(name string) *Scheduler {
var sched *Scheduler
var ok bool
lock.RLockFunc(func() any {
sched, ok = schedulerManager[name]
return nil
})
if ok {
return sched
}
if name == "" {
lock.WLockFunc(func() {
name = fmt.Sprintf("scheduler-%d", anonSchedCounter)
anonSchedCounter++
})
}
sched = &Scheduler{
name: name,
futureQ: make(chan future),
stop: make(chan any),
sem: nil,
}
lock.WLockFunc(func() {
schedulerManager[name] = sched
})
go sched.start()
return sched
}
// After creates a send-only channel. Sending a future to this channel will
// add it to scheduler after a duration. If d is negative, After will send the
// future to scheduler immediately.
//
// NOTE: You should send ONLY ONE future to this channel because it is designed
// to handle one. If you try sending another, it will be blocked forever. To
// send other futures to scheduler, let call this method again.
func (s *Scheduler) After(d time.Duration) chan<- future {
if d < 0 {
d = 0
}
var c = make(chan future)
go func() {
var f future
var timer *time.Timer
var done = make(chan any)
select {
case <-s.stop:
case f = <-c:
timer = time.AfterFunc(d, func() {
s.futureQ <- f
close(done)
})
logger.Event("prepare-to-schedule").
Field("scheduler", s.name).Field("future", f).Field("after", d).
Debug()
}
select {
case <-s.stop:
if timer != nil {
timer.Stop()
}
case <-f.stop():
if timer != nil {
timer.Stop()
}
case <-done:
}
}()
return c
}
// At is a shortcut of After(time.Until(next)).
//
// NOTE: You should send ONLY ONE future to this channel because it is designed
// to handle one. If you try sending another, it will be blocked forever. To
// send other futures to scheduler, let call this method again.
func (s *Scheduler) At(next time.Time) chan<- future {
return s.After(time.Until(next))
}
// Now is a shortcut of After(0).
//
// NOTE: You should send ONLY ONE future to this channel because it is designed
// to handle one. If you try sending another, it will be blocked forever. To
// send other futures to scheduler, let call this method again.
func (s *Scheduler) Now() chan<- future {
return s.After(0)
}
// Stop terminates the scheduler and all pending futures from now on. Running
// futures still run until they complete.
func (s *Scheduler) Stop() {
logger.Event("signal-stop").Field("scheduler", s.name).Info()
close(s.stop)
}
// Singleton is a shortcut of Concurrent(1).
func (s *Scheduler) Singleton() {
s.Concurrent(1)
}
// Concurrent limits the number of running futures at the same time. By default,
// there is no limited.
func (s *Scheduler) Concurrent(n int) {
s.sem = xylock.NewSemaphore(int64(n))
logger.Event("set-concurrent").
Field("scheduler", s.name).Field("futures", n).Debug()
}
// start begins the scheduled loop.
func (s *Scheduler) start() {
logger.Event("start").Field("scheduler", s.name).Info()
var isStop = false
for !isStop {
select {
case <-s.stop:
isStop = true
case f := <-s.futureQ:
select {
case <-f.stop():
default:
if next := f.next(); next != nil {
s.At(*next) <- f
}
go s.sem.AcquireFunc(1, func() {
f.run()
for _, cb := range f.callbacks() {
s.Now() <- cb
}
})
}
}
}
logger.Event("stop").Field("scheduler", s.name).Info()
}