This repository has been archived by the owner on Mar 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
taskgroup.go
158 lines (133 loc) · 2.87 KB
/
taskgroup.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
package taskchain
import (
"runtime"
"sync"
)
// TaskGroup is a grouping of Tasks that will be dispatched
// asynchronously
type TaskGroup struct {
sync.RWMutex
// Next is the task group that is to be executed after all
// tasks within the current task group are completed
Next *TaskGroup
// ErrorHandler will handle all errors returned within this
// task group
ErrorHandler func(*TaskGroup, error)
// PanicHandler will be called in case of a panic
PanicHandler func(err interface{}, stacktrace []byte) error
tasks []Task
bag *bag
}
// Add a Task to this group
func (t *TaskGroup) Add(task Task) {
t.Lock()
t.tasks = append(t.tasks, task)
t.Unlock()
}
// Exec will run the task group
func (t *TaskGroup) Exec() error {
numTasks := len(t.tasks)
errchan := make(chan error)
donechan := make(chan struct{}, numTasks)
defer func() {
close(errchan)
close(donechan)
}()
for _, td := range t.tasks {
go func(task Task, grErrChan chan error, grDoneChan chan struct{}) {
err := t.runTask(task)
if err != nil {
grErrChan <- err
}
grDoneChan <- struct{}{}
}(td, errchan, donechan)
}
var errorOut error
var firstErrorToken sync.Once
pops := 0
for {
if pops >= numTasks {
break
}
select {
case <-donechan:
pops++
case err := <-errchan:
firstErrorToken.Do(func() {
errorOut = err
})
if t.ErrorHandler != nil {
go t.ErrorHandler(t, err)
}
}
}
if errorOut == nil && t.Next != nil {
if t.Next.ErrorHandler == nil {
t.Next.ErrorHandler = t.ErrorHandler
}
if t.Next.PanicHandler == nil {
t.Next.PanicHandler = t.PanicHandler
}
t.ensureBag()
errorOut = t.Next.execWithBag(t.bag)
}
return errorOut
}
func (t *TaskGroup) runTask(task Task) (result error) {
if t.PanicHandler != nil {
defer func() {
if p := recover(); p != nil {
buf := make([]byte, 1<<10)
stackLen := runtime.Stack(buf, true)
result = t.PanicHandler(p, buf[:stackLen])
}
}()
}
result = task(t)
return
}
// Get the item identified by key, or a default value if the item doesn't exist
func (t *TaskGroup) Get(key string, dflt interface{}) interface{} {
t.ensureBag()
val, ok := t.bag.get(key)
if !ok || val == nil {
return dflt
}
return val
}
// Set the item by key
func (t *TaskGroup) Set(key string, value interface{}) {
t.ensureBag()
// don't allow nils in the map
if value == nil {
t.bag.remove(key)
} else {
t.bag.set(key, value)
}
}
// Unset an item by key
func (t *TaskGroup) Unset(key string) {
t.Set(key, nil)
}
func (t *TaskGroup) ensureBag() {
if t.isBagNil() {
t.makeBag()
}
}
func (t *TaskGroup) isBagNil() bool {
t.RLock()
defer t.RUnlock()
return t.bag == nil
}
func (t *TaskGroup) makeBag() {
t.Lock()
defer t.Unlock()
if t.bag == nil {
t.bag = &bag{data: make(map[string]interface{})}
}
}
func (t *TaskGroup) execWithBag(b *bag) error {
t.ensureBag()
t.bag.absorb(b)
return t.Exec()
}