This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoroutines.go
223 lines (199 loc) · 4.1 KB
/
goroutines.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
// Package goroutines provides utilities to perform common tasks on goroutines
package goroutines
import (
"context"
"sync"
"time"
)
// New creates an instange of Go struct
func New() Go { return Go{} }
// Go provides a fluent way to prepare & start a goroutine
type Go struct {
ensureStarted bool
timeout time.Duration
recoverFunc func(interface{})
before func()
after func()
deferAfter bool
wg *sync.WaitGroup
}
// Go is final call in the fluent chain
func (x Go) Go(f func()) error {
var started, funcDone chan struct{}
if x.ensureStarted {
started = make(chan struct{})
}
if x.timeout != 0 {
funcDone = make(chan struct{})
}
if x.wg != nil {
x.wg.Add(1)
}
go func() {
if started != nil {
close(started)
}
if x.wg != nil {
defer x.wg.Done()
}
if funcDone != nil {
defer close(funcDone)
}
if x.recoverFunc != nil {
defer func() {
if e := recover(); e != nil {
x.recoverFunc(e)
}
}()
}
if x.before != nil {
x.before()
}
if x.after != nil && x.deferAfter {
defer x.after()
}
f()
if x.after != nil && !x.deferAfter {
x.after()
}
}()
if started != nil {
<-started
}
if funcDone != nil {
if x.timeout > 0 {
tm := time.NewTimer(x.timeout)
defer func() {
if !tm.Stop() {
select {
case <-tm.C:
default:
}
}
}()
select {
case <-funcDone:
case <-tm.C:
return ErrTimeout
}
} else if x.timeout < 0 {
<-funcDone
}
}
return nil
}
// WithContext is same as Go(...) but also accepts a context and if a timeout
// larger than zero is set, creates a child context and cencels it on timeout
func (x Go) WithContext(ctx context.Context, f func(context.Context)) error {
_ctx := ctx
var _cancel context.CancelFunc
var started, funcDone chan struct{}
if x.ensureStarted {
started = make(chan struct{})
}
if x.timeout != 0 {
if x.timeout > 0 {
_ctx, _cancel = context.WithCancel(ctx)
defer _cancel()
}
funcDone = make(chan struct{})
}
if x.wg != nil {
x.wg.Add(1)
}
go func() {
if started != nil {
close(started)
}
if x.wg != nil {
defer x.wg.Done()
}
if funcDone != nil {
defer close(funcDone)
}
if x.recoverFunc != nil {
defer func() {
if e := recover(); e != nil {
x.recoverFunc(e)
}
}()
}
if x.before != nil {
x.before()
}
if x.after != nil && x.deferAfter {
defer x.after()
}
f(_ctx)
if x.after != nil && !x.deferAfter {
x.after()
}
}()
if started != nil {
<-started
}
if funcDone != nil {
if x.timeout > 0 {
tm := time.NewTimer(x.timeout)
defer func() {
if !tm.Stop() {
select {
case <-tm.C:
default:
}
}
}()
select {
case <-funcDone:
case <-tm.C:
return ErrTimeout
}
} else if x.timeout < 0 {
<-funcDone
}
}
return nil
}
// EnsureStarted instructs Go to start a goroutine and wait for it to start,
// and after goroutine started, it returns.
func (x Go) EnsureStarted() Go {
x.ensureStarted = true
return x
}
// Timeout sets a timeout and waits for f to complete (in a goroutine)
// or times out (returning ErrTimeout). A negative value for timeout, means
// waiting infinitely.
func (x Go) Timeout(timeout time.Duration) Go {
x.timeout = timeout
return x
}
// AddToGroup registers the goroutine in a sync.WaitGroup by adding necessary code (Add/Done)
func (x Go) AddToGroup(wg *sync.WaitGroup) Go {
x.wg = wg
return x
}
// Recover recovers from panic and returns error,
// or returns the provided error
func (x Go) Recover(recoverFunc func(interface{})) Go {
x.recoverFunc = recoverFunc
return x
}
// Before will be called before the goroutine func at the begining of the same goroutine
func (x Go) Before(before func()) Go {
x.before = before
return x
}
// After will get called after the goroutine func, it can be deferred
func (x Go) After(after func(), deferred ...bool) Go {
x.after = after
if len(deferred) > 0 {
x.deferAfter = deferred[0]
}
return x
}
var (
// ErrTimeout is a timeout error
ErrTimeout error = _error(`TIMEOUT`)
)
type _error string
func (v _error) Error() string { return string(v) }