-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
workerpool.go
82 lines (67 loc) · 1.83 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
// Copyright 2019 spaGO Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package workerpool
import (
"context"
"os"
"os/signal"
"sync"
"syscall"
)
// WorkerPool is a structure to run a pool of worker goroutines, gracefully
// handling interrupt and termination signals.
type WorkerPool struct {
size int
ingestChan chan interface{}
jobsChan chan interface{}
}
// WorkerFunc is a function to perform a single worker job.
type WorkerFunc func(workerID int, jobData interface{})
// New returns a new WorkerPool ready-to-use.
func New(size int) *WorkerPool {
return &WorkerPool{
size: size,
ingestChan: make(chan interface{}, 1),
jobsChan: make(chan interface{}, size),
}
}
// Run runs all workers and blocks until a signal is received.
func (wp *WorkerPool) Run(workerFunc WorkerFunc) {
ctx, ctxCancelFunc := context.WithCancel(context.Background())
wg := &sync.WaitGroup{}
wg.Add(wp.size)
go wp.runConsumer(ctx)
for workerID := 0; workerID < wp.size; workerID++ {
go wp.runWorker(workerID, wg, workerFunc)
}
wp.blockUntilSignal()
ctxCancelFunc()
wg.Wait()
}
// PublishJobData adds some data to be processed by the workers.
func (wp *WorkerPool) PublishJobData(jobData interface{}) {
wp.ingestChan <- jobData
}
func (wp *WorkerPool) runWorker(workerID int, wg *sync.WaitGroup, wFunc WorkerFunc) {
defer wg.Done()
for jobData := range wp.jobsChan {
wFunc(workerID, jobData)
}
}
func (wp *WorkerPool) runConsumer(ctx context.Context) {
for {
select {
case jobData := <-wp.ingestChan:
wp.jobsChan <- jobData
case <-ctx.Done():
close(wp.jobsChan)
return
}
}
}
func (wp *WorkerPool) blockUntilSignal() {
termChan := make(chan os.Signal)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
<-termChan
}