-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkerpool.go
249 lines (196 loc) · 4 KB
/
workerpool.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
package workerpool
import (
"sync"
"time"
)
// WorkerPool can manage a collection of workers, each with their own goroutine.
type WorkerPool interface {
// Spawn returns true if total of workers < size, else false.
Spawn(Fn, Argv) bool
// WaitSpawn will spawn, and block when pool is full.
WaitSpawn(Fn, Argv)
Start()
Stop()
}
// New creates a new WorkerPool
func New(size int, maxIdleWorkerDuration time.Duration) WorkerPool {
wp := &implWorkerPool{
workers: new(workerlist),
maxSize: size,
maxIdleWorkerDuration: maxIdleWorkerDuration,
}
wp.cond = sync.NewCond(&wp.lock)
return wp
}
type implWorkerPool struct {
lock sync.Mutex
cond *sync.Cond
workerCount int
maxSize int
mustStop bool
vworkerPool sync.Pool
workers *workerlist
maxIdleWorkerDuration time.Duration
stopCh chan struct{}
}
func (pool *implWorkerPool) Spawn(f Fn, a Argv) bool {
w := pool.getWorker()
if w == nil {
return false
}
w.taskCh <- task{f, a}
return true
}
func (pool *implWorkerPool) WaitSpawn(f Fn, a Argv) {
w := pool.waitWorker()
w.taskCh <- task{f, a}
}
func (pool *implWorkerPool) Start() {
if pool.stopCh != nil {
panic("BUG: workerPool already started")
}
pool.stopCh = make(chan struct{})
stopCh := pool.stopCh
go func() {
for {
pool.clean()
select {
case <-stopCh:
return
default:
time.Sleep(pool.maxIdleWorkerDuration)
}
}
}()
}
func (pool *implWorkerPool) Stop() {
if pool.stopCh == nil {
panic("BUG: workerPool wasn't started")
}
close(pool.stopCh)
pool.stopCh = nil
pool.lock.Lock()
var ele *element
for ele = pool.workers.Front(); ele != nil; ele = ele.next {
ele.value.taskCh <- task{fn: nil}
}
pool.workers.ResetFront(nil)
pool.mustStop = true
pool.lock.Unlock()
}
func (pool *implWorkerPool) getWorker() *worker {
createWorker := false
var w *worker
ok := false
pool.lock.Lock()
w, ok = pool.workers.PopBack()
if !ok {
if pool.workerCount < pool.maxSize {
createWorker = true
pool.workerCount++
}
}
pool.lock.Unlock()
if w == nil {
if !createWorker {
return nil
}
w = pool.createWorker()
}
return w
}
func (pool *implWorkerPool) waitWorker() *worker {
var w *worker
ok := false
pool.lock.Lock()
w, ok = pool.workers.PopBack()
for !ok && pool.workerCount >= pool.maxSize {
pool.cond.Wait()
w, ok = pool.workers.PopBack()
}
if !ok {
pool.workerCount++
}
pool.lock.Unlock()
if !ok {
w = pool.createWorker()
}
return w
}
func (pool *implWorkerPool) createWorker() *worker {
var w *worker
vworker := pool.vworkerPool.Get()
if vworker == nil {
vworker = &worker{
taskCh: make(chan task),
}
}
w = vworker.(*worker)
go func() {
pool.workerFunc(w)
pool.vworkerPool.Put(w)
}()
return w
}
func (pool *implWorkerPool) clean() {
currentTime := time.Now()
maxIdleWorkerDuration := pool.maxIdleWorkerDuration
clean := false
pool.lock.Lock()
var ele *element
first := pool.workers.Front()
for ele = first; ele != nil; ele = ele.next {
w := ele.value
if currentTime.Sub(w.lastUseTime) < maxIdleWorkerDuration {
break
}
clean = true
}
pool.workers.ResetFront(ele)
pool.lock.Unlock()
if !clean {
return
}
for ele = first; ele != nil; ele = ele.next {
ele.value.taskCh <- task{fn: nil}
}
}
func (pool *implWorkerPool) release(w *worker) bool {
w.lastUseTime = time.Now()
pool.lock.Lock()
if pool.mustStop {
pool.lock.Unlock()
return false
}
pool.workers.PushBack(w)
pool.lock.Unlock()
pool.cond.Signal()
return true
}
func (pool *implWorkerPool) workerFunc(w *worker) {
for t := range w.taskCh {
if t.fn == nil {
break
}
t.fn(t.argv)
if !pool.release(w) {
break
}
}
pool.lock.Lock()
pool.workerCount--
pool.lock.Unlock()
pool.cond.Signal()
}
// Argv is arguments value
type Argv interface{}
// Fn is a function
type Fn func(argv Argv)
type task struct {
fn Fn
argv Argv
}
type worker struct {
taskCh chan task
lastUseTime time.Time
}