-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool.go
117 lines (102 loc) 路 2.46 KB
/
pool.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
package gotcc
import (
"sync"
"github.com/panjf2000/ants/v2"
)
// Goroutine pool interface. It should be blocked until worker available.
type GoroutinePool interface {
Go(task func()) error
}
// Run the execution with a Coroutine Pool. If success, return a map[name]value, where names are task
// of termination dependent tasks and values are their return value.
// If failed, return ErrNoTermination, ErrLoopDependency or ErrAborted
func (m *TCController) PoolRun(pool GoroutinePool) (map[string]interface{}, error) {
if len(m.termination.dependency) == 0 {
return nil, ErrNoTermination{}
}
taskorder, noloop := m.analyzeDependency()
if !noloop {
return nil, ErrLoopDependency{}
}
sortedId, canSort := m.sortExecutor(taskorder)
if !canSort {
return nil, ErrPoolUnsupport{}
}
defer m.reset()
wg := sync.WaitGroup{}
lauchLoop:
for _, taskid := range sortedId {
e := m.executors[taskid]
wg.Add(1)
err := pool.Go(func() {
m.launch(e, &wg)
})
if err != nil {
return nil, err
}
select {
case <-m.cancelCtx.Done():
break lauchLoop
default:
}
}
// wait termination
t := m.termination
Results := map[string]interface{}{}
Aborted := false
waitLoop:
for !t.dependencyExpr.f() {
select {
case <-m.cancelCtx.Done():
// aborted
Aborted = true
break waitLoop
case msg := <-t.messageBuffer:
t.markDependency(msg.senderId, true)
Results[msg.senderName] = msg.value
}
}
if !Aborted {
// all done!
m.cancelFunc()
wg.Wait()
return Results, nil
} else {
// aborted because of some error
wg.Wait()
returnErr := ErrAborted{
TaskErrors: m.errorMsgs.items,
Cancelled: m.cancelled.items,
}
// do the rollback
returnErr.UndoErrors = m.undoStack.undoAll(&m.errorMsgs, &m.cancelled).items
// fmt.Println(returnErr.Error())
return nil, returnErr
}
}
// Default coroutine pool: actually not a coroutine pool but only launch new goroutines.
type DefaultNoPool struct{}
func (DefaultNoPool) Go(task func()) error {
go task()
return nil
}
// Default coroutine pool: base on ants pool.
type DefaultPool struct {
pool *ants.Pool
}
// Create a default coroutine pool with `size`.
func NewDefaultPool(size int) DefaultPool {
if size <= 0 {
pool, _ := ants.NewPool(size)
return DefaultPool{pool}
} else {
pool, _ := ants.NewPool(size, ants.WithPreAlloc(true))
return DefaultPool{pool}
}
}
func (p DefaultPool) Go(task func()) error {
return p.pool.Submit(task)
}
func (p DefaultPool) Close() {
p.pool.Release()
}