/
workerpool.go
59 lines (50 loc) · 904 Bytes
/
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
package netutil
import (
"context"
"sync"
)
type WorkerPool struct {
Fn func(ctx context.Context)
mu sync.Mutex
workers []worker
}
func (wp *WorkerPool) SetCount(count int) {
wp.mu.Lock()
defer wp.mu.Unlock()
for len(wp.workers) != count {
if len(wp.workers) < count {
wp.spawn()
} else {
wp.despawn()
}
}
}
func (wp *WorkerPool) Stop() {
wp.SetCount(0)
}
func (wp *WorkerPool) spawn() {
ctx, cf := context.WithCancel(context.Background())
w := worker{
ctx: ctx,
cf: cf,
done: make(chan struct{}),
}
wp.workers = append(wp.workers, w)
go w.run(wp.Fn)
}
func (wp *WorkerPool) despawn() {
l := len(wp.workers)
w := wp.workers[l-1]
wp.workers = wp.workers[:l-1]
w.cf()
<-w.done
}
type worker struct {
ctx context.Context
cf context.CancelFunc
done chan struct{}
}
func (w worker) run(fn func(context.Context)) {
defer close(w.done)
fn(w.ctx)
}