-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
214 lines (198 loc) · 5.18 KB
/
worker.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
package workerpool
import (
"context"
"errors"
"github.com/ebarti/utils"
"os"
"os/signal"
"reflect"
"sync"
)
const (
sigChanBufferSize = 1
)
type WorkerPool interface {
Send(interface{})
ReceiveFrom(t reflect.Type, inWorker ...WorkerPool) WorkerPool
Work() WorkerPool
OutChannel(t reflect.Type, out chan interface{})
CancelOnSignal(signals ...os.Signal) WorkerPool
Close() error
}
// Task : interface to be implemented by a desired type
// Its Run method is called by workerPool onceErr the workers start
// and there is data sent to the channel via its Send method
type Task interface {
Run(in interface{}, out chan<- interface{}) error
}
// workerPool : a pool of workers that asynchronously execute a given task
type workerPool struct {
Ctx context.Context
workerTask Task
err error
numberOfWorkers int64
inChan chan interface{}
internalOutChan chan interface{}
outTypedChan map[reflect.Type][]chan interface{}
sigChan chan os.Signal
cancel context.CancelFunc
semaphore chan struct{}
isLeader bool
wg *sync.WaitGroup
onceErr *sync.Once
onceCloseOut *sync.Once
}
// NewWorkerPool : workerPool factory. Needs a defined number of workers to instantiate.
func NewWorkerPool(ctx context.Context, workerFunction Task, numberOfWorkers int64) WorkerPool {
c, cancel := context.WithCancel(ctx)
return &workerPool{
numberOfWorkers: numberOfWorkers,
Ctx: c,
workerTask: workerFunction,
inChan: make(chan interface{}, numberOfWorkers),
internalOutChan: make(chan interface{}, numberOfWorkers),
sigChan: make(chan os.Signal, sigChanBufferSize),
outTypedChan: make(map[reflect.Type][]chan interface{}),
cancel: cancel,
semaphore: make(chan struct{}, numberOfWorkers),
isLeader: true,
wg: new(sync.WaitGroup),
onceErr: new(sync.Once),
onceCloseOut: new(sync.Once),
}
}
// Send : send to workers in channel
func (wp *workerPool) Send(in interface{}) {
select {
case <-wp.Ctx.Done():
return
case wp.inChan <- in:
return
}
}
// ReceiveFrom : assigns workers out channel to this workers in channel
func (wp *workerPool) ReceiveFrom(t reflect.Type, inWorker ...WorkerPool) WorkerPool {
wp.isLeader = false
for _, worker := range inWorker {
worker.OutChannel(t, wp.inChan)
}
return wp
}
// OutChannel : Sets the workers output channel to one provided.
// This can only be called onceErr per pool or an error will be returned.
func (wp *workerPool) OutChannel(t reflect.Type, out chan interface{}) {
if _, ok := wp.outTypedChan[t]; !ok {
var outs []chan interface{}
outs = append(outs, out)
wp.outTypedChan[t] = outs
} else {
wp.outTypedChan[t] = append(wp.outTypedChan[t], out)
}
}
// Work : starts workers
func (wp *workerPool) Work() WorkerPool {
wp.wg.Add(1)
wp.runOutChanMux()
go func() {
var wg = new(sync.WaitGroup)
defer func() {
wg.Wait()
wp.wg.Done()
}()
for in := range wp.inChan {
select {
case <-wp.Ctx.Done():
wp.err = context.Canceled
continue
default:
wp.semaphore <- struct{}{}
wg.Add(1)
go func(in interface{}) {
defer func() {
<-wp.semaphore
wg.Done()
}()
if err := wp.workerTask.Run(in, wp.internalOutChan); err != nil {
wp.onceErr.Do(func() {
wp.err = err
wp.cancel()
})
return
}
}(in)
}
}
}()
return wp
}
// out : pushes value to workers out channel
// Used when chaining worker pools.
func (wp *workerPool) out(out interface{}) error {
selectedChans := wp.outTypedChan[reflect.TypeOf(nil)]
for k, t := range wp.outTypedChan {
if k == reflect.TypeOf(out) {
selectedChans = t
break
}
}
if selectedChans == nil || len(selectedChans) == 0 {
return errors.New("couldn't locate out chan")
}
select {
case <-wp.Ctx.Done():
return nil
default:
utils.TeeValue(out, selectedChans...)
return nil
}
}
// waitForSignal : make sure we wait for a term signal and shutdown correctly
func (wp *workerPool) waitForSignal(signals ...os.Signal) {
go func() {
signal.Notify(wp.sigChan, signals...)
<-wp.sigChan
if wp.cancel != nil {
wp.cancel()
}
}()
}
// CancelOnSignal : set the signal to be used to cancel the pool
func (wp *workerPool) CancelOnSignal(signals ...os.Signal) WorkerPool {
if len(signals) > 0 {
wp.waitForSignal(signals...)
}
return wp
}
// closeChannels : waits for all the workers to finish up
func (wp *workerPool) closeChannels() (err error) {
wp.onceCloseOut.Do(func() {
close(wp.inChan)
close(wp.internalOutChan)
})
wp.wg.Wait()
return wp.err
}
// Close : a channel if the receiver is looking for a close.
// It indicates that no more data follows.
func (wp *workerPool) Close() error {
for len(wp.inChan) > 0 || len(wp.semaphore) > 0 {
// block
}
if err := wp.closeChannels(); err != nil && !errors.Is(err, context.Canceled) {
return err
}
return nil
}
// runOutChanMux : multiplex the out channel to the right destination
func (wp *workerPool) runOutChanMux() {
wp.wg.Add(1)
go func() {
defer wp.wg.Done()
for out := range wp.internalOutChan {
if err := wp.out(out); err != nil {
wp.err = err
wp.cancel()
}
}
}()
}