/
workerpool.go
202 lines (149 loc) · 3.87 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
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
package workerpool
import (
"context"
"golang.org/x/sync/semaphore"
"sync"
)
// TaskOption allows for customization of task behavior
type TaskOption func(j *job)
// Retry will cause a Task to be invoked again if an error is returned. By default, a Task will be retried indefinitely
// until either the Task terminates successfully or the WorkerPool is stopped by context cancellation. To place limits
// on this behavior, see the RetryMax(n int) TaskOption
func Retry(b bool) TaskOption {
return func(j *job) {
j.retry = b
}
}
// RetryMax will cause a Task to be invoked again error is returned, with a limit of `n` total retries
func RetryMax(n int) TaskOption {
return func(j *job) {
j.retry = true
j.retryMax = n
}
}
// Task represents a unit of work for the WorkerPool
type Task interface {
Invoke(ctx context.Context) error
}
// job describes a task to be run by the worker pool, and stores details of its execution parameters
type job struct {
t Task
retry bool
retryMax int
}
// New is a constructor function for WorkerPool. `n` specifies the number of parallel workers
func New(n int64) WorkerPool {
return WorkerPool{
mu: &sync.Mutex{},
wg: &sync.WaitGroup{},
sem: semaphore.NewWeighted(n),
jobs: &[]*job{},
next: make(chan *job, 1),
notify: make(chan struct{}),
}
}
// WorkerPool is a concurrent worker pool implementation based on a semaphore
type WorkerPool struct {
mu *sync.Mutex
wg *sync.WaitGroup
sem *semaphore.Weighted
jobs *[]*job
next chan *job
notify chan struct{}
}
// work runs a job according to its specified parameters and will notify the WaitGroup and release the semaphore lock
// when completed
func (p *WorkerPool) work(ctx context.Context, j *job) {
retries := 0
for {
err := j.t.Invoke(ctx)
if err != nil {
if ctx.Err() != nil {
break
}
if j.retry && (j.retryMax == 0 || retries < j.retryMax) {
retries++
continue
}
}
break
}
p.wg.Done()
p.sem.Release(1)
}
// pop is a non-blocking read from the front of the queue
func (p *WorkerPool) pop() *job {
p.mu.Lock()
defer p.mu.Unlock()
// grab next job, if any
select {
case j := <-p.next:
// reload p.next, if possible
if len(*p.jobs) > 0 {
select {
case p.next <- (*p.jobs)[0]:
*(p.jobs) = (*p.jobs)[1:]
default:
}
}
return j
default:
return nil
}
}
// Push adds a Task to the queue with the provided options
func (p *WorkerPool) Push(t Task, opts ...TaskOption) {
p.mu.Lock()
defer p.mu.Unlock()
p.wg.Add(1)
// build the job descriptor for Task t
j := &job{t: t}
for _, opt := range opts {
opt(j)
}
// add to queue
*(p.jobs) = append(*p.jobs, j)
// reload p.next, if needed
select {
case p.next <- (*p.jobs)[0]:
*(p.jobs) = (*p.jobs)[1:]
default:
}
// notify that new jobs are available
select {
case p.notify <- struct{}{}:
default:
}
}
// Run the WorkerPool. To stop processing, cancel the context
func (p *WorkerPool) Run(ctx context.Context) {
for {
// iterate through jobs in the pool until none are left
for j := p.pop(); j != nil; j = p.pop() {
// acquire a semaphore lock. Using context.Background() here rather than ctx because we must still cycle
// through remaining jobs on the queue once ctx is cancelled.
_ = p.sem.Acquire(context.Background(), 1)
// if context is cancelled, mark job as done and release semaphore.
// this effectively skips the job without running it
if ctx.Err() != nil {
p.wg.Done()
p.sem.Release(1)
// continuing here allows us to purge the remaining jobs from the queue
continue
}
// run job
go p.work(ctx, j)
}
// blocks until new jobs arrive or the context is cancelled
select {
case <-p.notify:
continue
case <-ctx.Done():
return
}
}
}
// Wait blocks until each Task in the queue completes, or the context passed to Run is cancelled
func (p WorkerPool) Wait() {
p.wg.Wait()
}