-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
44 lines (41 loc) · 1.12 KB
/
process.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
package efficiency
import (
"context"
"runtime"
"sync"
"github.com/andygeiss/cloud-native-utils/service"
)
// Process concurrently processes items from the input channel using the provided function `fn`.
// It spawns a number of worker goroutines equal to the number of available CPU cores.
func Process[IN, OUT any](in <-chan IN, fn service.Function[IN, OUT]) (<-chan OUT, <-chan error) {
out := make(chan OUT)
errCh := make(chan error)
ctx := context.Background()
// Launch `num` worker goroutines.
num := runtime.NumCPU()
var wg sync.WaitGroup
wg.Add(num)
for range num {
go func() {
defer wg.Done()
// Process items from the input channel.
for val := range in {
// Call the processing function `fn` with the current value.
res, err := fn(ctx, val)
// If an error occurs, send it to the error channel and stop processing.
if err != nil {
errCh <- err
return
}
// Send the processed result to the output channel.
out <- res
}
}()
}
// Start a goroutine to close the output channel after all workers finish.
go func() {
wg.Wait()
close(out)
}()
return out, errCh
}