-
Notifications
You must be signed in to change notification settings - Fork 8
/
pool.go
296 lines (247 loc) · 6.56 KB
/
pool.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
package goroutines
import (
"context"
"errors"
"sync"
"time"
)
var (
// ErrPoolRelease indicates the pool is released and closed.
ErrPoolRelease = errors.New("pool released")
// ErrScheduleTimeout indicates there is no resource to handle this task within specified period.
ErrScheduleTimeout = errors.New("schedule timeout")
)
// TaskFunc is the task function assigned by caller, running in the goroutine pool
type TaskFunc func()
// Pool is the struct handling the interacetion with asynchronous goroutines
type Pool struct {
initN int
totalN int
scalable bool
taskQueueChan chan TaskFunc
workerChan chan *worker
wg sync.WaitGroup
stopOnce sync.Once
stopChan chan struct{}
workerPool sync.Pool
workers []*worker
workerMut sync.Mutex
workerCond *sync.Cond
metric Metric
}
// NewPool creates an instance of asynchronously goroutine pool
// with the given size which indicates total numbers of workers.
func NewPool(size int, options ...PoolOption) *Pool {
// load options
o := loadPoolOption(options...)
if size <= 0 {
panic(errors.New("the total number of workers must be greater than zero"))
}
if o.preAllocWorkers == defaultPreAllocWorkers {
o.preAllocWorkers = size
}
if o.preAllocWorkers > size {
panic(errors.New("the number of pre-allocated workers must be less than or equal to total"))
}
p := Pool{
initN: o.preAllocWorkers,
totalN: size,
scalable: o.preAllocWorkers != size, // needs helper goroutines (Miner/Recycler) to adjust the worker size
taskQueueChan: make(chan TaskFunc, o.taskQueueLength),
workerChan: make(chan *worker),
stopChan: make(chan struct{}),
metric: newMetric(),
}
p.workerPool.New = func() interface{} {
return newWorker(p.taskQueueChan, &p.workerPool, p.metric)
}
// pre-allocate workers
p.adjustWorkerSize(p.initN, false)
p.workerCond = sync.NewCond(&p.workerMut)
// init helper goroutines (Miner/Recycler) if necessary
if p.scalable {
p.startMiner()
if o.workerAdjustPeriod != defaultAdjustPeriod {
p.startRecycler(o.workerAdjustPeriod)
}
}
return &p
}
// Schedule schedules the task executed by worker (goroutines) in the Pool.
// It will be blocked until the works accepting the request.
func (p *Pool) Schedule(task TaskFunc) error {
return p.schedule(context.Background(), task)
}
// ScheduleWithTimeout schedules the task executed by worker (goroutines)
// in the Pool within the specified period. Or return ErrScheduleTimeout.
func (p *Pool) ScheduleWithTimeout(timeout time.Duration, task TaskFunc) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return p.schedule(ctx, task)
}
// ScheduleWithContext schedules the task executed by worker (goroutines)
// in the Pool. It will be blocked until works accepting the request, or
// return ErrScheduleTimeout because ctx is done (timeout or cancellation).
func (p *Pool) ScheduleWithContext(ctx context.Context, task TaskFunc) error {
return p.schedule(ctx, task)
}
// Release will terminate all workers, and force them finishing
// what they are working on ASAP.
func (p *Pool) Release() {
// only allow calling Release() once
p.stopOnce.Do(func() {
close(p.stopChan)
// trigger Minor waking up again
p.workerCond.Broadcast()
// wait for helper goroutines (Minor / Recycler) to stop
p.wg.Wait()
// wait for goroutines in Pool to stop
p.adjustWorkerSize(0, true)
})
}
// Workers returns the numbers of workers created.
func (p *Pool) Workers() int {
p.workerMut.Lock()
defer p.workerMut.Unlock()
return len(p.workers)
}
// Running returns the number of workers running for tasks.
func (p *Pool) Running() int {
return int(p.metric.BusyWorkers())
}
// force stands for forcing adjusting the size, only true in Release()
func (p *Pool) adjustWorkerSize(n int, force bool) {
p.workerMut.Lock()
defer p.workerMut.Unlock()
workerN := len(p.workers)
if n == workerN {
return
} else if n > p.totalN {
n = p.totalN
} else if n == 0 && !force && workerN > 0 && p.workers[0].getState() == wStatInit {
// keep Minor alive when adjustWorkerSize() is triggered by Recycler
n = 1
}
for i := workerN; i < n; i++ {
w := p.workerPool.Get().(*worker)
p.workers = append(p.workers, w)
w.run(func() {})
}
for i := n; i < workerN; i++ {
p.workers[i].stop()
}
for i := n; i < workerN; i++ {
p.workers[i].join()
// prevent it from memory leak
p.workers[i] = nil
}
p.workers = p.workers[:n]
}
func (p *Pool) allocWorker() *worker {
p.workerMut.Lock()
defer p.workerMut.Unlock()
// Check availability
for {
select {
case <-p.stopChan:
return nil
default:
}
if len(p.workers) < p.totalN {
break
}
// wait for recycler reducing workers
p.workerCond.Wait()
}
w := p.workerPool.Get().(*worker)
// prepend to p.workers
p.workers = append(p.workers, nil)
copy(p.workers[1:], p.workers)
p.workers[0] = w
return w
}
func (p *Pool) schedule(ctx context.Context, task TaskFunc) error {
select {
case <-p.stopChan:
return ErrPoolRelease
default:
}
// queue first strategy
select {
case <-p.stopChan:
return ErrPoolRelease
case <-ctx.Done():
// timeout or cancellation
return ErrScheduleTimeout
case p.taskQueueChan <- task:
return nil
default:
}
select {
case <-p.stopChan:
return ErrPoolRelease
case <-ctx.Done():
// timeout or cancellation
return ErrScheduleTimeout
case p.taskQueueChan <- task:
return nil
case w := <-p.workerChan:
select {
case <-p.stopChan:
return ErrPoolRelease
default:
w.run(task)
return nil
}
}
}
func (p *Pool) startMiner() {
p.wg.Add(1)
go func() {
defer p.wg.Done()
for {
w := p.allocWorker()
if w == nil {
// stopChan is closed
return
}
select {
case p.workerChan <- w:
// retrieve worker, and send to those in need
case <-p.stopChan:
return
}
}
}()
}
func max(values []uint64) (max uint64) {
for i, v := range values {
if i == 0 || v > max {
max = v
}
}
return max
}
func (p *Pool) startRecycler(period time.Duration) {
p.wg.Add(1)
ticker := time.NewTicker(period)
windows := []uint64{0, 0, 0}
go func() {
defer p.wg.Done()
for {
select {
case <-ticker.C:
// append to windows with the latest number of busy workers
copy(windows, windows[1:])
windows[len(windows)-1] = p.metric.BusyWorkers()
// shrink the number of workers based on history, and keep at least one alive for Minor
p.adjustWorkerSize(int(max(windows)), false)
// trigger Minor waking up again
p.workerCond.Broadcast()
case <-p.stopChan:
ticker.Stop()
return
}
}
}()
}