-
Notifications
You must be signed in to change notification settings - Fork 277
/
pool.go
311 lines (279 loc) · 7.58 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Licensed to LinDB under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. LinDB licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package concurrent
import (
"context"
"sync"
"time"
"go.uber.org/atomic"
"github.com/lindb/lindb/metrics"
errorpkg "github.com/lindb/lindb/pkg/error"
"github.com/lindb/lindb/pkg/logger"
)
//go:generate mockgen -source=./pool.go -destination=./pool_mock.go -package=concurrent
const (
// size of the queue that workers register their availability to the dispatcher.
readyWorkerQueueSize = 32
// size of the tasks queue
tasksCapacity = 8
// sleeps in this interval when there are no available workers
sleepInterval = time.Millisecond * 5
)
// Task represents a task function to be executed by a worker(goroutine).
type Task struct {
// handle executes task function.
handle func()
// panicHandle executes callback if task happens panic.
panicHandle func(err error)
createTime time.Time
}
// NewTask creates a task.
func NewTask(handle func(), panicHandle func(err error)) *Task {
return &Task{
handle: handle,
panicHandle: panicHandle,
createTime: time.Now(),
}
}
func (t *Task) Exec() {
t.handle()
}
// Pool represents the goroutine pool that executes submitted tasks.
type Pool interface {
// Submit enqueues a callable task for a worker to execute.
//
// Each submitted task is immediately given to a ready worker.
// If there are no available workers, the dispatcher starts a new worker,
// until the maximum number of workers are added.
//
// After the maximum number of workers are running, and no workers are ready,
// execute function will be blocked.
Submit(ctx context.Context, task *Task)
// Stopped returns true if this pool has been stopped.
Stopped() bool
// Stop stops all goroutines gracefully,
// all pending tasks will be finished before exit
Stop()
}
// workerPool is a pool for goroutines.
type workerPool struct {
name string
maxWorkers int
tasks chan *Task // tasks channel
readyWorkers chan *worker // available worker
idleTimeout time.Duration // idle goroutine recycle time
onDispatcherStopped chan struct{} // signal that dispatcher is stopped
stopped atomic.Bool // mark if the pool is closed or not
ctx context.Context
cancel context.CancelFunc
statistics *metrics.ConcurrentStatistics
logger *logger.Logger
}
// NewPool returns a new worker pool,
// maxWorkers parameter specifies the maximum number workers that will execute tasks concurrently.
func NewPool(name string, maxWorkers int, idleTimeout time.Duration, statistics *metrics.ConcurrentStatistics) Pool {
if maxWorkers < 1 {
maxWorkers = 1
}
if idleTimeout <= 0 {
idleTimeout = time.Second * 5
}
ctx, cancel := context.WithCancel(context.Background())
pool := &workerPool{
name: name,
maxWorkers: maxWorkers,
tasks: make(chan *Task, tasksCapacity),
readyWorkers: make(chan *worker, readyWorkerQueueSize),
idleTimeout: idleTimeout,
onDispatcherStopped: make(chan struct{}),
stopped: *atomic.NewBool(false),
ctx: ctx,
cancel: cancel,
statistics: statistics,
logger: logger.GetLogger("Pool", name),
}
go pool.dispatch()
return pool
}
func (p *workerPool) Submit(ctx context.Context, task *Task) {
if task.handle == nil || p.Stopped() {
return
}
select {
case <-ctx.Done():
p.statistics.TasksRejected.Incr()
return
case p.tasks <- task:
}
}
// mustGetWorker makes sure that a ready worker is return
func (p *workerPool) mustGetWorker() *worker {
var worker *worker
for {
select {
// got a worker
case worker = <-p.readyWorkers:
return worker
default:
if int(p.statistics.WorkersAlive.Get()) >= p.maxWorkers {
// no available workers
time.Sleep(sleepInterval)
continue
}
w := newWorker(p)
return w
}
}
}
func (p *workerPool) dispatch() {
defer func() {
p.onDispatcherStopped <- struct{}{}
}()
idleTimeoutTimer := time.NewTimer(p.idleTimeout)
defer idleTimeoutTimer.Stop()
var (
worker *worker
task *Task
)
for {
idleTimeoutTimer.Reset(p.idleTimeout)
select {
case <-p.ctx.Done():
return
case task = <-p.tasks:
worker = p.mustGetWorker()
worker.execute(task)
case <-idleTimeoutTimer.C:
p.idle()
}
}
}
func (p *workerPool) idle() {
// timed out waiting, kill a ready worker
if p.statistics.WorkersAlive.Get() > 0 {
select {
case worker := <-p.readyWorkers:
worker.stop(func() {})
case <-p.ctx.Done():
// pool is stopped
default:
// workers are busy now
}
}
}
func (p *workerPool) Stopped() bool {
return p.stopped.Load()
}
// stopWorkers stops all workers
func (p *workerPool) stopWorkers() {
var wg sync.WaitGroup
for p.statistics.WorkersAlive.Get() > 0 {
wg.Add(1)
worker := <-p.readyWorkers
worker.stop(func() {
wg.Done()
})
}
wg.Wait()
}
// consumedRemainingTasks consumes all buffered tasks in the channel
func (p *workerPool) consumedRemainingTasks() {
for {
select {
case task := <-p.tasks:
p.execTask(task)
default:
return
}
}
}
func (p *workerPool) execTask(task *Task) {
defer func() {
var err error
r := recover()
if r != nil {
p.statistics.TasksPanic.Incr()
err = errorpkg.Error(r)
p.logger.Error("panic when execute task",
logger.Error(err), logger.Stack())
if task.panicHandle != nil {
task.panicHandle(err)
}
}
}()
p.statistics.TasksWaitingTime.UpdateDuration(time.Since(task.createTime))
task.Exec()
p.statistics.TasksExecutingTime.UpdateDuration(time.Since(task.createTime))
p.statistics.TasksConsumed.Incr()
}
// Stop tells the dispatcher to exit with pending tasks done.
func (p *workerPool) Stop() {
if p.stopped.Swap(true) {
return
}
// close dispatcher
p.cancel()
// wait dispatcher's exit
<-p.onDispatcherStopped
// close all workers
p.stopWorkers()
// consume remaining tasks
p.consumedRemainingTasks()
}
// worker represents the worker that executes the task
type worker struct {
pool *workerPool
tasks chan *Task
stopCh chan struct{}
}
// newWorker creates the worker that executes tasks given by the dispatcher
// When a new worker starts, it registers itself on the createdWorkers channel.
func newWorker(pool *workerPool) *worker {
w := &worker{
pool: pool,
tasks: make(chan *Task),
stopCh: make(chan struct{}),
}
w.pool.statistics.WorkersAlive.Incr()
w.pool.statistics.WorkersCreated.Incr()
go w.process()
return w
}
// execute submits the task to queue
func (w *worker) execute(task *Task) {
w.tasks <- task
}
func (w *worker) stop(callable func()) {
defer callable()
w.stopCh <- struct{}{}
w.pool.statistics.WorkersKilled.Incr()
w.pool.statistics.WorkersAlive.Decr()
}
// process task from queue
func (w *worker) process() {
var task *Task
for {
select {
case <-w.stopCh:
return
case task = <-w.tasks:
w.pool.execTask(task)
// register worker-self to readyWorkers again
w.pool.readyWorkers <- w
}
}
}