-
Notifications
You must be signed in to change notification settings - Fork 14
/
worker_pool.go
147 lines (123 loc) · 2.89 KB
/
worker_pool.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
package gitcollector
import (
"sync"
"time"
)
// WorkerPoolOpts are configuration options for a JobScheduler.
type WorkerPoolOpts struct {
SchedulerCapacity int
ScheduleJobTimeout time.Duration
NotWaitNewJobs bool
Metrics MetricsCollector
}
// WorkerPool holds a pool of workers to process Jobs.
type WorkerPool struct {
scheduler *jobScheduler
workers []*worker
resize chan struct{}
wg sync.WaitGroup
opts *WorkerPoolOpts
}
// NewWorkerPool builds a new WorkerPool.
func NewWorkerPool(
schedule JobScheduleFn,
opts *WorkerPoolOpts,
) *WorkerPool {
resize := make(chan struct{}, 1)
resize <- struct{}{}
if opts.Metrics == nil {
opts.Metrics = &hollowMetricsCollector{}
}
return &WorkerPool{
scheduler: newJobScheduler(schedule, opts),
resize: resize,
opts: opts,
}
}
// Run notify workers to start.
func (wp *WorkerPool) Run() {
go wp.opts.Metrics.Start()
go wp.scheduler.Schedule()
}
// Size returns the current number of workers in the pool.
func (wp *WorkerPool) Size() int {
<-wp.resize
defer func() { wp.resize <- struct{}{} }()
return len(wp.workers)
}
// SetWorkers set the number of Workers in the pool to n.
func (wp *WorkerPool) SetWorkers(n int) {
<-wp.resize
defer func() { wp.resize <- struct{}{} }()
if n < 0 {
n = 0
}
diff := n - len(wp.workers)
if diff == 0 {
return
} else if diff > 0 {
wp.add(diff)
} else {
wp.remove(-diff)
}
}
func (wp *WorkerPool) add(n int) {
wp.wg.Add(n)
for i := 0; i < n; i++ {
w := newWorker(wp.scheduler.jobs, wp.opts.Metrics)
go func() {
w.start()
wp.wg.Done()
}()
wp.workers = append(wp.workers, w)
}
}
func (wp *WorkerPool) remove(n int) {
var (
i = len(wp.workers) - n
workersToStop = wp.workers[i:]
wg sync.WaitGroup
)
wg.Add(len(workersToStop))
for _, w := range workersToStop {
worker := w
go func() {
worker.stop(false)
wg.Done()
}()
}
wp.workers = wp.workers[:i]
wg.Wait()
}
// Wait waits for the workers to finish.
func (wp *WorkerPool) Wait() {
wp.wg.Wait()
wp.workers = nil
wp.opts.Metrics.Stop(false)
}
// Close stops all the workers in the pool waiting for the jobs to finish.
func (wp *WorkerPool) Close() {
wp.SetWorkers(0)
wp.wg.Wait()
wp.scheduler.finish()
wp.opts.Metrics.Stop(false)
}
// Stop stops all the workers in the pool immediately.
func (wp *WorkerPool) Stop() {
<-wp.resize
defer func() { wp.resize <- struct{}{} }()
for _, w := range wp.workers {
w.stop(true)
}
wp.wg.Wait()
wp.workers = nil
wp.scheduler.finish()
wp.opts.Metrics.Stop(true)
}
type hollowMetricsCollector struct{}
var _ MetricsCollector = (*hollowMetricsCollector)(nil)
func (mc *hollowMetricsCollector) Start() {}
func (mc *hollowMetricsCollector) Stop(bool) {}
func (mc *hollowMetricsCollector) Success(Job) {}
func (mc *hollowMetricsCollector) Fail(Job) {}
func (mc *hollowMetricsCollector) Discover(Job) {}