-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkerpool.go
53 lines (48 loc) · 1.01 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
package jobq
import (
"sync"
)
// WorkerPool is a pool of workers that process Jobs from a JobQueue.
type WorkerPool struct {
queue *JobQueue
wg sync.WaitGroup
shutdown chan bool
}
// NewWorkerPool creates a new WorkerPool with the given JobQueue and number of workers
// and starts the workers.
func NewWorkerPool(queue *JobQueue, numWorkers int) *WorkerPool {
pool := &WorkerPool{
queue: queue,
shutdown: make(chan bool, 1),
}
pool.wg.Add(numWorkers)
for i := 0; i < numWorkers; i++ {
go pool.worker()
}
return pool
}
// worker is a single worker that processes Jobs from the JobQueue.
func (p *WorkerPool) worker() {
defer p.wg.Done()
for {
select {
case <-p.shutdown:
return
default:
job, err := p.queue.DequeueJob()
if err == ErrQueueClosed {
return
}
if err != nil {
continue
}
job.Run()
}
}
}
// Close closes the WorkerPool and the queue, and waits for all workers to finish.
func (p *WorkerPool) Close() {
close(p.shutdown)
p.queue.Close()
p.wg.Wait()
}