-
Notifications
You must be signed in to change notification settings - Fork 0
/
nerrgroup.go
133 lines (109 loc) · 2.27 KB
/
nerrgroup.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Package nerrgroup provides drop-in replacement for golang.org/x/sync/errgroup
// that's allow nested task scheduling
package nerrgroup
import (
"context"
"fmt"
"sync"
)
type task func() error
type Group struct {
sem chan struct{}
wg sync.WaitGroup
cancel func()
errOnce sync.Once
err error
cond *sync.Cond
todo []task
}
func New() *Group {
return &Group{
cond: sync.NewCond(new(sync.Mutex)),
}
}
func WithContext(ctx context.Context) (*Group, context.Context) {
ctx, cancel := context.WithCancel(ctx)
return &Group{
cancel: cancel,
cond: sync.NewCond(new(sync.Mutex)),
}, ctx
}
func (s *Group) Go(fn task) {
s.wg.Add(1)
s.cond.L.Lock()
s.todo = append(s.todo, fn)
s.cond.Signal()
s.cond.L.Unlock()
}
// Wait start worker pool for task executions and
// blocks until all function calls from the Go method have returned, then
// returns the first non-nil error (if any) from them.
func (s *Group) Wait() error {
exitFlag := false
go s.schedulingTasks(&exitFlag)
s.wg.Wait()
if s.cancel != nil {
s.cancel()
}
s.cond.L.Lock()
exitFlag = true
s.cond.Signal()
s.cond.L.Unlock()
return s.err
}
// SetLimit limits the number of active goroutines in this group to at most n.
// A negative value indicates no limit.
//
// Any subsequent call to the Go method will block until it can add an active
// goroutine without exceeding the configured limit.
//
// The limit must not be modified while any goroutines in the group are active.
func (s *Group) SetLimit(n int) {
if n < 0 {
s.sem = nil
return
}
if len(s.sem) != 0 {
panic(fmt.Errorf(
"nerrgroup: modify limit while %v goroutines in the group are still active",
len(s.sem),
))
}
s.sem = make(chan struct{}, n)
}
func (s *Group) done() {
if s.sem != nil {
<-s.sem
}
s.wg.Done()
s.cond.Signal()
}
func (s *Group) schedulingTasks(exitFlag *bool) {
for {
if s.sem != nil {
s.sem <- struct{}{}
}
s.cond.L.Lock()
for !*exitFlag && len(s.todo) == 0 {
s.cond.Wait()
}
if *exitFlag {
s.cond.L.Unlock()
return
}
go s.startTask(s.todo[0])
s.todo = s.todo[1:]
s.cond.L.Unlock()
}
}
func (s *Group) startTask(fn task) {
defer s.done()
if err := fn(); err != nil {
s.errOnce.Do(func() {
s.err = err
if s.cancel != nil {
s.cancel()
}
})
}
}