-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.go
378 lines (327 loc) · 8.51 KB
/
async.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
package async
import (
"context"
"errors"
"sync"
"time"
)
var (
ErrPanicOccurred = errors.New("panic occurred")
ErrIncomplete = errors.New("incomplete")
ErrCanceled = errors.New("operation canceled")
)
type Func[In, Out any] func(context.Context, In) (Out, error)
type ChainablePromise[Out any] struct {
val Out
err error
done chan struct{}
cancelFunc context.CancelFunc
}
func (cp *ChainablePromise[Out]) Get() (Out, error) {
<-cp.done
return cp.val, cp.err
}
func (cp *ChainablePromise[Out]) GetNow() (Out, error) {
select {
case <-cp.done:
return cp.val, cp.err
default:
var zero Out
return zero, ErrIncomplete
}
}
func RunPromise[Out any](ctx context.Context, f Func[context.Context, Out]) *ChainablePromise[Out] {
done := make(chan struct{})
cp := &ChainablePromise[Out]{
done: done,
}
go func() {
defer close(done)
defer func() {
if r := recover(); r != nil {
cp.err = ErrPanicOccurred
}
}()
select {
case <-ctx.Done():
cp.err = ErrCanceled
default:
cp.val, cp.err = f(ctx, nil) // Pass nil as input for RunPromise
}
}()
return cp
}
func ContinueWith[Out any](ctx context.Context, cp *ChainablePromise[Out], f Func[Out, Out]) *ChainablePromise[Out] {
done := make(chan struct{})
out := &ChainablePromise[Out]{
done: done,
}
go func() {
defer close(done)
select {
case <-ctx.Done():
out.err = ErrCanceled
default:
val, _ := cp.GetNow() // Ignore the error from the previous Promise
val2, err := f(ctx, val)
out.val = val2
out.err = err
}
}()
return out
}
func (cp *ChainablePromise[Out]) Filter(predicate func(Out) bool) *ChainablePromise[Out] {
return ContinueWith(context.Background(), cp, func(ctx context.Context, val Out) (Out, error) {
if predicate(val) {
return val, nil
}
return val, errors.New("filter condition not met")
})
}
func (cp *ChainablePromise[Out]) Catch(fallback Func[Out, Out]) *ChainablePromise[Out] {
done := make(chan struct{})
out := &ChainablePromise[Out]{
done: done,
}
go func() {
defer close(done)
select {
case <-cp.done:
// Do nothing; already closed
default:
val, err := cp.GetNow()
if err != nil {
val, _ = fallback(context.Background(), val)
}
out.val = val
out.err = err
}
}()
return out
}
func (cp *ChainablePromise[Out]) Finalize(finalizer func(Out, error)) *ChainablePromise[Out] {
done := make(chan struct{})
out := &ChainablePromise[Out]{
done: done,
}
go func() {
defer close(done)
val, err := cp.GetNow()
finalizer(val, err)
out.val = val
out.err = err
}()
return out
}
// Retry mechanism
func (cp *ChainablePromise[Out]) Retry(ctx context.Context, attempts int, delay time.Duration) *ChainablePromise[Out] {
retryFunc := func(ctx context.Context, t Out) (Out, error) {
for i := 0; i < attempts; i++ {
val, err := cp.val, cp.err
if err == nil {
return val, nil
}
select {
case <-ctx.Done():
return cp.val, ErrCanceled
case <-time.After(delay):
// Retry logic here if needed
}
}
return cp.val, cp.err
}
return ContinueWith(ctx, cp, retryFunc)
}
// Timeout operation
func (cp *ChainablePromise[Out]) Timeout(ctx context.Context, timeout time.Duration) *ChainablePromise[Out] {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return ContinueWith(ctx, cp, func(ctx context.Context, t Out) (Out, error) {
return cp.val, cp.err
})
}
// Any resolves when any of the promises in the slice is resolved.
func Any[Out any](ctx context.Context, promises ...*ChainablePromise[Out]) *ChainablePromise[Out] {
var wg sync.WaitGroup
out := make(chan struct{})
for _, p := range promises {
wg.Add(1)
go func(cp *ChainablePromise[Out]) {
defer wg.Done()
select {
case <-ctx.Done():
close(out)
case <-cp.done:
close(out)
}
}(p)
}
go func() {
wg.Wait()
close(out)
}()
return &ChainablePromise[Out]{done: out}
}
// All resolves when all of the promises in the slice are resolved.
func All[Out any](ctx context.Context, promises ...*ChainablePromise[Out]) *ChainablePromise[Out] {
var wg sync.WaitGroup
out := make(chan struct{})
for _, p := range promises {
wg.Add(1)
go func(cp *ChainablePromise[Out]) {
defer wg.Done()
select {
case <-ctx.Done():
case <-cp.done:
}
}(p)
}
go func() {
wg.Wait()
close(out)
}()
return &ChainablePromise[Out]{done: out}
}
// Race resolves when the first promise in the slice is resolved.
func Race[Out any](ctx context.Context, promises ...*ChainablePromise[Out]) *ChainablePromise[Out] {
out := make(chan struct{})
for _, p := range promises {
go func(cp *ChainablePromise[Out]) {
select {
case <-ctx.Done():
case <-cp.done:
close(out)
}
}(p)
}
return &ChainablePromise[Out]{done: out}
}
func (cp *ChainablePromise[Out]) Continue(onSuccess func(Out) Out, onFailure func(error) Out) *ChainablePromise[Out] {
done := make(chan struct{})
out := &ChainablePromise[Out]{
done: done,
}
go func() {
defer close(done)
val, err := cp.GetNow()
if err != nil {
out.val = onFailure(err)
} else {
out.val = onSuccess(val)
}
}()
return out
}
func (cp *ChainablePromise[Out]) OnSuccess(onSuccess func(Out)) *ChainablePromise[Out] {
return cp.Continue(func(val Out) Out {
onSuccess(val)
return val
}, func(err error) Out {
return cp.val
})
}
func (cp *ChainablePromise[Out]) OnFailure(onFailure func(error)) *ChainablePromise[Out] {
return cp.Continue(func(val Out) Out {
return cp.val
}, func(err error) Out {
onFailure(err)
return cp.val
})
}
// Delay introduces a delay before resolving the promise.
func (cp *ChainablePromise[Out]) Delay(ctx context.Context, delay time.Duration) *ChainablePromise[Out] {
out := &ChainablePromise[Out]{
done: make(chan struct{}),
}
go func() {
defer close(out.done)
select {
case <-time.After(delay):
val, _ := cp.GetNow()
out.val = val
case <-ctx.Done():
val, _ := cp.GetNow()
out.val = val
out.err = ErrCanceled
case <-cp.done:
val, _ := cp.GetNow()
out.val = val
out.err = ErrIncomplete
}
}()
return out
}
// RetryWithDelay retries the promise with a specified delay between attempts.
func (cp *ChainablePromise[Out]) RetryWithDelay(ctx context.Context, attempts int, delay time.Duration) *ChainablePromise[Out] {
retryFunc := func(ctx context.Context, t Out) (Out, error) {
for i := 0; i < attempts; i++ {
val, err := cp.val, cp.err
if err == nil {
return val, nil
}
select {
case <-ctx.Done():
return cp.val, ErrCanceled
case <-time.After(delay):
// Retry logic here if needed
}
}
return cp.val, cp.err
}
return ContinueWith(ctx, cp, retryFunc)
}
// TimeoutWithFallback applies a timeout to the promise and provides a fallback value or function if the timeout is reached.
func (cp *ChainablePromise[Out]) TimeoutWithFallback(ctx context.Context, timeout time.Duration, fallbackValue Out) *ChainablePromise[Out] {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return ContinueWith(ctx, cp, func(ctx context.Context, t Out) (Out, error) {
select {
case <-ctx.Done():
return fallbackValue, ErrCanceled
case <-cp.done:
return cp.val, cp.err
}
})
}
type BackoffFunc func(retryAttempt int) time.Duration
// ExponentialBackoff generates an exponential backoff duration based on the retry attempt.
func ExponentialBackoff(retryAttempt int) time.Duration {
return time.Duration(1<<uint(retryAttempt)) * time.Millisecond
}
// LinearBackoff generates a linear backoff duration based on the retry attempt.
func LinearBackoff(retryAttempt int) time.Duration {
return time.Duration(retryAttempt) * time.Millisecond
}
// Backoff applies a backoff strategy to the promise's retry attempts.
func (cp *ChainablePromise[Out]) Backoff(ctx context.Context, backoffFunc BackoffFunc) *ChainablePromise[Out] {
backoffRetryFunc := func(ctx context.Context, t Out) (Out, error) {
retryAttempt := 0
for {
val, err := cp.val, cp.err
if err == nil {
return val, nil
}
select {
case <-ctx.Done():
return cp.val, ErrCanceled
case <-time.After(backoffFunc(retryAttempt)):
// Retry logic here if needed
}
retryAttempt++
}
}
return ContinueWith(ctx, cp, backoffRetryFunc)
}
func WithCancellation[Out any](ctx context.Context) *ChainablePromise[Out] {
ctx, cancel := context.WithCancel(ctx)
done := make(chan struct{})
return &ChainablePromise[Out]{
done: done,
cancelFunc: cancel,
}
}
// Cancel cancels the ChainablePromise and releases associated resources.
func (cp *ChainablePromise[Out]) Cancel() {
cp.cancelFunc()
close(cp.done)
}