-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
98 lines (86 loc) · 1.87 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
package hypercache
import (
"sync"
)
// JobFunc is a function that can be enqueued in a worker pool.
type JobFunc func() error
// WorkerPool is a pool of workers that can execute jobs concurrently.
type WorkerPool struct {
workers int
jobs chan JobFunc
wg sync.WaitGroup
quit chan struct{}
errorChan chan error
}
// NewWorkerPool creates a new worker pool with the given number of workers.
func NewWorkerPool(workers int) *WorkerPool {
pool := &WorkerPool{
workers: workers,
jobs: make(chan JobFunc, workers),
quit: make(chan struct{}),
errorChan: make(chan error, workers),
}
pool.start()
return pool
}
// start starts the worker pool.
func (pool *WorkerPool) start() {
for i := 0; i < pool.workers; i++ {
go func() {
for {
select {
case job := <-pool.jobs:
err := job()
if err != nil {
pool.errorChan <- err
}
pool.wg.Done()
case <-pool.quit:
return
}
}
}()
}
}
// Enqueue adds a job to the worker pool.
func (pool *WorkerPool) Enqueue(job JobFunc) {
pool.wg.Add(1)
pool.jobs <- job
}
// Shutdown shuts down the worker pool. It waits for all jobs to finish.
func (pool *WorkerPool) Shutdown() {
close(pool.quit)
pool.wg.Wait()
close(pool.jobs)
close(pool.errorChan)
}
// Errors returns a channel that can be used to receive errors from the worker pool.
func (pool *WorkerPool) Errors() <-chan error {
return pool.errorChan
}
// Resize resizes the worker pool.
func (pool *WorkerPool) Resize(newSize int) {
if newSize < 0 {
return
}
diff := newSize - pool.workers
if diff == 0 {
return
}
pool.workers = newSize
if diff > 0 {
// Increase the number of workers
for i := 0; i < diff; i++ {
go func() {
for job := range pool.jobs {
job()
}
}()
}
} else {
// Decrease the number of workers
for i := 0; i < -diff; i++ {
pool.quit <- struct{}{}
}
}
}