@@ -2,81 +2,24 @@ package uploadstore
22
33import (
44 "runtime"
5- "sync"
65
7- "github.com/sourcegraph/sourcegraph/lib/errors "
6+ "github.com/sourcegraph/conc/pool "
87)
98
10- // poolWorker is a function invoked by RunWorkers that sends
11- // any errors that occur during execution down a shared channel.
12- type poolWorker func (errs chan <- error )
13-
14- // runWorkersN invokes the given worker n times and collects the
15- // errors from each invocation.
16- func runWorkersN (n int , worker poolWorker ) (err error ) {
17- errs := make (chan error , n )
18-
19- var wg sync.WaitGroup
20- for i := 0 ; i < n ; i ++ {
21- wg .Add (1 )
22- go func () { worker (errs ); wg .Done () }()
23- }
24-
25- go func () {
26- wg .Wait ()
27- close (errs )
28- }()
29-
30- for e := range errs {
31- if err == nil {
32- err = e
33- } else {
34- err = errors .Append (err , e )
35- }
36- }
37-
38- return err
39- }
40-
41- // RunWorkersOverStrings invokes the given worker once for each of the
42- // given string values. The worker function will receive the index as well
43- // as the string value as parameters. Workers will be invoked in a number
9+ // ForEachString invokes the given callback once for each of the
10+ // given string values. The callback function will receive the index as well
11+ // as the string value as parameters. Callbacks will be invoked in a number
4412// of concurrent routines proportional to the maximum number of CPUs that
4513// can be executing simultaneously.
46- func RunWorkersOverStrings (values []string , worker func (index int , value string ) error ) error {
47- return runWorkersOverStringsN (runtime .GOMAXPROCS (0 ), values , worker )
48- }
49-
50- // RunWorkersOverStrings invokes the given worker once for each of the
51- // given string values. The worker function will receive the index as well
52- // as the string value as parameters. Workers will be invoked in n concurrent
53- // routines.
54- func runWorkersOverStringsN (n int , values []string , worker func (index int , value string ) error ) error {
55- return runWorkersN (n , indexedStringWorker (loadIndexedStringChannel (values ), worker ))
56- }
57-
58- type indexedString struct {
59- index int
60- value string
61- }
62-
63- func loadIndexedStringChannel (values []string ) <- chan indexedString {
64- ch := make (chan indexedString , len (values ))
65- defer close (ch )
66-
14+ func ForEachString (values []string , f func (index int , value string ) error ) error {
15+ p := pool .New ().
16+ WithErrors ().
17+ WithMaxGoroutines (runtime .GOMAXPROCS (0 ))
6718 for i , value := range values {
68- ch <- indexedString {index : i , value : value }
69- }
70-
71- return ch
72- }
73-
74- func indexedStringWorker (ch <- chan indexedString , worker func (index int , value string ) error ) poolWorker {
75- return func (errs chan <- error ) {
76- for value := range ch {
77- if err := worker (value .index , value .value ); err != nil {
78- errs <- err
79- }
80- }
19+ i , value := i , value
20+ p .Go (func () error {
21+ return f (i , value )
22+ })
8123 }
24+ return p .Wait ()
8225}
0 commit comments