-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.go
466 lines (404 loc) · 13.4 KB
/
promise.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Copyright 2020 Ahmad Sameh(asmsh)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package promise
import (
"context"
"fmt"
"sync/atomic"
"time"
)
// Promise represents some asynchronous work (a goroutine).
//
// The zero value will block forever on any calls.
// It implements the [Result] interface
type Promise[T any] struct {
// group is a pointer to the promise group which this promise is part of,
// or nil, if it's part of the default group.
group *Group[T]
// initiated lazily, by the first extension call.
// this is sent on by the different extension calls.
// it's never closed.
extChanP atomic.Pointer[chan extQueue[T]]
// closed when this promise is resolved.
// its channel has one writer (one goroutine), which is the owner,
// which will close it, but can have multiple readers (chain promises).
syncCtx context.Context
// holds the result of the promise.
// written once, before the syncCtx channel is closed.
//
// don't read it unless the syncCtx is known to be closed.
res Result[T]
// resolveState holds the state of this promise.
// once the promise is resolved, it will be non-zero.
// written once, before the syncCtx channel is closed.
resolveState atomic.Uint32
// chainStatus holds the status of this promise's chain.
// once there are any calls registered on this promise, or
// the promise has been already handled, it will be non-zero.
chainStatus atomic.Uint32
}
// extQueue wil be owned, at any time, by a single goroutine.
type extQueue[T any] struct {
// initState is the state value at the time this queue was created.
// note: the reason for this field is, during extension calls,
// if the promise was resolved while creating this extQueue's chan,
// the two select cases for the blocking scenario might be available
// at the same time.
// because the syncCtx.chan will be closed, and the extQ value will
// be available only to this goroutine, at the same time.
// so we need a way to still detect if the promise was resolved and
// read its result sync, otherwise its result would be lost.
initState State
// whether the call value is valid or not.
valid bool
// call is the default extension call.
call extCall[T]
// extra holds any other extension calls, in addition to the one in call.
extra []extCall[T]
}
// extCall describes an extension call and how to communicate back to it.
type extCall[T any] struct {
// resChan is used to send the result back to the extension call's promise.
// this is a new, per extension call, unbuffered channel.
resChan chan<- IdxRes[T]
// syncChan is the channel used to communicate that the extension call's
// promise has been resolved, so that the sending promise can return.
// this is typically extension call's promise's syncChan.
syncChan <-chan struct{}
// idx is the index of this result's promise within the list of promises
// passed to the extension call.
idx int
}
func (p *Promise[T]) Val() T {
return p.Res().Val()
}
func (p *Promise[T]) Err() error {
return p.Res().Err()
}
func (p *Promise[T]) State() State {
return p.Res().State()
}
// String will block until the promise is resolved.
func (p *Promise[T]) String() string {
return fmt.Sprintf("%v", p.Res())
}
func (p *Promise[T]) Wait() {
p.regChainWait()
p.waitCall()
}
func (p *Promise[T]) WaitChan() <-chan struct{} {
return p.syncCtx.Done()
}
func (p *Promise[T]) waitCall() {
// wait the promise to be resolved
s := p.wait()
// if the chain has a read call or is already handled, return early
if p.chainStatus.Load() >= chainStatusRead {
return
}
// at this point, the promise is not handled and doesn't have a read call,
// so call the unhandled handlers if the state is one of a failure.
switch s {
case Rejected:
p.unhandledError()
case Panicked:
p.unhandledPanic()
}
}
func (p *Promise[T]) Res() Result[T] {
p.regChainRead()
return p.resCall()
}
func (p *Promise[T]) resCall() Result[T] {
// wait the promise to be resolved
p.wait()
// if it's a call to handle the result, set the 'Handled' flag.
// also, keep track of whether this handle was valid(first) or not,
// to decide whether we should return the actual result of the promise
// or an erroneous one.
validHandle := p.setChainHandled()
// if the promise isn't a one-time promise, all handle calls will be valid
if !p.isOnetimePromise() {
validHandle = true
}
// if the promise result has been used, return the expected error
if !validHandle {
return errPromiseConsumedResult[T]{}
}
// the promise result can be accessed multiple times...
return getFinalRes(p.res)
}
// getFinalRes returns the final result to be used when returned outside
// the scope of the internal functions here.
func getFinalRes[T any](res Result[T]) Result[T] {
// if no result was set, then it's implicitly the empty result
if res == nil {
return emptyResult[T]{}
}
return res
}
func (p *Promise[T]) Callback(
cb func(ctx context.Context, res Result[T]),
) {
if cb == nil {
panic(nilCallbackPanicMsg)
}
if p.group.isWaiting() {
return
}
if p.syncCtx == neverClosedSyncCtx {
return
}
if !p.group.reserveGoroutine(p.regChainRead) {
return
}
ctx, cancel := p.group.callbackCtx(nil)
debug(p, startHandler, startFollowHandler, startCallbackFollowHandler)
go callbackFollowHandler(p, cb, ctx, cancel)
}
func callbackFollowHandler[T any](
prevProm *Promise[T],
cb callbackCallback[T, T],
ctx context.Context,
cancel context.CancelFunc,
) {
// make sure we free this goroutine reservation
defer prevProm.group.freeGoroutine()
// wait the previous promise to be resolved
prevProm.wait()
// run the callback with the actual promise result
runCallbackHandler[T, T](nil, cb, prevProm.res, false, false, ctx, cancel)
debug(prevProm, endHandler, endFollowHandler, endCallbackFollowHandler)
}
func (p *Promise[T]) Delay(
d time.Duration,
cond ...DelayCond,
) *Promise[T] {
if p.group.isWaiting() {
return newPromSync[T](p.group, errPromiseGroupDoneResult[T]{})
}
if p.syncCtx == neverClosedSyncCtx {
// since the syncCtx is nil, this promise will never be resolved,
// so no point in allocating a new value.
// note: this can only happen if the promise is created by passing a
// Context value that's never canceled(nil Done) to the Ctx constructor.
return p
}
if !p.group.reserveGoroutine(p.regChainRead) {
return newPromSync[T](p.group, errPromiseGroupBusyResult[T]{})
}
nextProm := newPromInter[T](p.group)
flags := getDelayFlags(cond)
debug(p, startHandler, startFollowHandler, startDelayFollowHandler)
go delayFollowHandler(p, nextProm, d, flags)
return nextProm
}
// delay creates Promise values with the same type
func delayFollowHandler[T any](
prevProm *Promise[T],
nextProm *Promise[T],
dd time.Duration,
flags delayFlags,
) {
defer prevProm.group.freeGoroutine()
prevProm.wait()
// mark prevProm as 'Handled', and check whether we should continue or not.
// the res value returned will hold the correct value that should be used.
res, ok := handleFollow(prevProm, nextProm, false)
if !ok {
// it's not a valid handle. it's considered a failure.
if flags.onError {
time.Sleep(dd)
}
nextProm.resolveToRejectedRes(res)
return
}
nextProm.resolveToResWithDelay(res, dd, flags)
debug(prevProm, endHandler, endFollowHandler, endDelayFollowHandler)
}
func (p *Promise[T]) Then(
thenCb func(ctx context.Context, res Result[T]) Result[T],
) *Promise[T] {
if thenCb == nil {
panic(nilCallbackPanicMsg)
}
if p.group.isWaiting() {
return newPromSync[T](p.group, errPromiseGroupDoneResult[T]{})
}
if p.syncCtx == neverClosedSyncCtx {
return p
}
if !p.group.reserveGoroutine(p.regChainRead) {
return newPromSync[T](p.group, errPromiseGroupBusyResult[T]{})
}
nextProm := newPromInter[T](p.group)
ctx, cancel := p.group.callbackCtx(nextProm.syncCtx)
debug(p, startHandler, startFollowHandler, startThenFollowHandler)
go thenFollowHandler(p, nextProm, thenCb, ctx, cancel)
return nextProm
}
func thenFollowHandler[T any](
prevProm *Promise[T],
nextProm *Promise[T],
cb followCallback[T, T],
ctx context.Context,
cancel context.CancelFunc,
) {
defer prevProm.group.freeGoroutine()
s := prevProm.wait()
// 'Then' can handle only the 'Fulfilled' state, so return otherwise
if s != Fulfilled {
nextProm.resolveToRes(prevProm.res)
return
}
// mark prev as 'Handled', and check whether we should continue or not.
// the res value returned will hold the correct value that should be handled
// by the callback.
res, ok := handleFollow(prevProm, nextProm, true)
if !ok {
// return, since the promise is now resolved
return
}
// run the callback with the actual promise result
runCallbackHandler[T, T](nextProm, cb, res, true, true, ctx, cancel)
debug(prevProm, endHandler, endFollowHandler, endThenFollowHandler)
}
func (p *Promise[T]) Catch(
catchCb func(ctx context.Context, res Result[T]) Result[T],
) *Promise[T] {
if catchCb == nil {
panic(nilCallbackPanicMsg)
}
if p.group.isWaiting() {
return newPromSync[T](p.group, errPromiseGroupDoneResult[T]{})
}
if p.syncCtx == neverClosedSyncCtx {
return p
}
if !p.group.reserveGoroutine(p.regChainRead) {
return newPromSync[T](p.group, errPromiseGroupBusyResult[T]{})
}
nextProm := newPromInter[T](p.group)
ctx, cancel := p.group.callbackCtx(nextProm.syncCtx)
debug(p, startHandler, startFollowHandler, startCatchFollowHandler)
go catchFollowHandler(p, nextProm, catchCb, ctx, cancel)
return nextProm
}
func catchFollowHandler[T any](
prevProm *Promise[T],
nextProm *Promise[T],
cb followCallback[T, T],
ctx context.Context,
cancel context.CancelFunc,
) {
defer prevProm.group.freeGoroutine()
s := prevProm.wait()
// 'Catch' can handle only the 'Rejected' state, so return otherwise
if s != Rejected {
nextProm.resolveToRes(prevProm.res)
return
}
// mark prev as 'Handled'.
// the res value returned will hold the correct value that should be handled
// by the callback.
res, _ := handleFollow(prevProm, nextProm, false)
// run the callback with the actual promise result
runCallbackHandler[T, T](nextProm, cb, res, true, true, ctx, cancel)
debug(prevProm, endHandler, endFollowHandler, endCatchFollowHandler)
}
func (p *Promise[T]) Recover(
recoverCb func(ctx context.Context, res Result[T]) Result[T],
) *Promise[T] {
if recoverCb == nil {
panic(nilCallbackPanicMsg)
}
if p.group.isWaiting() {
return newPromSync[T](p.group, errPromiseGroupDoneResult[T]{})
}
if p.syncCtx == neverClosedSyncCtx {
return p
}
if !p.group.reserveGoroutine(p.regChainRead) {
return newPromSync[T](p.group, errPromiseGroupBusyResult[T]{})
}
nextProm := newPromInter[T](p.group)
ctx, cancel := p.group.callbackCtx(nextProm.syncCtx)
debug(p, startHandler, startFollowHandler, startRecoverFollowHandler)
go recoverFollowHandler(p, nextProm, recoverCb, ctx, cancel)
return nextProm
}
func recoverFollowHandler[T any](
prevProm *Promise[T],
nextProm *Promise[T],
cb followCallback[T, T],
ctx context.Context,
cancel context.CancelFunc,
) {
defer prevProm.group.freeGoroutine()
s := prevProm.wait()
// 'Recover' can handle only the 'Panicked' state, so return otherwise
if s != Panicked {
nextProm.resolveToRes(prevProm.res)
return
}
// mark prev as 'Handled', and check whether we should continue or not.
// the res value returned will hold the correct value that should be handled
// by the callback.
res, ok := handleFollow(prevProm, nextProm, true)
if !ok {
// return, since the promise is now resolved
return
}
// run the callback with the actual promise result
runCallbackHandler[T, T](nextProm, cb, res, true, true, ctx, cancel)
debug(prevProm, endHandler, endFollowHandler, endRecoverFollowHandler)
}
func (p *Promise[T]) Finally(
finallyCb func(ctx context.Context),
) *Promise[T] {
if finallyCb == nil {
panic(nilCallbackPanicMsg)
}
if p.group.isWaiting() {
return newPromSync[T](p.group, errPromiseGroupDoneResult[T]{})
}
if p.syncCtx == neverClosedSyncCtx {
return p
}
if !p.group.reserveGoroutine(p.regChainRead) {
return newPromSync[T](p.group, errPromiseGroupBusyResult[T]{})
}
nextProm := newPromInter[T](p.group)
ctx, cancel := p.group.callbackCtx(nextProm.syncCtx)
debug(p, startHandler, startFollowHandler, startFinallyFollowHandler)
go finallyFollowHandler(p, nextProm, finallyCb, ctx, cancel)
return nextProm
}
// finallyFollowHandler is like an asyncReadCall, except that it can't set the 'Handled'
// flag(handle the promise), and it can return new promise with new result.
// if we made the finally a normal 'follow' method(like then,..), it will be
// possible to call it on a panicked promise and return a fulfilled promise,
// and the panic will be dismissed implicitly, which is something we don't want.
func finallyFollowHandler[T any](
prevProm *Promise[T],
nextProm *Promise[T],
cb finallyCallback[T, T],
ctx context.Context,
cancel context.CancelFunc,
) {
defer prevProm.group.freeGoroutine()
prevProm.wait()
runCallbackHandler[T, T](nextProm, cb, prevProm.res, false, true, ctx, cancel)
debug(prevProm, endHandler, endFollowHandler, endFinallyFollowHandler)
}