-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpool.go
More file actions
273 lines (231 loc) · 4.17 KB
/
pool.go
File metadata and controls
273 lines (231 loc) · 4.17 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
package async_utils
import (
"fmt"
"sync"
"go.uber.org/atomic"
)
type PoolFunc func()
type EasyPool struct {
limit chan struct{}
pool chan PoolFunc
close PoolFunc
}
// NewPoolFunc 方法池
func NewPoolFunc(size int, close PoolFunc) *EasyPool {
pool := &EasyPool{
limit: make(chan struct{}, size),
pool: make(chan PoolFunc, size),
close: close,
}
go pool.core()
return pool
}
// Send 下发任务
func (e *EasyPool) Send(fn PoolFunc) {
e.pool <- fn
}
// Over 下发完毕信号 (任务下发完毕时调用)
func (e *EasyPool) Over() {
close(e.pool)
}
func (e *EasyPool) core() {
wg := sync.WaitGroup{}
loop:
for {
select {
case fn, over := <-e.pool:
if !over {
break loop
}
e.limit <- struct{}{}
wg.Add(1)
go func(fn PoolFunc) {
defer func() {
if err := recover(); err != nil {
PrintStack()
fmt.Println("Recover Err: ", err)
}
}() // 我怕你们乱写逻辑 把系统弄炸了
defer func() {
<-e.limit
wg.Done()
}()
fn()
}(fn)
}
}
wg.Wait()
e.close()
}
type SinglePoolFunc func() error
// SinglePool 可停止的pool
type SinglePool struct {
pool chan SinglePoolFunc
close PoolFunc
limit chan struct{}
err error
rmu sync.Mutex
stop atomic.Bool // stop 控制错误关闭
closeChan chan struct{}
}
func NewSinglePool(size int, close PoolFunc) *SinglePool {
pool := &SinglePool{
limit: make(chan struct{}, size),
pool: make(chan SinglePoolFunc, size),
close: close,
closeChan: make(chan struct{}),
}
go pool.core()
return pool
}
// Send 下发任务
func (s *SinglePool) Send(fn SinglePoolFunc) {
if !s.stop.Load() {
s.pool <- fn
}
}
// Over 下发完毕信号 (任务下发完毕时调用)
func (s *SinglePool) Over() {
close(s.pool)
}
func (s *SinglePool) core() {
wg := sync.WaitGroup{}
loop:
for {
select {
case fn, over := <-s.pool:
if !over {
break loop
}
if s.stop.Load() {
break loop
}
s.limit <- struct{}{}
wg.Add(1)
go func(fn SinglePoolFunc) {
defer func() {
if err := recover(); err != nil {
PrintStack()
fmt.Println("Recover Err: ", err)
} // 我怕你们乱写逻辑 把系统弄炸了
<-s.limit
wg.Done()
}()
err := fn()
if err != nil {
s.rmu.Lock()
s.err = err
s.stop.Store(true)
s.rmu.Unlock()
}
}(fn)
}
}
// 判断是否是因为错误而停止
if !s.stop.Load() {
wg.Wait()
}
close(s.closeChan)
s.close()
}
func (s *SinglePool) Error() error {
<-s.closeChan
return s.err
}
// Greedy Pool
type greedyPool struct {
pool chan PoolFunc
over chan struct{}
close PoolFunc
}
// NewGreedyPool: return greed pool
func NewGreedyPool(size int, close PoolFunc) *greedyPool {
pool := &greedyPool{
pool: make(chan PoolFunc, size),
over: make(chan struct{}),
close: close,
}
go pool.scheduler(size)
return pool
}
// Over: End of task issuance
func (g *greedyPool) Over() {
close(g.over)
}
// Send: Issue a task
func (g *greedyPool) Send(fn PoolFunc) {
g.pool <- fn
}
func (g *greedyPool) scheduler(size int) {
var wg sync.WaitGroup
for i := 0; i < size; i++ {
wg.Add(1)
go g.task(&wg)
}
wg.Wait()
g.close()
}
func (g *greedyPool) task(wg *sync.WaitGroup) {
defer func() {
wg.Done()
// recover
if err := recover(); err != nil {
PrintStack()
fmt.Println("Recover Err: ", err)
}
}()
loop:
for {
select {
case i, ex := <-g.pool:
if !ex {
break loop
}
i()
case <-g.over:
break loop
}
}
}
// 再来一个简单任务池
type SimpleTask struct {
funcs chan func()
ove chan struct{}
wg sync.WaitGroup
}
func NewSimpleTask(limit int) *SimpleTask {
sim := SimpleTask{
funcs: make(chan func(), limit),
ove: make(chan struct{}),
}
go sim.core()
return &sim
}
//func (s *SimpleTask) Over() {
// close(s.funcs)
//}
func (s *SimpleTask) AddTask(fn func()) {
s.funcs <- fn
}
func (s *SimpleTask) Wait() {
close(s.funcs)
<-s.ove
}
func (s *SimpleTask) core() {
loop:
for {
select {
case fn, exit := <-s.funcs:
if !exit {
break loop
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
fn()
}()
}
}
s.wg.Wait()
s.ove <- struct{}{}
}