forked from dreamsofcode-io/loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
67 lines (57 loc) · 1.25 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
package loop
import (
"context"
"sync"
)
// Pool is used to perform bounded concurrency when iterating over the
// elements in a slice.
//
// The workers parameter specifies the size of the concurrency pool
// for iteration. For example, if a factor of 2 is given, then there
// will only even be 2 iterations running at once.
// 1 would effectively be a serial iteration.
//
// Bounded concurrency is useful in cases where the user may wish
// to perform concurrency but in a reduced rate, so as to avoid
// rate limits or running out of file descriptors.
func Pool[E any](xs []E, workers int) func(func(int, E) bool) {
return func(yield func(int, E) bool) {
type iteration struct {
val E
i int
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch := make(chan iteration, workers)
var wg sync.WaitGroup
wg.Add(workers)
for range workers {
go func() {
defer wg.Done()
for x := range ch {
select {
case <-ctx.Done():
return
default:
if !yield(x.i, x.val) {
cancel()
}
}
}
}()
}
for i, x := range xs {
select {
case <-ctx.Done():
return
default:
ch <- iteration{
val: x,
i: i,
}
}
}
close(ch)
wg.Wait()
}
}