-
Notifications
You must be signed in to change notification settings - Fork 2
/
cachepool.go
380 lines (326 loc) · 9.04 KB
/
cachepool.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package octopus
import (
"math"
"runtime"
"sync"
"sync/atomic"
"time"
)
type cachedWorker struct {
pool *WorkPool
jobChannel chan Future
stop chan bool
}
func newCacheWorker(cachedpool *WorkPool) *cachedWorker {
return &cachedWorker {
pool : cachedpool ,
jobChannel : make(chan Future) ,
stop : make(chan bool) ,
}
}
func (w *cachedWorker) getStopChannel() chan bool {
return w.stop
}
func (w *cachedWorker) getJobChannel() chan Future {
return w.jobChannel
}
func (w *cachedWorker) start() {
timer := time.NewTimer(w.pool.keepAliveTime)
go func() {
var job Future
for {
timer.Reset(w.pool.keepAliveTime)
w.pool.workerChannel <- w
select {
case job = <- w.jobChannel :
w.pool.activeCount = w.pool.activeCount + 1
w.pool.wg.Add(1)
job.execute()
w.pool.wg.Done()
w.pool.activeCount = w.pool.activeCount - 1
w.pool.completedJobCount = w.pool.completedJobCount + 1
// get job timeout
case <- timer.C :
if w.pool.poolSize > w.pool.minPoolSize {
w.pool.poolSize = w.pool.poolSize - 1
return
} else {
select {
case job = <- w.jobChannel :
w.pool.activeCount = w.pool.activeCount + 1
w.pool.wg.Add(1)
job.execute()
w.pool.wg.Done()
w.pool.activeCount = w.pool.activeCount - 1
w.pool.completedJobCount = w.pool.completedJobCount + 1
case stop := <- w.stop :
if stop {
w.pool.poolSize = w.pool.poolSize - 1
w.stop <- true
return
}
}
}
case stop := <- w.stop :
if stop {
w.pool.poolSize = w.pool.poolSize - 1
w.stop <- true
return
}
}
}
}()
}
type WorkPool struct {
workerChannel chan worker
jobChannel chan Future
stop chan bool
isPoolOpen uint32
isShutdownNow bool
canDropJob bool
wg *sync.WaitGroup
minPoolSize uint64
poolSize uint64
maxPoolSize uint64
keepAliveTime time.Duration
awaitWokerTime time.Duration
activeCount uint64
completedJobCount uint64
logFunc LogFunc
}
// Creates a goroutine pool that reuses a fixed number of goroutines.
func NewFixWorkerPool(workerNum uint64) (workpool *WorkPool, err error) {
workpool, err = NewBaseCachedWorkerPool(workerNum, workerNum, 60*time.Second, 1*time.Second)
return
}
// Creates a goroutine pool that creates new goroutines as needed, but will reuse previously constructed goroutines when they are available.
func NewCachedWorkerPool() (workpool *WorkPool, err error) {
workpool, err = NewBaseCachedWorkerPool(uint64(runtime.NumCPU()), math.MaxUint64, 60*time.Second, 1*time.Second)
return
}
// Creates a goroutine pool with MinPoolSize , MaxPoolSize, the KeepAliveTime of a worker, the time of manager await worker.
// Please note that the KeepAliveTime must be greater than one second.
func NewBaseCachedWorkerPool(MinPoolSize uint64, MaxPoolSize uint64, KeepAliveTime time.Duration, AwaitWokerTime time.Duration) (workpool *WorkPool, err error) {
if MaxPoolSize == 0 {
err = ErrInvalidArguments
return
}
if KeepAliveTime < 1*time.Second {
err = ErrKeepAliveTimeArguments
return
}
wc := make(chan worker, MinPoolSize)
jc := make(chan Future, MinPoolSize)
sc := make(chan bool)
pool := &WorkPool {
workerChannel : wc ,
jobChannel : jc ,
stop : sc,
isPoolOpen : poolOpen ,
isShutdownNow : false ,
canDropJob : false ,
wg : &sync.WaitGroup{},
minPoolSize : MinPoolSize,
poolSize : MinPoolSize,
maxPoolSize : MaxPoolSize,
activeCount : 0,
keepAliveTime : KeepAliveTime,
awaitWokerTime : AwaitWokerTime,
completedJobCount : 0,
}
for i := 0; i < int(MinPoolSize); i++ {
w := newCacheWorker(pool)
w.start()
}
go pool.manager()
workpool = pool
return
}
func (pool *WorkPool) manager() {
pool.log("WorkPool Manager : started")
timer := time.NewTimer(pool.awaitWokerTime)
for {
timer.Reset(pool.awaitWokerTime)
select {
case w := <- pool.workerChannel :
select {
case job := <- pool.jobChannel :
w.getJobChannel() <- job
default:
pool.workerChannel <- w
}
// if await worker timeout
case <-timer.C :
if !pool.canDropJob {
if pool.GetPoolSize() < pool.GetMaxPoolSize() {
pool.log("WorkPool Manager : add routine")
worker := newCacheWorker(pool)
worker.start()
pool.poolSize = pool.poolSize + 1
}
} else {
select {
case <- pool.jobChannel :
pool.log("WorkPool Manager : drop job")
default:
}
}
case stop := <- pool.stop :
if stop {
pool.log("WorkPool Manager : stop pool begin")
close(pool.jobChannel)
if len(pool.jobChannel) > 0 {
for j := range pool.jobChannel {
w := <- pool.workerChannel
w.getJobChannel() <- j
}
}
if pool.isShutdownNow {
// wait for all job done
pool.wg.Wait()
}
// close all worker
for pool.poolSize != 0 {
worker := <- pool.workerChannel
worker.getStopChannel() <- true
<-worker.getStopChannel()
}
pool.stop <- true
pool.log("WorkPool Manager : stop pool end")
return
}
}
}
}
// Set drop job if await worker timeout, it will drop jobs when manager appears awaitWorkerTime timeout .
func (pool *WorkPool) SetDropJob(ok bool) {
pool.canDropJob = ok
}
// CanDropJob will return if the manager will drop jobs when pool is busy.
func (pool *WorkPool) CanDropJob() bool {
return pool.canDropJob
}
func (pool *WorkPool) log(msg string) {
if pool.logFunc != nil {
pool.logFunc(msg)
}
}
// Return approximate total number of goroutines in pool.
func (pool *WorkPool) GetPoolSize() uint64 {
return pool.poolSize
}
// Return the approximate total number of woker executing a job in pool.
func (pool *WorkPool) GetActiveCount() uint64 {
return pool.activeCount
}
// Return the approximate total number of jobs that have completed execution.
func (pool *WorkPool) GetCompletedJobCount() uint64 {
return pool.completedJobCount
}
// if pool is close it will return true.
func (pool *WorkPool) IsShutDown() bool {
return (atomic.LoadUint32(&pool.isPoolOpen) == poolClose)
}
// Set a log function to record log infos.
func (pool *WorkPool) SetLogFunc(function LogFunc) {
pool.logFunc = function
}
// Set the minimum number of goroutines.
func (pool *WorkPool) SetMinPoolSize(minPoolSize uint64) {
pool.minPoolSize = minPoolSize
}
// Return the minimum number of goroutines.
func (pool *WorkPool) GetMinPoolSize() uint64 {
return pool.minPoolSize
}
// Set the maximum allowed number of goroutines.
func (pool *WorkPool) SetMaxPoolSize(maxPoolSize uint64) {
pool.maxPoolSize = maxPoolSize
}
// Return the maximum allowed number of goroutines.
func (pool *WorkPool) GetMaxPoolSize() uint64 {
return pool.maxPoolSize
}
// Return the KeepAliveTime of a worker.
func (pool *WorkPool) GetKeepAliveTime() time.Duration {
return pool.keepAliveTime
}
// Set the KeepAliveTime of a worker. Please note that it must be greater than one second.
func (pool *WorkPool) SetKeepAliveTime(keepAliveTime time.Duration) error {
if keepAliveTime < 1*time.Second {
return ErrKeepAliveTimeArguments
}
pool.keepAliveTime = keepAliveTime
return nil
}
// Return the awaitWorkerTime of pool manager.
func (pool *WorkPool) GetAwaitWorkerTime() time.Duration {
return pool.awaitWokerTime
}
// Submit a Runnable job for execution and return a Future representing that job.
func (pool *WorkPool) SubmitRunnable(job Runnable) (future Future, err error) {
if pool.IsShutDown() {
err = ErrPoolShutdown
return
}
future = newRunnableFuture(pool, job)
pool.jobChannel <- future
return
}
// Submit a Callable job for execution and return a Future representing that job.
func (pool *WorkPool) SubmitCallable(job Callable) (future Future, err error) {
if pool.IsShutDown() {
err = ErrPoolShutdown
return
}
future = newCallableFuture(pool, job)
pool.jobChannel <- future
return
}
// Submit Runnable jobs for execution and return Futures representing those jobs.
func (pool *WorkPool) InvokeAllRunnable(jobs []Runnable) (futures []Future,err error) {
if pool.IsShutDown() {
err = ErrPoolShutdown
return
}
futures = make([]Future,len(jobs))
for i,j := range jobs {
f , _ := pool.SubmitRunnable(j)
futures[i] = f
}
return
}
// Submit Callable jobs for execution and return Futures representing those jobs.
func (pool *WorkPool) InvokeAllCallable(jobs []Callable) (futures []Future,err error) {
if pool.IsShutDown() {
err = ErrPoolShutdown
return
}
futures = make([]Future,len(jobs))
for i,j := range jobs {
f , _ := pool.SubmitCallable(j)
futures[i] = f
}
return
}
// Close the pool and wait for all goroutines done, it may be block.
func (pool *WorkPool) Shutdown() {
if pool.IsShutDown() {
return
}
atomic.SwapUint32(&pool.isPoolOpen, poolClose)
pool.isShutdownNow = false
pool.stop <- true
<- pool.stop
close(pool.stop)
}
// Close the pool but will not wait for all goroutines done, it will be never block.
func (pool *WorkPool) ShutdownNow() {
if pool.IsShutDown() {
return
}
atomic.SwapUint32(&pool.isPoolOpen, poolClose)
pool.isShutdownNow = true
pool.stop <- true
close(pool.stop)
}