-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_pool.go
246 lines (206 loc) · 4.6 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
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
package workers
import (
"context"
"errors"
"runtime"
"sync"
"time"
)
type WorkerPoolConfig struct {
QueueSize int
WorkerSize int
// 0 for infinity time
JobTimeout time.Duration
ShutdownTimeout time.Duration
}
type workerPool struct {
jobQueue chan Job
workerSize int
queueSize int
jobTimeout time.Duration
shutdownTimeout time.Duration
status Status
statusLock sync.RWMutex
lifecycleLock sync.Mutex
wgShutdown sync.WaitGroup
shutdownChan chan ChanSignal
doneChan chan ChanSignal
}
var (
defaultQueueSize = 1024
defaultWorkerSize = runtime.NumCPU()
)
func NewWorkerPool(config WorkerPoolConfig) (Worker, error) {
if config.QueueSize <= 0 {
config.QueueSize = defaultQueueSize
}
if config.WorkerSize <= 0 {
config.WorkerSize = defaultWorkerSize
}
if config.JobTimeout < 0 {
config.JobTimeout = 0
}
if config.ShutdownTimeout < 0 {
config.ShutdownTimeout = 0
}
return &workerPool{
queueSize: config.QueueSize,
jobQueue: make(chan Job, config.QueueSize),
workerSize: config.WorkerSize,
jobTimeout: config.JobTimeout,
shutdownTimeout: config.ShutdownTimeout,
status: StatusCreated,
shutdownChan: make(chan ChanSignal),
doneChan: make(chan ChanSignal),
}, nil
}
func (w *workerPool) Start() error {
w.lifecycleLock.Lock()
defer w.lifecycleLock.Unlock()
w.statusLock.Lock()
if w.status != StatusCreated {
w.statusLock.Unlock()
return errors.New("worker has been running or already stopped")
}
w.status = StatusRunning
w.statusLock.Unlock()
for i := 0; i < w.workerSize; i++ {
w.wgShutdown.Add(1)
go w.looper(i)
}
return nil
}
func (w *workerPool) Shutdown() error {
w.lifecycleLock.Lock()
defer w.lifecycleLock.Unlock()
w.statusLock.Lock()
if w.status != StatusRunning {
w.statusLock.Unlock()
return ErrWorkerNotRunning
}
w.status = StatusStopped
w.statusLock.Unlock()
close(w.shutdownChan)
w.wgShutdown.Wait()
close(w.doneChan)
return nil
}
func (w *workerPool) Status() Status {
w.statusLock.RLock()
defer w.statusLock.RUnlock()
return w.status
}
func (w *workerPool) Done() chan ChanSignal {
return w.doneChan
}
func (w *workerPool) GetJobTimeout() time.Duration {
return w.jobTimeout
}
func (w *workerPool) GetShutdownTimeout() time.Duration {
return w.shutdownTimeout
}
func (w *workerPool) Push(job Job) error {
if w.Status() != StatusRunning {
return ErrWorkerNotRunning
}
if job == nil {
return errors.New("job is nil")
}
select {
case <-w.shutdownChan:
return ErrWorkerNotRunning
case w.jobQueue <- job:
}
return nil
}
func (w *workerPool) PushAndWait(job Job) error {
if w.Status() != StatusRunning {
return ErrWorkerNotRunning
}
if job == nil {
return errors.New("job is nil")
}
select {
case <-w.shutdownChan:
return ErrWorkerNotRunning
case w.jobQueue <- job:
}
<-job.Done()
return nil
}
func (w *workerPool) looper(workerId int) {
defer w.wgShutdown.Done()
var (
jobTimeout *time.Duration
shutdownTimeout *time.Duration
jobQueuePrio = make(chan Job, w.queueSize)
)
if w.jobTimeout > 0 {
jobTimeout = ptrDuration(w.jobTimeout)
}
if w.shutdownTimeout > 0 {
shutdownTimeout = ptrDuration(w.shutdownTimeout)
}
defer w.jobDeferDispatcher(w.jobQueue, jobTimeout)
defer w.jobDeferDispatcher(jobQueuePrio, jobTimeout)
for {
var job Job
select {
case <-w.shutdownChan:
return
case job = <-w.jobQueue:
}
if w.Status() != StatusRunning {
// in case there is problem with shutdownChan
jobQueuePrio <- job
return
}
done := make(chan ChanSignal)
go w.jobDispatcher(job, done, false, jobTimeout)
select {
case <-done:
// the job is done
case <-w.shutdownChan:
// on graceful shutdown
w.jobDispatcher(job, nil, true, shutdownTimeout)
}
}
}
func (w *workerPool) jobDispatcher(job Job, done chan ChanSignal, isCancellation bool, timeout *time.Duration) {
var (
ctx = context.Background()
cancelFunc context.CancelFunc
)
if timeout != nil {
ctx, cancelFunc = context.WithTimeout(ctx, *timeout)
}
if isCancellation {
job.Cancel(ctx)
} else {
job.Do(ctx)
}
// to make sure the job is already done
<-job.Done()
if cancelFunc != nil {
cancelFunc()
}
if done != nil {
close(done)
}
}
func (w *workerPool) jobDeferDispatcher(jobQueue chan Job, jobTimeout *time.Duration) {
for {
select {
case job := <-jobQueue:
if w.shutdownTimeout > 0 {
// force stop by setting timeout to 0
w.jobDispatcher(job, nil, false, ptrDuration(0))
} else {
// run the remaining jobs
w.jobDispatcher(job, nil, false, jobTimeout)
}
default:
return
}
}
}