-
Notifications
You must be signed in to change notification settings - Fork 12
/
awg.go
299 lines (251 loc) · 6.32 KB
/
awg.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package awg
import (
"context"
"fmt"
"runtime"
"sync"
"time"
)
const (
// StatusIdle means that WG did not run yet
StatusIdle int = iota
// StatusSuccess means successful execution of all tasks
StatusSuccess
// StatusTimeout means that job was broken by timeout
StatusTimeout
// StatusError means that job was broken by error in one task (if stopOnError is true)
StatusError
errTimeoutMessage = "Wait group timeout after %v"
stackBufferSize = 1000
)
// ErrorTimeout error on timeout
type ErrorTimeout time.Duration
// Error implementation
func (e ErrorTimeout) Error() string {
return fmt.Sprintf(errTimeoutMessage, time.Duration(e).String())
}
// WaitgroupFunc func
type WaitgroupFunc func() error
// AdvancedWaitGroup enhanced wait group struct
type AdvancedWaitGroup struct {
waitGroupStatus
stackBuffer []WaitgroupFunc
receiver chan WaitgroupFunc
sender chan WaitgroupFunc
capacity uint32
length int
timeout *time.Duration
ctx context.Context
done func() <-chan struct{}
stopOnError bool
errors []error
}
type waitGroupStatus struct {
status int
statusLock sync.RWMutex
}
func done() <-chan struct{} {
return nil
}
// SetTimeout defines timeout for all tasks
func (wg *AdvancedWaitGroup) SetTimeout(t time.Duration) *AdvancedWaitGroup {
wg.timeout = &t
return wg
}
// SetStopOnError make wiatgroup stops if any task returns error
func (wg *AdvancedWaitGroup) SetStopOnError(b bool) *AdvancedWaitGroup {
wg.stopOnError = b
return wg
}
// Add adds new task in waitgroup
func (wg *AdvancedWaitGroup) Add(f ...WaitgroupFunc) *AdvancedWaitGroup {
wg.stackBuffer = append(wg.stackBuffer, f...)
return wg
}
// AddSlice adds new tasks in waitgroup
func (wg *AdvancedWaitGroup) AddSlice(s []WaitgroupFunc) *AdvancedWaitGroup {
return wg.Add(s...)
}
// WithContext make wiatgroup work with context timeout and Done
func (wg *AdvancedWaitGroup) WithContext(ctx context.Context) *AdvancedWaitGroup {
wg.ctx = ctx
wg.done = ctx.Done
return wg
}
// SetCapacity defines tasks channel capacity
func (wg *AdvancedWaitGroup) SetCapacity(c int) *AdvancedWaitGroup {
if c >= 0 {
wg.capacity = uint32(c)
}
return wg
}
// GetCapacity defines tasks channel capacity
func (wg *AdvancedWaitGroup) GetCapacity() int {
return int(wg.capacity)
}
func (wg *AdvancedWaitGroup) init() {
wg.setStatus(StatusSuccess)
if wg.done == nil {
wg.done = done
}
wg.length = len(wg.stackBuffer)
cap := wg.length
if c := wg.GetCapacity(); c > 0 {
cap = c
}
wg.receiver = make(chan WaitgroupFunc, cap)
wg.sender = make(chan WaitgroupFunc, wg.length)
for _, f := range wg.stackBuffer {
wg.sender <- f
}
}
// Start runs tasks in separate goroutines
func (wg *AdvancedWaitGroup) Start() *AdvancedWaitGroup {
if wg.CheckStatus(StatusSuccess) {
return wg
}
wg.init()
if wg.length > 0 {
failed := make(chan error, wg.length)
done := make(chan struct{}, wg.length)
wgDone := make(chan struct{})
var startTime time.Time
var timer <-chan time.Time
if wg.timeout != nil {
if *wg.timeout != 0 {
startTime = time.Now()
}
timer = time.After(*wg.timeout)
}
if wg.ctx != nil {
startTime = time.Now()
}
go func() {
for f := range wg.sender {
select {
case wg.receiver <- f:
// Nothing to do
case <-wgDone:
return
}
}
}()
ForLoop:
for wg.length > 0 {
select {
case f := <-wg.receiver:
go func(f WaitgroupFunc, failed chan<- error, done chan<- struct{}) {
if wg.stopOnError {
wg.doIfSuccess(f, failed, done)
return
}
wg.do(f, failed, done)
}(f, failed, done)
case err := <-failed:
wg.errors = append(wg.errors, err)
wg.length--
if wg.stopOnError {
wg.setStatus(StatusError)
break ForLoop
}
case <-done:
wg.length--
case <-wg.done():
if deadlineTime, ok := wg.ctx.Deadline(); ok {
wg.errors = append(wg.errors, ErrorTimeout(deadlineTime.Sub(startTime)))
wg.setStatus(StatusTimeout)
}
break ForLoop
case t := <-timer:
d := t.Sub(startTime)
wg.errors = append(wg.errors, ErrorTimeout(d))
wg.setStatus(StatusTimeout)
break ForLoop
}
}
close(wgDone)
close(wg.sender)
}
return wg
}
func (wg *AdvancedWaitGroup) do(f WaitgroupFunc, failed chan<- error, done chan<- struct{}) {
// Handle panic and pack it into stdlib error
defer func() {
if r := recover(); r != nil {
buf := make([]byte, stackBufferSize)
count := runtime.Stack(buf, false)
failed <- fmt.Errorf("Panic handeled\n%v\n%s", r, buf[:count])
}
}()
if err := f(); err != nil {
failed <- err
return
}
done <- struct{}{}
}
func (wg *AdvancedWaitGroup) doIfSuccess(f WaitgroupFunc, failed chan<- error, done chan<- struct{}) {
// Handle panic and pack it into stdlib error
defer func() {
if r := recover(); r != nil {
buf := make([]byte, stackBufferSize)
count := runtime.Stack(buf, false)
failed <- fmt.Errorf("Panic handeled\n%v\n%s", r, buf[:count])
}
}()
// Check stop on error
if !wg.CheckStatus(StatusSuccess) {
// If some other goroutine get an error
done <- struct{}{}
return
}
if err := f(); err != nil {
failed <- err
return
}
done <- struct{}{}
}
// Reset performs cleanup task queue and reset state
func (wg *AdvancedWaitGroup) Reset() {
wg.stackBuffer = []WaitgroupFunc{}
wg.receiver = nil
wg.sender = nil
wg.timeout = nil
wg.stopOnError = false
wg.setStatus(StatusIdle)
// pool
wg.errors = []error{}
}
// GetLastError returns last error that caught by execution process
func (wg *AdvancedWaitGroup) GetLastError() error {
if l := len(wg.errors); l > 0 {
return wg.errors[l-1]
}
return nil
}
// GetAllErrors returns all errors that caught by execution process
func (wg *AdvancedWaitGroup) GetAllErrors() []error {
return wg.errors
}
func (wg *AdvancedWaitGroup) setStatus(status int) {
if status < StatusIdle || status > StatusError {
return
}
wg.statusLock.Lock()
wg.status = status
wg.statusLock.Unlock()
}
// Status return result state string
func (wg *AdvancedWaitGroup) Status() int {
wg.statusLock.RLock()
defer wg.statusLock.RUnlock()
return wg.status
}
// CheckStatus return result of status compare
func (wg *AdvancedWaitGroup) CheckStatus(status int) bool {
if status < StatusIdle || status > StatusError {
return false
}
wg.statusLock.RLock()
defer wg.statusLock.RUnlock()
return wg.status == status
}