-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweep.go
41 lines (37 loc) · 1016 Bytes
/
sweep.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
// Copyright 2021 John Papandriopoulos. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package concurrent
// RunSweepErr will use at most maxThreads (or equal to the number of CPUs on
// the system if zero), to run func f concurrently, returning the first error
// received. If an error is reported, some func f may not be executed.
func RunSweepErr(count, maxThreads int, f func(index int) error) error {
if count == 0 {
return nil
}
if count == 1 {
// Run it on the current goroutine.
return f(0)
}
jr := NewRunner(maxThreads)
for i := 0; i < count; i++ {
if jr.Failures() > 0 {
break
}
index := i
jr.RunErr(func() error {
return f(index)
})
}
if jr.Finish() != count {
return jr.Errors()[0]
}
return nil
}
// RunSweep is like RunSweepErr, but without errors.
func RunSweep(count, maxThreads int, f func(index int)) {
RunSweepErr(count, maxThreads, func(i int) error {
f(i)
return nil
})
}