-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlaborunion.go
367 lines (306 loc) · 8.11 KB
/
laborunion.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
package laborunion
import (
"fmt"
"math/rand"
"sync"
"time"
)
func New() *WorkerPool {
return &WorkerPool{
quitChan: make(chan bool),
inChan: make(chan interface{}),
retries: 3, // default
maxRetryMilliseconds: 5, // default
batchSize: 1, // default
}
}
type WorkerPool struct {
quitChan chan bool
inChan chan interface{}
inChanSize int
// hooks
beforeBatchingHook func()
afterBatchingHook func([]interface{})
onFailedWork func(error)
onSuccessWork func([]interface{})
onNewWorker func()
onDeleteWorker func()
worker func([]interface{}) error
workerCount int
retries int
maxRetryMilliseconds int
batchSize int
mtx sync.RWMutex
}
// ------------------------------------
// Hooks
// ------------------------------------
// SetBeforeBatchingHook sets function to be executed before batching.
func (p *WorkerPool) SetBeforeBatchingHook(f func()) {
p.mtx.Lock()
p.beforeBatchingHook = f
p.mtx.Unlock()
}
// GetBeforeBatchingHook returns function to be executed before batching.
func (p *WorkerPool) GetBeforeBatchingHook() func() {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.beforeBatchingHook
}
// SetAfterBatchingHook sets function to be executed after batching.
func (p *WorkerPool) SetAfterBatchingHook(f func([]interface{})) {
p.mtx.Lock()
p.afterBatchingHook = f
p.mtx.Unlock()
}
// GetAfterBatchingHook returns function to be executed after batching.
func (p *WorkerPool) GetAfterBatchingHook() func([]interface{}) {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.afterBatchingHook
}
// SetOnNewWorker returns function to be executed when a worker is created.
func (p *WorkerPool) SetOnNewWorker(f func()) {
p.mtx.Lock()
p.onNewWorker = f
p.mtx.Unlock()
}
// GetOnNewWorker returns function to be executed when a worker is created.
func (p *WorkerPool) GetOnNewWorker() func() {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.onNewWorker
}
// SetOnDeleteWorker returns function to be executed when a worker is deleted.
func (p *WorkerPool) SetOnDeleteWorker(f func()) {
p.mtx.Lock()
p.onDeleteWorker = f
p.mtx.Unlock()
}
// GetOnDeleteWorker returns function to be executed when a worker is deleted.
func (p *WorkerPool) GetOnDeleteWorker() func() {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.onDeleteWorker
}
// SetOnFailedWork sets function to be executed when worker failed to finish.
func (p *WorkerPool) SetOnFailedWork(f func(error)) {
p.mtx.Lock()
p.onFailedWork = f
p.mtx.Unlock()
}
// GetOnFailedWork returns function to be executed when worker failed to finish.
func (p *WorkerPool) GetOnFailedWork() func(error) {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.onFailedWork
}
// SetOnSuccessWork sets function to be executed when worker finished successfully.
func (p *WorkerPool) SetOnSuccessWork(f func([]interface{})) {
p.mtx.Lock()
p.onSuccessWork = f
p.mtx.Unlock()
}
// GetOnSuccessWork returns function to be executed when worker finished successfully.
func (p *WorkerPool) GetOnSuccessWork() func([]interface{}) {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.onSuccessWork
}
// Run pulls tasks and use worker function to process the task.
// It must be executed as goroutine because it blocks forever.
func (p *WorkerPool) Run() error {
if p.GetWorker() == nil {
return fmt.Errorf("WorkerPool is missing the worker function")
}
for {
// 1. Execute custom logic before batching happens.
if p.GetBeforeBatchingHook() != nil {
p.GetBeforeBatchingHook()()
}
tasks := make([]interface{}, 0)
// 2. Pull N number of tasks from InChan and batch them into a slice.
for i := 0; i < p.GetBatchSize(); i++ {
select {
case task, ok := <-p.GetInChan():
if ok {
tasks = append(tasks, task)
}
case <-p.GetQuitChan():
return nil
}
}
// 3. Execute custom logic after batching happens.
if p.GetAfterBatchingHook() != nil {
p.GetAfterBatchingHook()(tasks)
}
// 4. Loop through every single task and executes them.
var err error
if p.GetRetries() > 0 {
for i := 0; i < p.GetRetries(); i++ {
err = p.GetWorker()(tasks)
if err == nil {
break
}
// Sleep between retrying
time.Sleep(p.RetryDuration())
}
} else {
err = p.GetWorker()(tasks)
}
if err != nil {
if p.GetOnFailedWork() != nil {
p.GetOnFailedWork()(err)
}
} else {
if p.GetOnSuccessWork() != nil {
p.GetOnSuccessWork()(tasks)
}
}
}
return nil
}
// SetWorkerCount updates the total number of workers as well as adding/removing workers.
// It returns number of added/removed workers.
func (p *WorkerPool) SetWorkerCount(newCount int) int {
oldCount := p.workerCount
diff := newCount - oldCount
p.mtx.Lock()
p.workerCount = newCount
p.mtx.Unlock()
counter := 0
if diff < 0 {
for i := 0; i < diff*-1; i++ {
p.GetQuitChan() <- true
if p.GetOnDeleteWorker() != nil {
p.GetOnDeleteWorker()()
}
p.mtx.Lock()
counter += 1
p.mtx.Unlock()
}
} else if diff > 0 {
for i := 0; i < diff; i++ {
go p.Run()
if p.GetOnNewWorker() != nil {
p.GetOnNewWorker()()
}
p.mtx.Lock()
counter += 1
p.mtx.Unlock()
}
}
return counter
}
// GetWorkerCount returns the total number of workers.
func (p *WorkerPool) GetWorkerCount() int {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.workerCount
}
// RetryDuration returns randomized duration for retry sleep.
func (p *WorkerPool) RetryDuration() time.Duration {
return time.Duration(rand.Intn(p.GetMaxRetryMilliseconds())) * time.Millisecond
}
// SetMaxRetryMilliseconds sets maxRetryMilliseconds
func (p *WorkerPool) SetMaxRetryMilliseconds(ms int) {
p.mtx.Lock()
p.maxRetryMilliseconds = ms
p.mtx.Unlock()
}
// GetMaxRetryMilliseconds returns maxRetryMilliseconds
func (p *WorkerPool) GetMaxRetryMilliseconds() int {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.maxRetryMilliseconds
}
// SetRetries sets the number of retries.
func (p *WorkerPool) SetRetries(retries int) {
p.mtx.Lock()
p.retries = retries
p.mtx.Unlock()
}
// GetRetries returns the number of retries.
func (p *WorkerPool) GetRetries() int {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.retries
}
// SetBatchSize sets the batch size.
func (p *WorkerPool) SetBatchSize(batchSize int) {
p.mtx.Lock()
p.batchSize = batchSize
p.mtx.Unlock()
}
// GetBatchSize returns the batch size.
func (p *WorkerPool) GetBatchSize() int {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.batchSize
}
// SetQuitChan sets the quit channel.
func (p *WorkerPool) SetQuitChan(quitChan chan bool) {
p.mtx.Lock()
p.quitChan = quitChan
p.mtx.Unlock()
}
// SetQuitChan returns the quit channel.
func (p *WorkerPool) GetQuitChan() chan bool {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.quitChan
}
// ResizeInChan destroys the old imput channel and create a new input channel with the new size.
func (p *WorkerPool) ResizeInChan(inChanSize int) {
// 1. Kill all existing workers
if p.GetWorkerCount() > 0 {
for i := 0; i < p.GetWorkerCount(); i++ {
p.GetQuitChan() <- true
}
}
// 2. Close old channel
if p.GetInChan() != nil {
close(p.inChan)
}
// 3. Create a new channel
// if size > 0, create a buffered channel
// if size == 0, create an unbuffered channel
var newChan chan interface{}
if inChanSize > 0 {
newChan = make(chan interface{}, inChanSize)
} else {
newChan = make(chan interface{})
}
p.mtx.Lock()
p.inChan = newChan
p.inChanSize = inChanSize
p.mtx.Unlock()
// 4. Spawn new workers
for i := 0; i < p.GetWorkerCount(); i++ {
go p.Run()
}
}
// GetInChan returns the input channel.
func (p *WorkerPool) GetInChan() chan interface{} {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.inChan
}
// GetInChanSize returns the size of input channel.
func (p *WorkerPool) GetInChanSize() int {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.inChanSize
}
// SetWorker sets worker (defined as function hook).
func (p *WorkerPool) SetWorker(worker func([]interface{}) error) {
p.mtx.Lock()
p.worker = worker
p.mtx.Unlock()
}
// GetWorker returns worker (defined as function hook).
func (p *WorkerPool) GetWorker() func([]interface{}) error {
p.mtx.RLock()
defer p.mtx.RUnlock()
return p.worker
}