-
Notifications
You must be signed in to change notification settings - Fork 1
/
wait_group.go
179 lines (158 loc) · 3.8 KB
/
wait_group.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package syncx
import (
"context"
"fmt"
"runtime"
"sync"
"time"
)
// Status represents AdvancedWaitGroup status
type Status int
const (
// StatusIdle means that WG did not run yet
StatusIdle Status = iota
// StatusSuccess means successful execution of all tasks
StatusSuccess
// StatusTimeout means that job was broken by timeout
StatusTimeout
// StatusCanceled means that WG was canceled
StatusCanceled
// StatusError means that job was broken by error in one task (if stopOnError is true)
StatusError
)
// WaitgroupFunc func
type WaitgroupFunc func() error
// AdvancedWaitGroup enhanced wait group struct. You can use it in different goroutines. It's thread safe.
type AdvancedWaitGroup struct {
sync.RWMutex
context context.Context
tasks []WaitgroupFunc
stopOnError bool
status Status
errors []error
}
// SetTimeout defines timeout for all tasks
func (wg *AdvancedWaitGroup) SetTimeout(t time.Duration) *AdvancedWaitGroup {
wg.Lock()
ctx := context.Background()
if wg.context != nil {
ctx = wg.context
}
wg.context, _ = context.WithTimeout(ctx, t)
wg.Unlock()
return wg
}
// SetContext defines Context
func (wg *AdvancedWaitGroup) SetContext(t context.Context) *AdvancedWaitGroup {
wg.Lock()
wg.context = t
wg.Unlock()
return wg
}
// SetStopOnError stops waitgroup if any task returns error
func (wg *AdvancedWaitGroup) SetStopOnError(b bool) *AdvancedWaitGroup {
wg.Lock()
wg.stopOnError = b
wg.Unlock()
return wg
}
// Add adds new tasks into waitgroup
func (wg *AdvancedWaitGroup) Add(funcs ...WaitgroupFunc) *AdvancedWaitGroup {
wg.Lock()
wg.tasks = append(wg.tasks, funcs...)
wg.Unlock()
return wg
}
// Start runs tasks in separate goroutines and waits for their completion
func (wg *AdvancedWaitGroup) Start() *AdvancedWaitGroup {
wg.Lock()
defer wg.Unlock()
wg.status = StatusSuccess
if taskCount := len(wg.tasks); taskCount > 0 {
failed := make(chan error, taskCount)
done := make(chan bool, taskCount)
StarterLoop:
for _, f := range wg.tasks {
// check if context is canceled
select {
case <-wg.doneChannel():
break StarterLoop
default:
}
go func(f WaitgroupFunc, failed chan<- error, done chan<- bool) {
// Handle panic and pack it into stdlib error
defer func() {
if r := recover(); r != nil {
buf := make([]byte, 1000)
runtime.Stack(buf, false)
failed <- fmt.Errorf("Panic handeled\n%v\n%s", r, string(buf))
}
}()
if err := f(); err != nil {
failed <- err
} else {
done <- true
}
}(f, failed, done)
}
ForLoop:
for taskCount > 0 {
select {
case err := <-failed:
wg.errors = append(wg.errors, err)
taskCount--
if wg.stopOnError {
wg.status = StatusError
break ForLoop
}
case <-done:
taskCount--
case <-wg.doneChannel():
if _, ok := wg.context.Deadline(); ok {
wg.status = StatusTimeout
} else {
wg.status = StatusCanceled
}
break ForLoop
}
}
}
return wg
}
func (wg *AdvancedWaitGroup) doneChannel() <-chan struct{} {
if wg.context != nil {
return wg.context.Done()
}
return nil
}
// Reset performs cleanup task queue and reset state
func (wg *AdvancedWaitGroup) Reset() {
wg.Lock()
wg.tasks = nil
wg.stopOnError = false
wg.status = StatusIdle
wg.errors = nil
wg.context = nil
wg.Unlock()
}
// LastError returns last error that caught by execution process
func (wg *AdvancedWaitGroup) LastError() error {
wg.RLock()
defer wg.RUnlock()
if l := len(wg.errors); l > 0 {
return wg.errors[l-1]
}
return nil
}
// AllErrors returns all errors that caught by execution process
func (wg *AdvancedWaitGroup) AllErrors() []error {
wg.RLock()
defer wg.RUnlock()
return wg.errors
}
// Status returns result state
func (wg *AdvancedWaitGroup) Status() Status {
wg.RLock()
defer wg.RUnlock()
return wg.status
}