-
Notifications
You must be signed in to change notification settings - Fork 28
/
future_task.go
119 lines (105 loc) · 2.95 KB
/
future_task.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
package routine
import (
"fmt"
"sync"
"sync/atomic"
"time"
)
type taskState = int32
const (
taskStateNew taskState = iota
taskStateRunning
taskStateCompleted
taskStateCanceled
taskStateFailed
)
type futureTask[TResult any] struct {
await sync.WaitGroup
state taskState
callable FutureCallable[TResult]
result TResult
error RuntimeError
}
func (task *futureTask[TResult]) IsDone() bool {
state := atomic.LoadInt32(&task.state)
return state == taskStateCompleted || state == taskStateCanceled || state == taskStateFailed
}
func (task *futureTask[TResult]) IsCanceled() bool {
return atomic.LoadInt32(&task.state) == taskStateCanceled
}
func (task *futureTask[TResult]) IsFailed() bool {
return atomic.LoadInt32(&task.state) == taskStateFailed
}
func (task *futureTask[TResult]) Complete(result TResult) {
if atomic.CompareAndSwapInt32(&task.state, taskStateNew, taskStateCompleted) ||
atomic.CompareAndSwapInt32(&task.state, taskStateRunning, taskStateCompleted) {
task.result = result
task.await.Done()
}
}
func (task *futureTask[TResult]) Cancel() {
if atomic.CompareAndSwapInt32(&task.state, taskStateNew, taskStateCanceled) ||
atomic.CompareAndSwapInt32(&task.state, taskStateRunning, taskStateCanceled) {
task.error = NewRuntimeError("Task was canceled.")
task.await.Done()
}
}
func (task *futureTask[TResult]) Fail(error any) {
if atomic.CompareAndSwapInt32(&task.state, taskStateNew, taskStateFailed) ||
atomic.CompareAndSwapInt32(&task.state, taskStateRunning, taskStateFailed) {
runtimeErr, isRuntimeErr := error.(RuntimeError)
if !isRuntimeErr {
runtimeErr = NewRuntimeError(error)
}
task.error = runtimeErr
task.await.Done()
}
}
func (task *futureTask[TResult]) Get() TResult {
task.await.Wait()
if atomic.LoadInt32(&task.state) == taskStateCompleted {
return task.result
}
panic(task.error)
}
func (task *futureTask[TResult]) GetWithTimeout(timeout time.Duration) TResult {
waitChan := make(chan struct{})
go func() {
task.await.Wait()
close(waitChan)
}()
timer := time.NewTimer(timeout)
defer timer.Stop()
select {
case <-waitChan:
if atomic.LoadInt32(&task.state) == taskStateCompleted {
return task.result
}
panic(task.error)
case <-timer.C:
task.timeout(timeout)
task.await.Wait()
if atomic.LoadInt32(&task.state) == taskStateCompleted {
return task.result
}
panic(task.error)
}
}
func (task *futureTask[TResult]) Run() {
if atomic.CompareAndSwapInt32(&task.state, taskStateNew, taskStateRunning) {
defer func() {
if cause := recover(); cause != nil {
task.Fail(cause)
}
}()
result := task.callable(task)
task.Complete(result)
}
}
func (task *futureTask[TResult]) timeout(timeout time.Duration) {
if atomic.CompareAndSwapInt32(&task.state, taskStateNew, taskStateCanceled) ||
atomic.CompareAndSwapInt32(&task.state, taskStateRunning, taskStateCanceled) {
task.error = NewRuntimeError(fmt.Sprintf("Task execution timeout after %v.", timeout))
task.await.Done()
}
}