/
errsizedgroup.go
69 lines (55 loc) · 1.39 KB
/
errsizedgroup.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package syncs
import (
"context"
"sync"
"golang.org/x/sync/semaphore"
)
// ErrSizedGroup has the same role as WaitingGroup but adds a limit of the amount of goroutines started concurrently.
// Uses similar Go() scheduling as errgroup.Group, thread safe.
type ErrSizedGroup struct {
options
ctx context.Context //nolint: containedctx
err *multierror
sem *semaphore.Weighted
wg sync.WaitGroup
}
// NewErrSizedGroup makes wait group with limited size of alive goroutines.
func NewErrSizedGroup(ctx context.Context, size int64, opts ...GroupOption) *ErrSizedGroup {
options := options{}
for _, opt := range opts {
opt(&options)
}
return &ErrSizedGroup{
options: options,
ctx: ctx,
err: &multierror{},
sem: semaphore.NewWeighted(size),
}
}
// Go calls the given function in a new goroutine.
// Every call will be unblocked, but some goroutines may wait.
func (g *ErrSizedGroup) Go(fn func(ctx context.Context) error) {
g.wg.Add(1)
if g.preLock {
if err := g.sem.Acquire(g.ctx, 1); err != nil {
return
}
}
go func() {
defer g.wg.Done()
if !g.preLock {
if err := g.sem.Acquire(g.ctx, 1); err != nil {
return
}
}
defer g.sem.Release(1)
if err := fn(g.ctx); err != nil {
g.err.Add(err)
}
}()
}
// Wait blocks until all function calls from the Go method have returned.
func (g *ErrSizedGroup) Wait() error {
g.wg.Wait()
return g.err.errorOrNil()
}