-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal.go
446 lines (393 loc) · 12 KB
/
internal.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
// Copyright 2023 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"
"errors"
"time"
)
// panic messages
const (
nilResChanPanicMsg = "promise: the provided channel value is nil"
nilCtxPanicMsg = "promise: the provided Context value is nil"
nilCallbackPanicMsg = "promise: the provided function value is nil"
)
const (
chainStatusEmpty = iota
chainStatusWait
chainStatusRead
chainStatusHandled
)
func (p *Promise[T]) regChainWait() {
// this will do nothing if the chain was already wait, read or handled.
p.chainStatus.CompareAndSwap(chainStatusEmpty, chainStatusWait)
}
func (p *Promise[T]) regChainRead() {
// fast-path, in case we are trying to do a redundant CAS, returns early.
if p.chainStatus.Load() >= chainStatusRead {
return
}
// this will first try to swap the read value with the empty value,
// otherwise, will assume the current value is wait and try to swap it.
// this will do nothing if the chain was already read or handled.
if !p.chainStatus.CompareAndSwap(chainStatusEmpty, chainStatusRead) {
p.chainStatus.CompareAndSwap(chainStatusWait, chainStatusRead)
}
}
func (p *Promise[T]) setChainHandled() (validHandle bool) {
// this will return true, only if the chain wasn't already handled.
oldChain := p.chainStatus.Swap(chainStatusHandled)
return oldChain != chainStatusHandled
}
func (p *Promise[T]) extsChan() chan extQueue[T] {
extChanP := p.extChanP.Load()
if extChanP != nil {
return *extChanP
}
// initiate the queue value, even though the chan might not be used after all,
// because if we initiated it after the CAS below, it might be available to some
// call and make that call block until we initiate it.
extChan := make(chan extQueue[T], 1)
extChan <- extQueue[T]{
initState: State(p.resolveState.Load()),
}
if p.extChanP.CompareAndSwap(nil, &extChan) {
return extChan
}
extChanP = p.extChanP.Load()
return *extChanP
}
// wait waits for the promise p to be resolved, by either blocking on receiving
// from the syncChan, or by using the resolveState value of the promise.
func (p *Promise[T]) wait() State {
// if the resolve status is non-zero, then no need to wait, as
// this is guaranteed to happen before closing the sync channel.
s := p.resolveState.Load()
if s != 0 {
return State(s)
}
// wait until the promise is resolved.
// the Context will be closed by the previous promise,
// after setting the res and status fields as expected.
<-p.syncCtx.Done()
s = p.resolveState.Load()
return State(s)
}
func (p *Promise[T]) isOnetimePromise() bool {
if p.group == nil {
return false
}
return p.group.core.onetimeHandling
}
func (p *Promise[T]) unhandledPanic() {
if p.group == nil {
return
}
debug(p, startUnhandledPanicLogic)
// if it's request to cancel all the promises of the group on
// uncaught panics, call the cancel function before the handler.
if p.group.core.cancel != nil {
p.group.core.cancel()
}
// call the callback if one is provided
if p.group.core.unhandledPanicCB != nil {
debug(p, callUnhandledPanicCallback)
p.group.core.unhandledPanicCB(getPanicVFromRes(p.res))
}
}
func (p *Promise[T]) unhandledError() {
if p.group == nil {
return
}
debug(p, startUnhandledErrorLogic)
// if it's request to cancel all the promises of the group on
// uncaught errors, call the cancel function before the handler.
if p.group.core.cancel != nil {
p.group.core.cancel()
}
// call the callback if one is provided
if p.group.core.unhandledErrorCB != nil {
debug(p, callUnhandledErrorCallback)
p.group.core.unhandledErrorCB(p.res.Err())
}
}
func (p *Promise[T]) resolveToRes(res Result[T]) {
// res will be nil after a Finally callback on a Promise with nil Result,
// after a callback that doesn't support returning Result, or when it's
// explicitly returned from a callback that supports returning Result.
if res == nil {
p.resolveToFulfilledRes(nil)
return
}
// resolve the provided Promise to the provided Result, accordingly.
// Note: if res is a Promise value, the State call will block until that
// Promise is resolved.
switch s := res.State(); s {
case Panicked:
p.resolveToPanickedRes(res)
case Rejected:
p.resolveToRejectedRes(res)
case Fulfilled:
p.resolveToFulfilledRes(res)
default:
panic("promise: unexpected Result state: " + s.String())
}
}
func (p *Promise[T]) resolveToResWithDelay(
res Result[T],
dd time.Duration,
flags delayFlags,
) {
if res == nil {
if flags.onSuccess {
time.Sleep(dd)
}
p.resolveToFulfilledRes(nil)
return
}
switch s := res.State(); s {
case Panicked:
if flags.onPanic {
time.Sleep(dd)
}
p.resolveToPanickedRes(res)
case Rejected:
if flags.onError {
time.Sleep(dd)
}
p.resolveToRejectedRes(res)
case Fulfilled:
if flags.onSuccess {
time.Sleep(dd)
}
p.resolveToFulfilledRes(res)
default:
panic("promise: unexpected Result state: " + s.String())
}
}
func (p *Promise[T]) resolveToPanickedRes(
res Result[T],
) {
debug(p, resolve, resolvePanicked)
// save the result before executing any callbacks.
p.res = res
if p.chainStatus.Load() == chainStatusEmpty {
// if the chain is empty (no follow, read or wait calls), execute
// the unhandled panic logic.
// otherwise, it will be delayed until the last call in the chain.
p.unhandledPanic()
}
// resolve with the Panicked status.
p.resolveState.Store(uint32(Panicked))
closeSyncCtx(p.syncCtx) // unblocks all calls waiting on p.
handleExtCalls(p) // handles all extension calls that involve p.
// note: any code that gets added after closeSyncCtx and handleExtCalls
// isn't guaranteed to be executed without extra wait arrangements.
}
func (p *Promise[T]) resolveToRejectedRes(
res Result[T],
) {
debug(p, resolve, resolveRejected)
p.res = res
if p.chainStatus.Load() == chainStatusEmpty {
p.unhandledError()
}
p.resolveState.Store(uint32(Rejected))
closeSyncCtx(p.syncCtx)
handleExtCalls(p)
}
func (p *Promise[T]) resolveToFulfilledRes(
res Result[T],
) {
debug(p, resolve, resolveFulfilled)
p.res = res
p.resolveState.Store(uint32(Fulfilled))
closeSyncCtx(p.syncCtx)
handleExtCalls(p)
}
func (p *Promise[T]) resolveToResSync(res Result[T]) {
if res == nil {
p.resolveState.Store(uint32(Fulfilled))
return
}
p.res = res
state := res.State()
switch state {
case Panicked:
p.unhandledPanic()
case Rejected:
p.unhandledError()
case Fulfilled:
// nothing special to be done.
default:
panic("promise: unexpected Result state: " + state.String())
}
p.resolveState.Store(uint32(state))
}
func handleFollow[PrevT, NextT any](
prevProm *Promise[PrevT],
nextProm *Promise[NextT],
resolveOnErr bool,
) (resToBeHandled Result[PrevT], valid bool) {
// set the 'Handled' flag, and keep track of whether this handle is
// valid(first) or not, to decide whether we should move forward and
// use the actual result of the promise or reject with an erroneous one.
validHandle := prevProm.setChainHandled()
// if the promise isn't a one-time promise, all handle calls will be valid
if !prevProm.isOnetimePromise() {
validHandle = true
}
// if the promise result has been used, either return or resolve with the expected error
if !validHandle {
if resolveOnErr {
nextProm.resolveToRejectedRes(errPromiseConsumedResult[NextT]{})
return nil, false
}
return errPromiseConsumedResult[PrevT]{}, false
}
// the promise result can be accessed multiple times...
// Note: res might be nil if the previous promise was fulfilled via a nil return.
return prevProm.res, true
}
// handleReturns must be deferred.
// the callback function is called after a deferred call to this method.
// no internal call that may cause a panic should be called after this method.
// TODO: pass a new value, panicked (similar to valid from the sync.OnceFunc implementation),
// and make the handleReturns function uses this value to tell whether the nil value is valid or not.
func handleReturns[PrevResT, NewResT any](
p *Promise[NewResT],
prevRes Result[PrevResT],
newResP *Result[NewResT],
) {
// get the new Result value based on the state of the callback
var newRes Result[NewResT]
if v := recover(); v != nil {
// the callback panicked, create the appropriate Result value.
newRes = panicResult[NewResT]{v: v}
} else {
// the callback returned normally, called runtime.Goexit, or
// called panic with nil value.
newRes = getEffectiveNewRes(prevRes, newResP)
}
// resolve the provided Promise to the new Result value.
p.resolveToRes(newRes)
}
func getEffectiveNewRes[PrevResT, NewResT any](
prevRes Result[PrevResT],
newResP *Result[NewResT],
) (effRes Result[NewResT]) {
// if a new Result is set, return it.
if newResP != nil {
return *newResP
}
// if there was no previous Result provided, return the zero value
// of the new Result.
if prevRes == nil {
return effRes
}
// no new result is set, and the previous Result is non-nil, so try
// to cast the previous Result to the new Result's type...
// handle having the previous Result value as nil.
anyPrevRes := any(prevRes.Val())
if anyPrevRes == nil {
return result[NewResT]{
err: prevRes.Err(),
state: prevRes.State(),
}
}
// handle having the previous Result's type compatible with the new one.
prevResVal, ok := anyPrevRes.(NewResT)
if ok {
return result[NewResT]{
val: prevResVal,
err: prevRes.Err(),
state: prevRes.State(),
}
}
// TODO: this can't happen in the current implementation,
// as all type parameters used so far is the same type.
return Err[NewResT](errors.New("TODO: unexpected"))
}
func handleExtCalls[T any](p *Promise[T]) (handled bool) {
debug(p, startExtCall)
extChanP := p.extChanP.Load()
if extChanP == nil {
debug(p, missingExtChan)
return false
}
debug(p, foundExtChan)
extQ := <-*extChanP
debug(p, foundExtQueue)
// handle not having any extension calls
if !extQ.valid {
return false
}
// get the final and ready-to-use result
res := getFinalRes(p.res)
// handle having a single extension call
handled = handleExtCall(extQ.call, res)
// handle having multiple extension calls
for _, call := range extQ.extra {
handled = handleExtCall(call, res) || handled
}
debug(p, endExtCall)
return handled
}
func handleExtCall[T any](call extCall[T], res Result[T]) bool {
select {
case call.resChan <- IdxRes[T]{
Idx: call.idx,
Result: res,
}:
return true
case <-call.syncChan:
return false
}
}
// newPromInter creates a new Promise which is resolved internally,
// using an internal allocated channel.
func newPromInter[T any](g *Group[T]) *Promise[T] {
return &Promise[T]{
group: g,
syncCtx: newSyncCtx(),
}
}
func newPromCtx[T any](g *Group[T], ctx context.Context) *Promise[T] {
return &Promise[T]{
group: g,
syncCtx: ctx,
res: ctxResult[T]{ctx: ctx},
}
}
// newPromBlocked returns a promise that will never be resolved.
// it's used for promises for Ctx calls with empty context.Context value,
// or for follow calls on such promises.
func newPromBlocked[T any]() *Promise[T] {
return &Promise[T]{
syncCtx: neverClosedSyncCtx,
// no other fields need to be initialized, since this promise will never be resolved.
}
}
// newPromSync returns a new Promise which is resolved synchronously and
// immediately to the provided [Result] value.
func newPromSync[T any](g *Group[T], res Result[T]) *Promise[T] {
p := &Promise[T]{
group: g,
syncCtx: closedSyncCtx,
// no other fields are needed, since sync promises are resolved directly
// after created, so any extension call will depend on the syncChan.
}
p.resolveToResSync(res)
return p
}