-
Notifications
You must be signed in to change notification settings - Fork 2
/
pool.go
205 lines (177 loc) · 4.92 KB
/
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
package simpleflow
import (
"context"
"sync"
)
// Job is a function that the slice or channel worker pool executes
type Job[T any] func(ctx context.Context, item T) error
// JobKV is a function that the map worker pool executes
type JobKV[K comparable, V any] func(context.Context, K, V) error
// KeyValue is a tuple of key, value
type KeyValue[K comparable, V any] struct {
Key K
Value V
}
// WorkerPoolFromMap starts a worker pool of size `nWorkers` and calls the function `f` for each
// element in the `items` map
func WorkerPoolFromMap[K comparable, V any](ctx context.Context, items map[K]V, nWorkers int, f JobKV[K, V]) []error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var errChan = make(chan error)
var wg sync.WaitGroup
wg.Add(nWorkers + 1)
// Spawn a fixed pool of workers
ch := make(chan KeyValue[K, V])
for ii := 0; ii < nWorkers; ii++ {
go func() {
defer wg.Done()
for {
select {
case v, ok := <-ch:
if !ok {
// If the channel is closed, exit
return
}
err := f(ctx, v.Key, v.Value)
if err != nil {
errChan <- err
}
case <-ctx.Done():
return
}
}
}()
}
// Start a separate go routine that is pulling errors from the workers and appending them to a slice
// This is required so that the error channel does not block. The alternative is to create a buffered
// error channel with size len(items)
var errors []error
errWaitGroup := sync.WaitGroup{}
errWaitGroup.Add(1)
go func() {
defer errWaitGroup.Done()
errors = ChannelToSlice(errChan)
}()
// Push items to the workers in a separate go routine in case the channel gets blocked by a canceled worker
go func() {
defer wg.Done()
for k, v := range items {
select {
case ch <- KeyValue[K, V]{Key: k, Value: v}:
case <-ctx.Done():
return
}
}
close(ch)
}()
// Wait for the workers to finish processing their current items
wg.Wait()
// Close the error channel and wait for the errors to drain into the slice
close(errChan)
errWaitGroup.Wait()
return errors
}
// WorkerPoolFromChan starts a worker pool of size `nWorkers` and calls the function `f` for each
// element in the `items` channel
func WorkerPoolFromChan[T any](ctx context.Context, items <-chan T, nWorkers int, f Job[T]) []error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var errChan = make(chan error)
var wg sync.WaitGroup
wg.Add(nWorkers)
// Spawn a fixed pool of workers
for ii := 0; ii < nWorkers; ii++ {
go func(ii int) {
defer wg.Done()
for {
select {
case v, ok := <-items:
if !ok {
return
}
err := f(ctx, v)
if err != nil {
errChan <- err
}
case <-ctx.Done():
return
}
}
}(ii)
}
// Start a separate go routine that is pulling errors from the workers and appending them to a slice
// This is required so that the error channel does not block. The alternative is to create a buffered
// error channel with size len(items)
var errors []error
errWaitGroup := sync.WaitGroup{}
errWaitGroup.Add(1)
go func() {
defer errWaitGroup.Done()
errors = ChannelToSlice(errChan)
}()
// Wait for the workers to finish processing their current items
wg.Wait()
// Close the error channel and wait for the errors to drain into the slice
close(errChan)
errWaitGroup.Wait()
return errors
}
// WorkerPoolFromSlice starts a worker pool of size `nWorkers` and calls the function `f` for each
// element in the `items` slice. It returns an array of errors from the jobs.
func WorkerPoolFromSlice[T any](ctx context.Context, items []T, nWorkers int, f Job[T]) []error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var errChan = make(chan error)
var wg sync.WaitGroup
wg.Add(nWorkers + 1)
// Spawn a fixed pool of workers
ch := make(chan T)
for ii := 0; ii < nWorkers; ii++ {
go func() {
defer wg.Done()
for {
select {
case v, ok := <-ch:
if !ok {
// If the channel is closed, exit
return
}
err := f(ctx, v)
if err != nil {
errChan <- err
}
case <-ctx.Done():
return
}
}
}()
}
// Start a separate go routine that is pulling errors from the workers and appending them to a slice
// This is required so that the error channel does not block. The alternative is to create a buffered
// error channel with size len(items)
var errors []error
errWaitGroup := sync.WaitGroup{}
errWaitGroup.Add(1)
go func() {
defer errWaitGroup.Done()
errors = ChannelToSlice(errChan)
}()
// Push items to the workers in a separate go routine in case the channel gets blocked by a canceled worker
go func() {
defer wg.Done()
for ii := 0; ii < len(items); ii++ {
select {
case ch <- items[ii]:
case <-ctx.Done():
return
}
}
close(ch)
}()
// Wait for the workers to finish processing their current items
wg.Wait()
// Close the error channel and wait for the errors to drain into the slie
close(errChan)
errWaitGroup.Wait()
return errors
}