/
sizedgroup.go
63 lines (50 loc) · 1.24 KB
/
sizedgroup.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
package syncs
import (
"context"
"sync"
"golang.org/x/sync/semaphore"
)
// SizedGroup 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 SizedGroup struct {
options
ctx context.Context //nolint:containedctx
sem *semaphore.Weighted
wg sync.WaitGroup
}
// NewSizedGroup makes wait group with limited size of alive goroutines.
func NewSizedGroup(ctx context.Context, size int64, opts ...GroupOption) *SizedGroup {
options := options{}
for _, opt := range opts {
opt(&options)
}
return &SizedGroup{
options: options,
ctx: ctx,
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 *SizedGroup) Go(fn func(ctx context.Context)) {
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)
fn(g.ctx)
}()
}
// Wait blocks until all function calls from the Go method have returned.
func (g *SizedGroup) Wait() {
g.wg.Wait()
}