-
Notifications
You must be signed in to change notification settings - Fork 27
/
wait.go
51 lines (38 loc) · 1002 Bytes
/
wait.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
package wait
import "fmt"
// Until iterates over buffered error channel and:
// * upon receiving non-nil value from the channel, makes an early return with this value
// * if no non-nil values were received from iteration over the channel, it just returns nil
// Used to wait for a series of goroutines, launched altogether from the same loop, to finish.
func Until(done chan error) error {
i := 0
for err := range done {
if err != nil {
return err
}
i++
if i >= cap(done) {
close(done)
}
}
return nil
}
// WithTolerance is the same as Until, but it does not return immediately on errors
// rather loops through all channel capacity returning "composite" error in the end.
func WithTolerance(done chan error) error {
var errMessage string
i := 0
for err := range done {
if err != nil {
errMessage = errMessage + err.Error() + "\n"
}
i++
if i >= cap(done) {
close(done)
}
}
if len(errMessage) == 0 {
return nil
}
return fmt.Errorf(errMessage)
}