-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkerpool.go
549 lines (442 loc) · 13.2 KB
/
workerpool.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package workerpool
import (
"fmt"
"runtime"
"runtime/debug"
"sync"
"sync/atomic"
"time"
)
type WorkerPool struct {
name string
minWorkers int
maxWorkers int
idleTimeout time.Duration
upScaling int
downScaling int
queue chan func()
panicHandler func(panicErr interface{})
metrics *Metrics
runningWorkers int32
idleWorkers int32
submittedTasks uint64
waitingTasks uint64
successfulTasks uint64
failedTasks uint64
// properties
stopOnce sync.Once
workerWG sync.WaitGroup
stopped bool
// events
idleTimeoutChanged chan struct{}
// signals
stoppedSignal chan struct{}
purgeSignal chan struct{}
}
func New(name string, settings *Settings) *WorkerPool {
wp := &WorkerPool{
name: name,
minWorkers: settings.MinWorkers,
maxWorkers: settings.MaxWorkers,
idleTimeout: settings.IdleTimeout,
upScaling: settings.UpScaling,
downScaling: settings.DownScaling,
panicHandler: settings.PanicHandler,
stoppedSignal: make(chan struct{}),
purgeSignal: make(chan struct{}),
idleTimeoutChanged: make(chan struct{}, 1),
}
wp.metrics = &Metrics{wp: wp}
if wp.maxWorkers < 1 {
if wp.minWorkers > 0 {
wp.maxWorkers = wp.minWorkers
} else {
wp.maxWorkers = runtime.NumCPU()
}
}
if wp.minWorkers > wp.maxWorkers {
wp.minWorkers = wp.maxWorkers
} else if wp.minWorkers < 0 {
wp.minWorkers = 0
}
if wp.idleTimeout < 1 {
wp.idleTimeout = defaultIdleTimeout
}
if wp.upScaling < 1 {
wp.upScaling = 1
}
if wp.downScaling < 1 {
wp.downScaling = 1
}
if settings.Queue < 0 {
wp.queue = make(chan func())
} else {
wp.queue = make(chan func(), settings.Queue)
}
if wp.panicHandler == nil {
wp.panicHandler = defaultPanicHandler
}
wp.createWorkers(wp.minWorkers).Wait()
go wp.monitor()
return wp
}
// Submit sends a task to run asynchronous in the WorkerPool.
// If the queue is full, it will block until a task is dispatched to a worker.
func (wp *WorkerPool) Submit(task func()) {
wp.submit(task, true, false, nil)
}
// SubmitAndWait sends a task to run asynchronous in the WorkerPool and waits for it to complete.
func (wp *WorkerPool) SubmitAndWait(task func()) {
wp.submit(task, true, true, nil)
}
// SubmitAndWaitWithTimeout sends a task to run asynchronous in the WorkerPool and waits for it to complete or until the defined timeout.
func (wp *WorkerPool) SubmitAndWaitWithTimeout(timeout time.Duration, task func()) (finished bool, wait <-chan struct{}) {
return wp.SubmitAndWaitWithDeadline(time.Now().Add(timeout), task)
}
// SubmitAndWaitWithDeadline sends a task to run asynchronous in the WorkerPool and waits for it to complete or until the defined deadline.
func (wp *WorkerPool) SubmitAndWaitWithDeadline(deadline time.Time, task func()) (finished bool, wait <-chan struct{}) {
_, finished, wait = wp.submit(task, true, true, &deadline)
return
}
// TrySubmit Attempts to send a task to run asynchronous in the WorkerPool.
// If the queue has capacity or have available workers, the task will run asynchronously and returns true. Otherwise, the task will not run and will return false.
func (wp *WorkerPool) TrySubmit(task func()) (submitted bool) {
submitted, _, _ = wp.submit(task, false, false, nil)
return
}
// TrySubmitAndWait Attempts to send a task to run asynchronous in the WorkerPool and waits for it to complete.
// If the queue has capacity or have available workers, the task will run asynchronously and returns true. Otherwise, the task will not run and will return false.
func (wp *WorkerPool) TrySubmitAndWait(task func()) (submitted bool) {
submitted, _, _ = wp.submit(task, false, true, nil)
return
}
// TrySubmitAndWaitWithTimeout Attempts to send a task to run asynchronous in the WorkerPool and waits for it to complete or until the defined timeout.
// If the queue has capacity or have available workers, the task will run asynchronously and returns true. Otherwise, the task will not run and will return false.
func (wp *WorkerPool) TrySubmitAndWaitWithTimeout(timeout time.Duration, task func()) (submitted bool, finished bool, wait <-chan struct{}) {
return wp.TrySubmitAndWaitWithDeadline(time.Now().Add(timeout), task)
}
// TrySubmitAndWaitWithDeadline Attempts to send a task to run asynchronous in the WorkerPool and waits for it to complete or until the defined deadline.
// If the queue has capacity or have available workers, the task will run asynchronously and returns true. Otherwise, the task will not run and will return false.
func (wp *WorkerPool) TrySubmitAndWaitWithDeadline(deadline time.Time, task func()) (submitted bool, finished bool, wait <-chan struct{}) {
submitted, finished, wait = wp.submit(task, false, true, &deadline)
return
}
// Burst will create the specified number of workers even above the WorkerPool limit (WorkerPool.MaxWorkers).
func (wp *WorkerPool) Burst(workers int) {
if workers < 1 {
return
}
wp.createWorkers(workers)
}
// ScaleUp will create the specified number of workers until the WorkerPool limit (WorkerPool.MaxWorkers).
func (wp *WorkerPool) ScaleUp(workers int) int {
if workers < 1 {
return 0
}
availableWorkersToBeCreated := wp.maxWorkers - wp.metrics.RunningWorkers()
if availableWorkersToBeCreated < 1 {
return 0
}
if workers > availableWorkersToBeCreated {
workers = availableWorkersToBeCreated
}
wp.createWorkers(workers)
return workers
}
// ScaleUp will purge the specified number of workers until the WorkerPool limit (WorkerPool.MinWorkers).
func (wp *WorkerPool) ScaleDown(workers int) int {
if workers < 1 {
return 0
}
maxWorkersToBePurged := wp.metrics.RunningWorkers() - wp.minWorkers
if maxWorkersToBePurged < 1 {
return 0
}
if workers > maxWorkersToBePurged {
workers = maxWorkersToBePurged
}
for i := 0; i < workers; i++ {
wp.purgeSignal <- struct{}{}
}
return workers
}
// ReleaseIdleWorkers will purge all idle workers.
func (wp *WorkerPool) ReleaseIdleWorkers() int {
workersToBePurged := wp.metrics.IdleWorkers() - wp.minWorkers
if workersToBePurged < 1 {
return 0
}
for i := 0; i < workersToBePurged; i++ {
wp.purgeSignal <- struct{}{}
}
return workersToBePurged
}
// Stop will stop the worker and return.
func (wp *WorkerPool) Stop() {
wp.stopOnce.Do(func() {
wp.stopped = true
close(wp.stoppedSignal)
})
}
// StopAndWait will stop the worker and return when all workers finished their tasks.
func (wp *WorkerPool) StopAndWait() {
wp.Stop()
wp.workerWG.Wait()
}
// StopAndWaitWithTimeout will stop the worker and return when all workers finished their tasks or until timeout.
func (wp *WorkerPool) StopAndWaitWithTimeout(timeout time.Duration) (finished bool, wait <-chan struct{}) {
return wp.StopAndWaitWithDeadline(time.Now().Add(timeout))
}
// StopAndWaitWithDeadline will stop the worker and return when all workers finished their tasks or until deadline.
func (wp *WorkerPool) StopAndWaitWithDeadline(deadline time.Time) (finished bool, wait <-chan struct{}) {
wp.Stop()
done := make(chan struct{})
go func() {
wp.workerWG.Wait()
close(done)
}()
select {
case <-done:
// stopped before deadline
return true, done
case <-time.After(time.Until(deadline)):
// deadline expired, then unlock
return false, done
}
}
func (wp *WorkerPool) Stopped() bool {
return wp.stopped
}
func (wp *WorkerPool) Metrics() *Metrics {
return wp.metrics
}
func (wp *WorkerPool) MinWorkers() int {
return wp.minWorkers
}
func (wp *WorkerPool) SetMinWorkers(minWorkers int) int {
if minWorkers < 0 {
return 0
}
if minWorkers > wp.maxWorkers {
wp.maxWorkers = minWorkers
}
workersToBeCreated := minWorkers - wp.minWorkers
wp.minWorkers = minWorkers
if workersToBeCreated < 1 {
return 0
}
wp.createWorkers(workersToBeCreated)
return workersToBeCreated
}
func (wp *WorkerPool) MaxWorkers() int {
return wp.maxWorkers
}
func (wp *WorkerPool) SetMaxWorkers(maxWorkers int) int {
if maxWorkers < 1 {
return 0
}
if wp.minWorkers > maxWorkers {
wp.minWorkers = maxWorkers
}
workersToBePurged := wp.metrics.RunningWorkers() - maxWorkers
wp.maxWorkers = maxWorkers
if workersToBePurged < 1 {
return 0
}
for i := 0; i < workersToBePurged; i++ {
wp.purgeSignal <- struct{}{}
}
return workersToBePurged
}
func (wp *WorkerPool) IdleTimeout() time.Duration {
return wp.idleTimeout
}
func (wp *WorkerPool) SetIdleTimeout(idleTimeout time.Duration) {
if idleTimeout < 1 {
idleTimeout = defaultIdleTimeout
}
wp.idleTimeout = idleTimeout
wp.idleTimeoutChanged <- struct{}{}
}
func (wp *WorkerPool) UpScaling() int {
return wp.upScaling
}
func (wp *WorkerPool) SetUpScaling(upScaling int) {
if upScaling < 1 {
upScaling = 1
}
wp.upScaling = upScaling
}
func (wp *WorkerPool) DownScaling() int {
return wp.downScaling
}
func (wp *WorkerPool) SetDownScaling(downScaling int) {
if downScaling < 1 {
downScaling = 1
}
wp.downScaling = downScaling
}
func (wp *WorkerPool) QueueCapacity() int {
return cap(wp.queue)
}
func (wp *WorkerPool) PanicHandler() func(panicErr interface{}) {
return wp.panicHandler
}
func (wp *WorkerPool) SetPanicHandler(panicHandler func(panicErr interface{})) {
if panicHandler == nil {
panicHandler = defaultPanicHandler
}
wp.panicHandler = panicHandler
}
func (wp *WorkerPool) String() string {
return fmt.Sprintf(workerPoolStringFormat, wp.name, wp.minWorkers, wp.maxWorkers, wp.idleTimeout.String(), wp.upScaling, wp.downScaling, cap(wp.queue))
}
func (wp *WorkerPool) createWorkers(workers int) *sync.WaitGroup {
wp.workerWG.Add(workers)
atomic.AddInt32(&wp.runningWorkers, int32(workers))
atomic.AddInt32(&wp.idleWorkers, int32(workers))
var wg sync.WaitGroup
wg.Add(workers)
for i := 0; i < workers; i++ {
go wp.worker(&wg)
}
return &wg
}
func (wp *WorkerPool) worker(wg *sync.WaitGroup) {
defer func() {
atomic.AddInt32(&wp.runningWorkers, -1)
atomic.AddInt32(&wp.idleWorkers, -1)
wp.workerWG.Done()
}()
wg.Done()
for {
select {
case <-wp.stoppedSignal:
// Worker Pool is stopped, finish
return
case <-wp.purgeSignal:
// Purge worker
return
case task := <-wp.queue:
atomic.AddInt32(&wp.idleWorkers, -1)
wp.executeTask(task)
atomic.AddInt32(&wp.idleWorkers, 1)
}
}
}
func (wp *WorkerPool) executeTask(task func()) {
defer func() {
if panicErr := recover(); panicErr != nil {
atomic.AddUint64(&wp.failedTasks, 1)
wp.panicHandler(panicErr)
}
}()
atomic.AddUint64(&wp.waitingTasks, ^uint64(0))
task()
atomic.AddUint64(&wp.successfulTasks, 1)
}
func (wp *WorkerPool) submit(task func(), mustSubmit bool, mustWait bool, waitDeadline *time.Time) (submitted bool, finished bool, doneSignal chan struct{}) {
if task == nil {
return true, true, nil
}
if wp.stopped {
if mustSubmit {
panic(fmt.Errorf("WorkerPool %s is stopped and is no longer accepting new tasks", wp.name))
}
return false, false, nil
}
if mustWait {
doneSignal = make(chan struct{})
originalTask := task
task = func() {
defer func() {
finished = true
close(doneSignal)
}()
originalTask()
}
}
atomic.AddUint64(&wp.submittedTasks, 1)
atomic.AddUint64(&wp.waitingTasks, 1)
// attempt to submit task without blocking
select {
case wp.queue <- task:
// submitted, continue
submitted = true
default:
// queue is full and no idle workers available, continue
}
// attempt to scale if no workers are available
workersScaled := 0
if wp.metrics.IdleWorkers() == 0 {
workersScaled = wp.ScaleUp(wp.upScaling)
}
// try to resubmit task
if !submitted {
// if no workers are scaled, try to scale a worker to process this task
if workersScaled == 0 {
workersScaled = wp.ScaleUp(1)
}
if mustSubmit || workersScaled > 0 {
// enqueue task if must submit or at least one worker is created to process this task
wp.queue <- task
} else {
// no workers can be created, decrement counters and return
atomic.AddUint64(&wp.submittedTasks, ^uint64(0))
atomic.AddUint64(&wp.waitingTasks, ^uint64(0))
return false, false, nil
}
}
if mustWait {
if waitDeadline == nil {
<-doneSignal
return true, true, doneSignal
} else {
select {
case <-doneSignal:
return true, true, doneSignal
case <-time.After(time.Until(*waitDeadline)):
return true, finished, doneSignal
}
}
}
return true, finished, nil
}
func (wp *WorkerPool) monitor() {
ticker := time.NewTicker(wp.idleTimeout)
defer func() {
ticker.Stop()
}()
for {
select {
case <-wp.idleTimeoutChanged:
ticker.Stop()
ticker = time.NewTicker(wp.idleTimeout)
case <-ticker.C:
wp.handlePurgeIdleWorkers()
case <-wp.stoppedSignal:
return
}
}
}
func (wp *WorkerPool) handlePurgeIdleWorkers() {
workersToBePurged := wp.downScaling
idleWorkers := wp.metrics.IdleWorkers()
if workersToBePurged > idleWorkers {
workersToBePurged = idleWorkers
}
maxPurge := wp.metrics.RunningWorkers() - wp.minWorkers
if workersToBePurged > maxPurge {
workersToBePurged = maxPurge
}
for i := 0; i < workersToBePurged; i++ {
wp.purgeSignal <- struct{}{}
}
}
func defaultPanicHandler(panicErr interface{}) {
fmt.Printf("Worker recovered from a panic: %v\nStack trace: %s\n", panicErr, string(debug.Stack()))
}
const (
workerPoolStringFormat = "WorkerPool [name=%s, minWorkers=%d, maxWorkers=%d, idleTimeout=%s, upScaling=%d, downScaling=%d, queueCapacity=%v]"
defaultIdleTimeout = 15 * time.Second
)