-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.go
328 lines (302 loc) · 6.56 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
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
// Copyright (c) 2021 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
// Package scheduler implements a task scheduler.
package scheduler
import (
"runtime"
"sync"
"sync/atomic"
"time"
)
const (
threshold = 1
idleTime = time.Second
interval = time.Second
numBuckets = 256
// Unlimited represents the unlimited number of goroutines.
Unlimited = 0
)
var (
buckets = func() [numBuckets]*scheduler {
var buckets = [numBuckets]*scheduler{}
for i := 0; i < numBuckets; i++ {
buckets[i] = newScheduler(Unlimited, nil)
}
return buckets
}()
seq uint64
)
// Schedule schedules the task to an idle worker.
func Schedule(task func()) {
if task != nil {
// Hashing tasks into multiple schedulers only works in a single thread.
seq++
buckets[seq%numBuckets].Schedule(task)
}
}
// Scheduler represents a task scheduler.
type Scheduler interface {
// Schedule schedules the task to an idle worker.
Schedule(task func())
// NumWorkers returns the number of workers.
NumWorkers() int
// Close closes the task scheduler.
Close()
}
// Options represents options
type Options struct {
// Threshold option represents the threshold at which batch task is enabled.
// If the threshold option is greater than 1, tasks will be scheduled in batches.
Threshold int
// IdleTime option represents the max idle time of the worker.
IdleTime time.Duration
// Interval option represents the check interval of the worker.
Interval time.Duration
}
// DefaultOptions returns default options.
func DefaultOptions() *Options {
return &Options{
Threshold: threshold,
Interval: interval,
}
}
type schedulers struct {
sync.RWMutex
wake bool
active map[*scheduler]struct{}
}
var m = &schedulers{
active: make(map[*scheduler]struct{}),
}
func (m *schedulers) register(s *scheduler) {
m.Lock()
m.active[s] = struct{}{}
m.Unlock()
m.wakeCheck()
}
func (m *schedulers) unregister(s *scheduler) {
m.Lock()
delete(m.active, s)
m.Unlock()
}
func (m *schedulers) numSchedulers() int {
m.RLock()
num := len(m.active)
m.RUnlock()
return num
}
func (m *schedulers) wakeCheck() {
m.Lock()
if !m.wake {
m.wake = true
go func() {
ticker := time.NewTicker(interval / 10)
var checks []*scheduler
for {
select {
case <-ticker.C:
now := time.Now()
m.RLock()
for s := range m.active {
if now.Sub(s.lastCheckTime) > s.opts.Interval {
s.lastCheckTime = now
checks = append(checks, s)
}
}
m.RUnlock()
for i, s := range checks {
checks[i] = nil
s.check()
}
checks = checks[:0]
m.Lock()
if len(m.active) == 0 {
m.wake = false
ticker.Stop()
m.Unlock()
return
}
m.Unlock()
}
runtime.Gosched()
}
}()
}
m.Unlock()
}
type scheduler struct {
lock sync.RWMutex
cond sync.Cond
wg sync.WaitGroup
pending []func()
tasks int64
running map[*worker]struct{}
workers int
maxWorkers int
lastIdleTime time.Time
lastCheckTime time.Time
closed int32
opts Options
}
// New returns a new task scheduler.
func New(maxWorkers int, opts *Options) Scheduler {
return newScheduler(maxWorkers, opts)
}
func newScheduler(maxWorkers int, opts *Options) *scheduler {
// If the max number of workers is not greater than zero, the number of goroutines will not be limited.
if maxWorkers <= 0 {
maxWorkers = Unlimited
}
if opts == nil {
opts = DefaultOptions()
} else {
if opts.Interval <= 0 {
opts.Interval = interval
}
}
s := &scheduler{
maxWorkers: maxWorkers,
opts: *opts,
}
if s.opts.IdleTime > 0 {
s.running = make(map[*worker]struct{})
s.cond.L = &s.lock
m.register(s)
}
return s
}
// Schedule schedules the task to an idle worker.
func (s *scheduler) Schedule(task func()) {
if atomic.LoadInt32(&s.closed) > 0 {
if task != nil {
task()
}
return
}
tasks := atomic.AddInt64(&s.tasks, 1)
s.lock.Lock()
if int(tasks) > s.workers && (s.maxWorkers == Unlimited || s.workers < s.maxWorkers) {
w := &worker{}
if s.opts.IdleTime > 0 {
s.running[w] = struct{}{}
}
s.workers++
s.wg.Add(1)
go w.run(s, task)
s.lock.Unlock()
} else {
s.pending = append(s.pending, task)
s.lock.Unlock()
if s.opts.IdleTime > 0 {
s.cond.Signal()
}
}
}
// NumWorkers returns the number of workers.
func (s *scheduler) NumWorkers() int {
s.lock.RLock()
numWorkers := s.workers
s.lock.RUnlock()
return numWorkers
}
// Close closes the task scheduler.
func (s *scheduler) Close() {
if atomic.CompareAndSwapInt32(&s.closed, 0, 1) {
s.lock.Lock()
if s.opts.IdleTime > 0 {
for w := range s.running {
w.close()
}
s.running = make(map[*worker]struct{})
}
for _, task := range s.pending {
task()
}
s.pending = s.pending[:0]
s.lock.Unlock()
if s.opts.IdleTime > 0 {
s.cond.Broadcast()
for {
if s.NumWorkers() == 0 {
break
}
s.cond.Broadcast()
time.Sleep(time.Millisecond)
}
m.unregister(s)
}
}
s.wg.Wait()
}
func (s *scheduler) check() {
s.lock.Lock()
if len(s.running) > 0 && len(s.running) > len(s.pending) {
if s.lastIdleTime.IsZero() {
s.lastIdleTime = time.Now()
} else if time.Since(s.lastIdleTime) > s.opts.IdleTime {
deletions := len(s.running) - len(s.pending)
for w := range s.running {
delete(s.running, w)
w.close()
deletions--
if deletions == 0 {
break
}
}
s.lastIdleTime = time.Time{}
s.cond.Broadcast()
}
} else {
s.lastIdleTime = time.Time{}
}
s.lock.Unlock()
}
type worker struct {
closed int32
}
func (w *worker) run(s *scheduler, task func()) {
var batch []func()
for {
if len(batch) > 0 {
for _, task = range batch {
task()
}
atomic.AddInt64(&s.tasks, -int64(len(batch)))
batch = batch[:0]
} else {
task()
atomic.AddInt64(&s.tasks, -1)
}
s.lock.Lock()
for {
if s.maxWorkers != Unlimited && s.opts.Threshold > 1 && len(s.pending) > s.maxWorkers*s.opts.Threshold {
alloc := len(s.pending) / s.maxWorkers
batch = s.pending[:alloc]
s.pending = s.pending[alloc:]
break
} else if len(s.pending) > 0 {
task = s.pending[0]
s.pending = s.pending[1:]
break
}
if s.opts.IdleTime <= 0 {
s.workers--
s.lock.Unlock()
s.wg.Done()
return
}
s.cond.Wait()
if atomic.LoadInt32(&s.closed) > 0 || atomic.LoadInt32(&w.closed) > 0 {
s.workers--
s.lock.Unlock()
s.wg.Done()
s.cond.Broadcast()
return
}
}
s.lock.Unlock()
}
}
func (w *worker) close() {
atomic.CompareAndSwapInt32(&w.closed, 0, 1)
}