-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconcurrent.go
More file actions
46 lines (42 loc) · 945 Bytes
/
Copy pathconcurrent.go
File metadata and controls
46 lines (42 loc) · 945 Bytes
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
package main
import (
"runtime"
"sync"
)
// mapConcurrent runs fn over every job across a bounded worker pool (one
// worker per CPU, capped at len(jobs)) and returns the results in completion
// order. It is the shared fan-out for the per-file decrypt and encrypt
// pipelines; callers build the job slice and tally the results themselves.
func mapConcurrent[J, R any](jobs []J, fn func(J) R) []R {
if len(jobs) == 0 {
return nil
}
workers := min(runtime.NumCPU(), len(jobs))
jobCh := make(chan J)
resultCh := make(chan R, len(jobs))
var wg sync.WaitGroup
wg.Add(workers)
for range workers {
go func() {
defer wg.Done()
for j := range jobCh {
resultCh <- fn(j)
}
}()
}
go func() {
for _, j := range jobs {
jobCh <- j
}
close(jobCh)
}()
go func() {
wg.Wait()
close(resultCh)
}()
results := make([]R, 0, len(jobs))
for r := range resultCh {
results = append(results, r)
}
return results
}