-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfuture.go
More file actions
304 lines (266 loc) · 6.93 KB
/
future.go
File metadata and controls
304 lines (266 loc) · 6.93 KB
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
package executor
import (
"context"
"math"
"sync"
"time"
)
// 任务的状态
const (
taskStateNew = iota
taskStateRunning
taskStateComplete
taskStateCancel
taskStateError
)
// NewTaskFuture 创建一个TaskFuture
func NewTaskFuture(callable Callable) *TaskFuture {
taskFuture := &TaskFuture{
callable: callable,
state: taskStateNew,
listenerQueue: newLockQueue[Listener](NewLinkedQueue[Listener](math.MaxInt32)),
contextCancelQueue: newLockQueue[context.CancelFunc](NewLinkedQueue[context.CancelFunc](math.MaxInt32)),
}
return taskFuture
}
// TaskFuture 代表一个任务,且有着执行后的信息,callable、callableMultiple、runnable 3个只会存在一个有值
type TaskFuture struct {
callable Callable
//任务状态
state uint8
//单个返回值结果
result any
//恐慌错误信息
panicError any
//读写锁
rwMux sync.RWMutex
//监听器队列
listenerQueue Queue[Listener]
//取消阻塞队列
contextCancelQueue Queue[context.CancelFunc]
}
func (t *TaskFuture) Run() {
//如果被取消则结束
var cancel bool
var ctxCancels []context.CancelFunc
var listeners []Listener
t.writeLockOperate(func() {
//如果被取消,则不继续运行
if t.state == taskStateCancel {
cancel = true
} else {
//不被取消
if t.state == taskStateNew {
//设置为运行状态
t.state = taskStateRunning
cancel = false
}
}
})
//不被取消则继续运行,被取消成功的已经回调了
if !cancel {
//只有运行状态才可以继续,只运行一次
if t.stateLock() == taskStateRunning {
defer func() {
//该层不处理恐慌,只唤醒和设置异常
if r := recover(); r != nil {
//设置为错误,唤醒和回调
t.writeLockOperate(func() {
t.state = taskStateError
t.panicError = r
ctxCancels = t.contextCancelQueue.RemoveAll()
listeners = t.listenerQueue.RemoveAll()
})
t.doCancelAndOnDone(listeners, ctxCancels)
//向上抛出恐慌,该层只记录
panic(r)
}
}()
//执行运行
t.doRun()
}
}
}
func (t *TaskFuture) Cancel() bool {
ok := false
var ctxCancels []context.CancelFunc
var listeners []Listener
t.writeLockOperate(func() {
if t.state == taskStateNew {
t.state = taskStateCancel
//取消成功,则回调,只通过该方式回调一次
ctxCancels = t.contextCancelQueue.RemoveAll()
listeners = t.listenerQueue.RemoveAll()
ok = true
} else {
ok = t.state == taskStateCancel
}
})
t.doCancelAndOnDone(listeners, ctxCancels)
return ok
}
func (t *TaskFuture) IsCancelled() bool {
t.rwMux.RLock()
defer t.rwMux.RUnlock()
return t.state == taskStateCancel
}
func (t *TaskFuture) IsDone() bool {
t.rwMux.RLock()
defer t.rwMux.RUnlock()
return t.isDone()
}
func (t *TaskFuture) IsSuccess() bool {
t.rwMux.RLock()
defer t.rwMux.RUnlock()
return t.state == taskStateComplete
}
func (t *TaskFuture) PanicError() any {
t.rwMux.RLock()
defer t.rwMux.RUnlock()
return t.panicError
}
func (t *TaskFuture) Get() any {
return t.timeoutHandle(maxTimeDuration)
}
func (t *TaskFuture) GetTimeout(timeout time.Duration) any {
return t.timeoutHandle(timeout)
}
func (t *TaskFuture) AddListener(listenerFunc ListenerFunc) {
t.AddListeners(Listener(listenerFunc))
}
func (t *TaskFuture) AddListeners(listeners ...Listener) {
// 任务未完成,添加进队列中
ok := t.noDoneOperateLock(func() {
for _, listener := range listeners {
t.listenerQueue.Enqueue(listener)
}
})
//任务已完成回调
if !ok {
t.doOnDone(listeners)
}
}
// noDoneOperateLock 任务状态未完成时,加锁进行操作,保证并发安全
func (t *TaskFuture) noDoneOperateLock(f func()) (ok bool) {
t.writeLockOperate(func() {
if !t.isDone() {
f()
ok = true
}
})
return
}
// isDone 任务是否完成,不带锁
func (t *TaskFuture) isDone() bool {
return t.state == taskStateComplete || t.state == taskStateCancel || t.state == taskStateError
}
// timeoutHandle 超时处理
func (t *TaskFuture) timeoutHandle(timeout time.Duration) any {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
//任务未完成时加锁写入,保证状态和队列信息一致
ok := t.noDoneOperateLock(func() {
t.contextCancelQueue.Enqueue(cancel)
})
if !ok {
return t.resultLock()
} else {
//任务未完成,等待超时或完成唤醒
select {
case <-ctx.Done():
return t.resultLock()
}
}
}
// doRun 执行任务
func (t *TaskFuture) doRun() {
//开始执行任务,只执行一个
result := t.callable.Call()
//回调信息
var ctxCancels []context.CancelFunc
var listeners []Listener
t.writeLockOperate(func() {
t.state = taskStateComplete
t.result = result
ctxCancels = t.contextCancelQueue.RemoveAll()
listeners = t.listenerQueue.RemoveAll()
})
//唤醒和回调
t.doCancelAndOnDone(listeners, ctxCancels)
}
// writeLockOperate 加写锁进行操作
func (t *TaskFuture) writeLockOperate(f func()) {
t.rwMux.Lock()
defer t.rwMux.Unlock()
f()
}
// resultLock 加读锁获取结果
func (t *TaskFuture) resultLock() any {
t.rwMux.RLock()
defer t.rwMux.RUnlock()
return t.result
}
// readLockOperate 加读锁进行操作
func (t *TaskFuture) readLockOperate(f func()) {
t.rwMux.RLock()
defer t.rwMux.RUnlock()
f()
}
// stateLock 加锁获取状态
func (t *TaskFuture) stateLock() uint8 {
t.rwMux.RLock()
defer t.rwMux.RUnlock()
return t.state
}
// doOnDone 执行监听回调
func (t *TaskFuture) doOnDone(listeners []Listener) {
for _, listener := range listeners {
listener.OnDone(t)
}
}
// doCancel 执行任务取消
func (t *TaskFuture) doCancel(cancels []context.CancelFunc) {
for _, cancel := range cancels {
cancel()
}
}
// doCancelAndOnDone 执行任务取消和监听回调
func (t *TaskFuture) doCancelAndOnDone(listeners []Listener, cancels []context.CancelFunc) {
t.doOnDone(listeners)
t.doCancel(cancels)
}
// ListenerFunc 监听器 简写
type ListenerFunc func(Future)
func (l ListenerFunc) OnDone(future Future) {
l(future)
}
// ListenableFuture 监听器的Future 提供监听回调的功能
type ListenableFuture interface {
Future
// AddListener 添加一个监听器
AddListener(listenerFunc ListenerFunc)
// AddListeners 添加多个监听器
AddListeners(listeners ...Listener)
}
// Future 异步结果
type Future interface {
// Cancel 取消任务 bool 是否取消成功
Cancel() bool
// IsCancelled 是否任务被取消
IsCancelled() bool
// IsDone 任务是否完成,取消、异常、完成都算完成
IsDone() bool
// IsSuccess 任务是否成功执行,只有正常完成才算成功
IsSuccess() bool
// PanicError 获取恐慌信息,当发生恐慌时才有
PanicError() any
// Get 阻塞等待至任务完成
Get() any
// GetTimeout 阻塞等待至任务完成或超时
GetTimeout(timeout time.Duration) any
}
// Listener 监听器,任务完成时回调
type Listener interface {
// OnDone 回调的方法中不能抛出恐慌
OnDone(future Future)
}