-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopool.go
306 lines (273 loc) · 4.99 KB
/
gopool.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
package threading
import (
"math"
"runtime"
"sync"
"time"
)
const (
DefaultMaxWorkCount = 256 * 1024
DefaultOverJobCount = 10
MaxIdleWorkerDuration = time.Second * 20
)
type (
goPool struct {
MaxWorkCount int
MinWorkCount int
MaxIdleWorkerDuration time.Duration
going []*goChan //就绪队列,可获取已就绪routine的通讯,写入/移除过程保证有序
goCount int //正在执行任务数量
goChanPool sync.Pool //通讯池
gsPool sync.Pool //对象池
mu sync.Mutex
}
goChan struct {
lastUseTime time.Time
task chan *task
}
PoolOpt func(*goPool)
task struct {
job func() error
gs *GoSync
}
)
var (
gp *goPool
allJob chan *task
once sync.Once
)
func SetMaxWorkCount(count int) PoolOpt {
return func(*goPool) {
if gp == nil || count == 0 {
return
}
if count < 0 {
gp.MaxWorkCount = math.MaxInt
}
gp.MaxWorkCount = count
if gp.MinWorkCount > gp.MaxWorkCount && gp.MaxWorkCount > 0 {
gp.MinWorkCount = gp.MaxWorkCount
}
}
}
func SetMinWorkCount(count int) PoolOpt {
return func(*goPool) {
if gp == nil || count == 0 {
return
}
if count > 0 {
gp.MinWorkCount = count
}
if gp.MinWorkCount > gp.MaxWorkCount && gp.MaxWorkCount > 0 {
gp.MinWorkCount = gp.MaxWorkCount
}
}
}
func SetMaxIdleWorkerDuration(duration time.Duration) PoolOpt {
return func(p *goPool) {
if gp == nil || duration <= 0 {
return
}
gp.MaxIdleWorkerDuration = duration
}
}
func tryGetTask() *task {
select {
case t := <-allJob:
return t
default:
return nil
}
}
var goChanCap = func() int {
if runtime.GOMAXPROCS(0) == 1 {
return 0
}
return 1
}()
func newGoPool() *goPool {
g := &goPool{
MaxIdleWorkerDuration: MaxIdleWorkerDuration,
MaxWorkCount: DefaultMaxWorkCount,
}
g.goChanPool.New = func() any {
return &goChan{task: make(chan *task, goChanCap)}
}
g.gsPool.New = func() any {
return newGoS()
}
return g
}
func newWorkerChan() *goChan {
return &goChan{
task: make(chan *task, 0),
}
}
func runGlobalTasks(t *task) {
gs := t.gs
defer gs.recover()
gs.Err(t.job())
}
func StartPool(opt ...PoolOpt) {
once.Do(func() {
initPath()
gp = newGoPool()
for i := range opt {
opt[i](gp)
}
gp.start()
})
}
func (p *goPool) start() {
allJob = make(chan *task, DefaultOverJobCount)
p.mu.Lock()
need := p.MinWorkCount - len(p.going)
if need > 0 {
p.going = make([]*goChan, 0, need)
for i := 0; i < need; i++ {
ch := p.goChanPool.Get().(*goChan)
go func() {
//执行任务
p.goFunc(ch)
}()
p.going = append(p.going, ch)
}
}
p.mu.Unlock()
//开启清扫任务
go func() {
var reuse []*goChan
for {
p.clean(&reuse)
select {
default:
time.Sleep(p.MaxIdleWorkerDuration)
}
}
}()
//go开启全局任务处理
go func() {
for t := range allJob {
runGlobalTasks(t)
}
}()
}
//获取资源并传递任务
func (p *goPool) Serve(j *task) {
ch := p.getCh()
if ch == nil {
allJob <- j
return
}
ch.task <- j
return
}
//获取资源
func (p *goPool) getCh() *goChan {
var ch *goChan
newGo := false
//尝试获取与正在闲置的goroutine的通信或者获取创建新routine的权限
p.mu.Lock()
going := p.going
last := len(going) - 1
if last < 0 {
if p.goCount < p.MaxWorkCount {
newGo = true
p.goCount++
}
} else {
ch = going[last]
going[last] = nil
p.going = going[:last]
}
p.mu.Unlock()
if ch == nil {
if !newGo {
return nil
}
ch = p.goChanPool.Get().(*goChan)
go func() {
//执行任务
p.goFunc(ch)
p.goChanPool.Put(ch)
}()
}
return ch
}
func (p *goPool) Going() int {
p.mu.Lock()
defer p.mu.Unlock()
return p.goCount
}
func (p *goPool) goFunc(ch *goChan) {
var (
t *task
)
defer func() {
p.mu.Lock()
p.goCount--
p.mu.Unlock()
}()
for t = range ch.task {
//用于清扫协程进行关闭通知
if t == nil {
break
}
help:
// 执行内容
runTask(t)
//尝试从全局中获取数据消费
if t = tryGetTask(); t != nil {
goto help
}
//记录最后一次通讯信息
p.release(ch)
t = nil
}
}
func runTask(t *task) {
gs := t.gs
defer gs.recover()
gs.Err(t.job())
}
func (p *goPool) release(ch *goChan) {
ch.lastUseTime = time.Now()
p.mu.Lock()
defer p.mu.Unlock()
p.going = append(p.going, ch)
}
//最大闲置清扫
func (p *goPool) clean(reuse *[]*goChan) {
maxIdleGoDuration := p.MaxIdleWorkerDuration
currentTime := time.Now().Add(-maxIdleGoDuration)
p.mu.Lock()
going := p.going
park := len(going)
//清除超过闲置时间未使用的通讯
l, r, mid := 0, park-1, 0
for l <= r {
mid = (l + r) / 2
if currentTime.After(p.going[mid].lastUseTime) {
l = mid + 1
} else {
r = mid - 1
}
}
i := r - p.MinWorkCount
if i < 0 {
p.mu.Unlock()
return
}
*reuse = append((*reuse)[:0], going[:i+1]...)
m := copy(going, going[i+1:])
for i = m; i < park; i++ {
going[i] = nil
}
p.going = going[:m]
p.mu.Unlock()
tmp := *reuse
for i := range tmp {
tmp[i].task <- nil
tmp[i] = nil
}
}