forked from fanliao/go-promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
future_factory.go
278 lines (250 loc) · 6.66 KB
/
future_factory.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
package promise
import (
"sync/atomic"
)
type anyPromiseResult struct {
result interface{}
i int
}
//Start start a goroutines to execute task function
//and return a Future that presents the result.
//If option paramter is true, the act function will be sync called.
//Type of act can be any of below four types:
// func() (r interface{}, err error):
// if err returned by act != nil or panic error, then Future will be rejected with error,
// otherwise be resolved with r.
// func():
// if act panic error, then Future will be rejected, otherwise be resolved with nil.
// func(c promise.Canceller) (r interface{}, err error):
// if err returned by act != nil or panic error,
// then Future will be rejected with err, otherwise be resolved with r.
// We can check c.IsCancelled() to decide whether need to exit act function
// func(promise.Canceller):
// if act panic error, then Future will be rejected with error, otherwise be resolved with nil.
// We can check c.IsCancelled() to decide whether need to exit act function.
// error:
// Future will be rejected with error immediately
// other value:
// Future will be resolved with value immediately
func Start(act interface{}, syncs ...bool) *Future {
pr := NewPromise()
if f, ok := act.(*Future); ok {
return f
}
if action := getAct(pr, act); action != nil {
if syncs != nil && len(syncs) > 0 && !syncs[0] {
//sync call
r, err := action()
if pr.IsCancelled() {
pr.Cancel()
} else {
if err == nil {
pr.Resolve(r)
} else {
pr.Reject(err)
}
}
} else {
//async call
go func() {
r, err := action()
if pr.IsCancelled() {
pr.Cancel()
} else {
if err == nil {
pr.Resolve(r)
} else {
pr.Reject(err)
}
}
}()
}
}
return pr.Future
}
//Wrap return a Future that presents the wrapped value
func Wrap(value interface{}) *Future {
pr := NewPromise()
if e, ok := value.(error); !ok {
pr.Resolve(value)
} else {
pr.Reject(e)
}
return pr.Future
}
//WhenAny returns a Future.
//If any Future is resolved, this Future will be resolved and return result of resolved Future.
//Otherwise will rejected with results slice returned by all Futures
//Legit types of act are same with Start function
func WhenAny(acts ...interface{}) *Future {
return WhenAnyMatched(nil, acts...)
}
//WhenAnyMatched returns a Future.
//If any Future is resolved and match the predicate, this Future will be resolved and return result of resolved Future.
//If all Futures are cancelled, this Future will be cancelled.
//Otherwise will rejected with a NoMatchedError included results slice returned by all Futures
//Legit types of act are same with Start function
func WhenAnyMatched(predicate func(interface{}) bool, acts ...interface{}) *Future {
if predicate == nil {
predicate = func(v interface{}) bool { return true }
}
fs := make([]*Future, len(acts))
for i, act := range acts {
fs[i] = Start(act)
}
nf, rs := NewPromise(), make([]interface{}, len(fs))
if len(acts) == 0 {
nf.Resolve(nil)
}
chFails, chDones := make(chan anyPromiseResult), make(chan anyPromiseResult)
go func() {
for i, f := range fs {
k := i
f.OnSuccess(func(v interface{}) {
defer func() { _ = recover() }()
chDones <- anyPromiseResult{v, k}
}).OnFailure(func(v interface{}) {
defer func() { _ = recover() }()
chFails <- anyPromiseResult{v, k}
}).OnCancel(func() {
defer func() { _ = recover() }()
chFails <- anyPromiseResult{CANCELLED, k}
})
}
}()
if len(fs) == 1 {
select {
case r := <-chFails:
if _, ok := r.result.(CancelledError); ok {
nf.Cancel()
} else {
nf.Reject(newNoMatchedError1(r.result))
}
case r := <-chDones:
if predicate(r.result) {
nf.Resolve(r.result)
} else {
nf.Reject(newNoMatchedError1(r.result))
}
}
} else {
go func() {
defer func() {
if e := recover(); e != nil {
nf.Reject(newErrorWithStacks(e))
}
}()
j := 0
for {
select {
case r := <-chFails:
rs[r.i] = getError(r.result)
case r := <-chDones:
if predicate(r.result) {
//try to cancel other futures
for _, f := range fs {
f.Cancel()
}
//close the channel for avoid the send side be blocked
closeChan := func(c chan anyPromiseResult) {
defer func() { _ = recover() }()
close(c)
}
closeChan(chDones)
closeChan(chFails)
//Resolve the future and return result
nf.Resolve(r.result)
return
} else {
rs[r.i] = r.result
}
}
if j++; j == len(fs) {
m := 0
for _, r := range rs {
switch val := r.(type) {
case CancelledError:
default:
m++
_ = val
}
}
if m > 0 {
nf.Reject(newNoMatchedError(rs))
} else {
nf.Cancel()
}
break
}
}
}()
}
return nf.Future
}
//WhenAll receives function slice and returns a Future.
//If all Futures are resolved, this Future will be resolved and return results slice.
//Otherwise will rejected with results slice returned by all Futures
//Legit types of act are same with Start function
func WhenAll(acts ...interface{}) (fu *Future) {
pr := NewPromise()
fu = pr.Future
if len(acts) == 0 {
pr.Resolve([]interface{}{})
return
}
fs := make([]*Future, len(acts))
for i, act := range acts {
fs[i] = Start(act)
}
fu = whenAllFuture(fs...)
return
}
//WhenAll receives Futures slice and returns a Future.
//If all Futures are resolved, this Future will be resolved and return results slice.
//If any Future is cancelled, this Future will be cancelled.
//Otherwise will rejected with results slice returned by all Futures.
//Legit types of act are same with Start function
func whenAllFuture(fs ...*Future) *Future {
wf := NewPromise()
rs := make([]interface{}, len(fs))
if len(fs) == 0 {
wf.Resolve([]interface{}{})
} else {
n := int32(len(fs))
cancelOthers := func(j int) {
for k, f1 := range fs {
if k != j {
f1.Cancel()
}
}
}
go func() {
isCancelled := int32(0)
for i, f := range fs {
j := i
f.OnSuccess(func(v interface{}) {
rs[j] = v
if atomic.AddInt32(&n, -1) == 0 {
wf.Resolve(rs)
}
}).OnFailure(func(v interface{}) {
if atomic.CompareAndSwapInt32(&isCancelled, 0, 1) {
//try to cancel all futures
cancelOthers(j)
//errs := make([]error, 0, 1)
//errs = append(errs, v.(error))
e := newAggregateError1("Error appears in WhenAll:", v)
wf.Reject(e)
}
}).OnCancel(func() {
if atomic.CompareAndSwapInt32(&isCancelled, 0, 1) {
//try to cancel all futures
cancelOthers(j)
wf.Cancel()
}
})
}
}()
}
return wf.Future
}