forked from rfyiamcool/gpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpool.go
282 lines (230 loc) Β· 4.66 KB
/
gpool.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
package gpool
import (
"context"
"errors"
"sync"
"time"
)
const (
DefaultGPoolMaxWorker = 50
DefaultIdleTimeout = 180 * time.Second
DefaultDispatchPeriod = 200 * time.Millisecond
bucketSize = 5
)
var (
ErrJobChanFull = errors.New("gpool job chan is full")
ErrInvalidValue = errors.New("invalid value")
GlobalGoPool *GoPool
)
type jobFunc func()
type taskModel struct {
call jobFunc
res chan bool
}
type Options struct {
// number of maxworkers,
MaxWorker int
MinWorker int
// size of job queue
JobBuffer int
IdleTimeout time.Duration
DispatchPeriod time.Duration
}
type GoPool struct {
sync.Mutex
ctx context.Context
ctxCancel context.CancelFunc
isClose bool
// opt *gpOptions
maxWorker int
idleTimeout time.Duration
dispatchPeriod time.Duration
// inside
curWorker int
// TODO: Feature
minWorker int
// idleWorker int
// runningWorker int
jobChan chan taskModel
jobBuffer int
killChan chan bool
call jobFunc
}
func NewGPool(op *Options) (*GoPool, error) {
pool := GoPool{}
// cp option
pool.maxWorker = op.MaxWorker
pool.minWorker = op.MinWorker
pool.jobBuffer = op.JobBuffer
pool.idleTimeout = op.IdleTimeout
pool.dispatchPeriod = op.DispatchPeriod
// reset default var
if pool.maxWorker <= 0 {
pool.maxWorker = DefaultGPoolMaxWorker
}
// too slow
if pool.idleTimeout < time.Second {
pool.idleTimeout = DefaultIdleTimeout
}
if pool.dispatchPeriod < time.Millisecond || pool.dispatchPeriod > time.Second {
pool.dispatchPeriod = DefaultDispatchPeriod
}
if pool.minWorker < 1 {
pool.minWorker = pool.maxWorker / 5
}
if pool.jobBuffer <= 0 {
// jobBuffer must > 0, dispatch add goworker with the option
pool.jobBuffer = pool.maxWorker
}
// err
if pool.maxWorker < pool.minWorker {
return &pool, errors.New("maxWorker must > minWorker")
}
// init signal
pool.jobChan = make(chan taskModel, pool.jobBuffer)
pool.killChan = make(chan bool, 0)
// inside var
pool.isClose = false
ctx, cancel := context.WithCancel(context.Background())
pool.ctx = ctx
pool.ctxCancel = cancel
// start
// go pool.dispatchRun(pool.dispatchPeriod)
pool.spawnWorker(pool.maxWorker)
return &pool, nil
}
// TODO: scan workers' timer in heap, kill worker
func (p *GoPool) dispatchRun(d time.Duration) {
ticker := time.NewTicker(d)
for {
select {
case <-ticker.C:
if len(p.jobChan) == 0 {
continue
}
// don't need lock
if p.maxWorker == p.curWorker {
continue
}
// start some workers at a time
p.spawnWorker(p.maxWorker / bucketSize)
case <-p.ctx.Done():
ticker.Stop()
return
}
}
}
func (p *GoPool) validator() error {
if p.maxWorker <= 0 {
return ErrInvalidValue
}
if p.idleTimeout.Seconds() <= 0 {
return ErrInvalidValue
}
return nil
}
func (p *GoPool) spawnWorker(num int) {
// two check, reduce lock syscall
if p.curWorker == p.maxWorker {
return
}
p.Lock()
defer p.Unlock()
for i := 0; i < num; i++ {
if p.curWorker >= p.maxWorker {
return
}
go p.handle()
p.curWorker++
}
}
func (p *GoPool) ResizeMaxWorker(maxWorker int) error {
if maxWorker <= 0 || maxWorker < p.minWorker {
return ErrInvalidValue
}
p.maxWorker = maxWorker
return nil
}
func (p *GoPool) trySpawnWorker() {
if len(p.jobChan) > 0 && p.maxWorker > p.curWorker {
p.spawnWorker(p.maxWorker / bucketSize)
}
}
// after push job queue, retrun direct
func (p *GoPool) ProcessAsync(f jobFunc) error {
task := taskModel{
call: f,
res: nil,
}
p.trySpawnWorker()
select {
case p.jobChan <- task:
return nil
}
}
// wait jobFunc finish
func (p *GoPool) ProcessSync(f jobFunc) error {
task := taskModel{
call: f,
res: make(chan bool, 1),
}
p.trySpawnWorker()
select {
case p.jobChan <- task:
<-task.res
return nil
}
}
func (p *GoPool) handle() {
timer := time.NewTimer(p.idleTimeout)
for {
select {
case job := <-p.jobChan:
job.call()
if job.res != nil {
job.res <- true
}
timer.Reset(p.idleTimeout)
case <-timer.C:
// for a long time without a task, worker exit
err := p.workerExit(timer)
if err != nil {
continue
}
return
case <-p.killChan:
// recv signal {exit} by dispatch; judge ; then return
err := p.workerExit(timer)
if err != nil {
continue
}
return
}
}
}
func (p *GoPool) workerExit(timer *time.Timer) error {
p.Lock()
defer p.Unlock()
if p.curWorker <= p.minWorker {
timer.Reset(p.idleTimeout)
return errors.New("don't <= minWorker")
}
p.curWorker--
timer.Stop()
return nil
}
func (p *GoPool) Close() {
p.Lock()
// double check
if p.isClose {
p.Unlock()
return
}
p.isClose = true
p.ctxCancel()
p.Unlock()
}
func (p *GoPool) Stats() {
}
func (p *GoPool) Wait() {
}