forked from src-d/borges
/
worker_pool.go
106 lines (94 loc) · 2.58 KB
/
worker_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
package borges
import (
"sync"
"github.com/inconshreveable/log15"
"gopkg.in/src-d/framework.v0/queue"
)
// A WorkerJob is a job to be passed to the worker. It contains the Job itself
// and an acknowledger that the worker uses to signal that it finished the job.
type WorkerJob struct {
*Job
queue.Acknowledger
}
// WorkerPool is a pool of workers that can process jobs.
type WorkerPool struct {
log log15.Logger
do func(log15.Logger, *Job) error
jobChannel chan *WorkerJob
workers []*Worker
wg *sync.WaitGroup
m *sync.Mutex
}
// NewWorkerPool creates a new empty worker pool. It takes a function to be used
// by workers to process jobs. The pool is started with no workers.
// SetWorkerCount must be called to start them.
func NewWorkerPool(log log15.Logger, f func(log15.Logger, *Job) error) *WorkerPool {
return &WorkerPool{
log: log,
do: f,
jobChannel: make(chan *WorkerJob),
workers: nil,
wg: &sync.WaitGroup{},
m: &sync.Mutex{},
}
}
// Do executes a job. It blocks until a worker is assigned to process the job
// and then it returns, with the worker processing the job asynchronously.
func (wp *WorkerPool) Do(j *WorkerJob) {
wp.jobChannel <- j
}
// SetWorkerCount changes the number of running workers. Workers will be started
// or stopped as necessary to satisfy the new worker count. It blocks until the
// all required workers are started or stopped. Each worker, if busy, will
// finish its current job before stopping.
func (wp *WorkerPool) SetWorkerCount(workers int) {
wp.m.Lock()
defer wp.m.Unlock()
n := workers - len(wp.workers)
if n > 0 {
wp.add(n)
} else if n < 0 {
wp.del(-n)
}
}
// Len returns the number of workers currently in the pool.
func (wp *WorkerPool) Len() int {
wp.m.Lock()
defer wp.m.Unlock()
return len(wp.workers)
}
func (wp *WorkerPool) add(n int) {
wp.wg.Add(n)
for i := 0; i < n; i++ {
log := wp.log.New("worker", i)
w := NewWorker(log, wp.do, wp.jobChannel)
go func() {
defer wp.wg.Done()
w.Start()
}()
wp.workers = append(wp.workers, w)
}
}
func (wp *WorkerPool) del(n int) {
prevWorkers := len(wp.workers)
wg := &sync.WaitGroup{}
for i := prevWorkers - 1; i >= prevWorkers-n; i-- {
wg.Add(1)
w := wp.workers[i]
wp.workers = wp.workers[:len(wp.workers)-1]
go func() {
w.Stop()
wg.Done()
}()
}
wg.Wait()
}
// Close stops all the workers in the pool and frees resources used by it.
// Workers are
// It blocks until it finishes.
func (wp *WorkerPool) Close() error {
wp.SetWorkerCount(0)
wp.wg.Wait()
close(wp.jobChannel)
return nil
}