-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup.go
More file actions
95 lines (77 loc) · 2.2 KB
/
Copy pathgroup.go
File metadata and controls
95 lines (77 loc) · 2.2 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package resultgroup
import (
"context"
"sync"
)
// Group is a generic struct that holds errors and results from concurrent tasks.
// To create a Group without a context and error threshold, use the struct directly:
// group := resultgroup.Group[T]{}
type Group[T any] struct {
mutex sync.Mutex
errs []error
wg sync.WaitGroup
cancel func()
threshold int
results []T
}
// WithErrorsThreshold creates a new Group with the provided context
// and a threshold for the maximum number of errors.
// If the threshold is reached, the context will be canceled.
// Threshold must be greater than or equal to 1.
func WithErrorsThreshold[T any](ctx context.Context, threshold int) (Group[T], context.Context) {
if threshold < 1 {
panic("threshold must be greater than or equal to 1")
}
ctx, cancel := context.WithCancel(ctx)
return Group[T]{cancel: cancel, threshold: threshold}, ctx
}
// Go runs the provided function in a new goroutine and append the results
// to aggregated slice that will be returned by Wait.
// If the function returns an error, it will be appended to the aggregated
// slice of errors if the threshold is not reached.
func (g *Group[T]) Go(f func() ([]T, error)) {
g.wg.Add(1)
go func() {
defer g.wg.Done()
res, err := f()
g.processResult(res, err)
}()
}
func (g *Group[T]) processResult(res []T, err error) {
if err != nil {
g.handleErrors(err)
}
g.appendResults(res)
}
func (g *Group[T]) handleErrors(err error) {
g.mutex.Lock()
defer g.mutex.Unlock()
if g.threshold == 0 || len(g.errs) < g.threshold {
g.errs = append(g.errs, err)
}
if len(g.errs) == g.threshold {
if g.cancel != nil {
g.cancel()
}
}
}
func (g *Group[T]) appendResults(res []T) {
g.mutex.Lock()
defer g.mutex.Unlock()
g.results = append(g.results, res...)
}
// Wait blocks until all function calls from the Go method have returned, then
// returns the concatenated results and a multiError containing all errors that
// are below the threshold.
func (g *Group[T]) Wait() ([]T, errorWithUnwrap) {
g.wg.Wait()
g.mutex.Lock()
defer g.mutex.Unlock()
if g.cancel != nil {
g.cancel()
}
if len(g.errs) == 0 {
return g.results, nil
}
return g.results, &multiError{errs: g.errs}
}