-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
226 lines (191 loc) · 4.4 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package worker
import (
"log"
"os"
"sync"
"time"
"context"
)
const (
DefaultWorkersCount = 10
)
var (
DefaultTTR time.Duration = 10 * time.Minute
)
// Pool represents a pool of workers connected to a queue.
type Pool struct {
queue Queue // input queue
count int // workers count
ttr time.Duration // Time to run.
middleware middleware
handlers []Handler
mux map[string]Factory
logger *log.Logger
}
// NewPool returns a new Pool instance.
func NewPool(opts ...func(*Pool)) *Pool {
pool := &Pool{
queue: NewMemoryQueue(),
count: DefaultWorkersCount,
ttr: DefaultTTR,
mux: map[string]Factory{},
logger: log.New(os.Stdout, "[worker] ", 0),
handlers: CommonStack(),
}
// Apply options.
for _, opt := range opts {
opt(pool)
}
// Init middleware stack.
pool.middleware = pool.build(pool.handlers)
return pool
}
// Add registers a new job factory.
func (p *Pool) Add(f Factory) error {
typ, err := StructType(f)
if err != nil {
return err
}
if _, ok := p.mux[typ]; ok {
return NewErrorFmt("factory %q exists already", typ)
}
p.mux[typ] = f
return nil
}
// Use appends a new middleware to current stack.
func (p *Pool) Use(h Handler) {
p.handlers = append(p.handlers, h)
p.middleware = p.build(p.handlers)
}
// build iterates over the handlers, it returns a
// list of middlewares with each item linked to
// the next one.
func (p *Pool) build(hs []Handler) middleware {
if len(hs) == 0 {
return p.last()
}
next := p.build(hs[1:])
return middleware{hs[0], &next}
}
// last builds and returns the last middleware, which will
// execute the job without calling next middleware.
func (p *Pool) last() middleware {
return middleware{
HandlerFunc(func(sw StatusWriter, fact string, args *Args, next JobRunner) {
if err := p.execute(fact, args); err != nil {
sw.Set(err)
}
}),
&middleware{},
}
}
// Run starts processing jobs from the queue.
func (p *Pool) Run(ctx context.Context) error {
var wg sync.WaitGroup
// Handle unix signals.
sig := trap()
ctx, cancel := context.WithCancel(ctx)
// Fan-out channel.
c := make(chan Message)
// Start workers.
wg.Add(p.count + 1)
for i := 0; i < p.count; i++ {
go func() {
defer wg.Done()
p.worker(ctx, c)
}()
}
// Start the master.
go func() {
defer wg.Done()
p.master(ctx, c)
}()
var err error
select {
case <-ctx.Done():
err = ctx.Err()
case <-sig:
p.logger.Println("Quit signal received ...")
cancel()
}
p.logger.Println("Stopping workers ...")
close(c)
wg.Wait()
p.logger.Println("Shutdown completed!")
return err
}
// master polls the input queue sending jobs to workers through a blocking channel.
func (p *Pool) master(ctx context.Context, c chan<- Message) {
qs := newQueueService(p.queue)
var r *response
for {
select {
case <-ctx.Done():
return
case r = <-qs.get():
}
if r.Err != nil {
if wkerr, ok := r.Err.(*Error); ok && wkerr.Temporary() {
continue
}
return
}
select {
case <-ctx.Done():
return
case c <- r.Msg:
}
}
}
// worker executes jobs from the in channel in a separate goroutine.
func (p *Pool) worker(ctx context.Context, in <-chan Message) {
for msg := range in {
status := NewStatusWriter()
done := make(chan struct{}, 1)
// Start the job in a separate goroutine.
go func() {
p.Exec(status, msg.Type(), msg.Args())
done <- struct{}{}
}()
ctx, cancel := context.WithTimeout(ctx, p.ttr)
defer cancel()
// Wait job completion.
select {
case <-ctx.Done():
if err := p.queue.Reject(msg); err != nil {
p.logger.Println("Reject failure:", msg, err)
}
return
case <-done:
if status.OK() {
if err := p.queue.Delete(msg); err != nil {
p.logger.Println("Delete failure:", msg, err)
}
} else {
if err := p.queue.Reject(msg); err != nil {
p.logger.Println("Reject failure:", msg, err)
}
}
}
}
}
// Exec runs the job passing it through the middleware stack.
func (p *Pool) Exec(sw StatusWriter, fact string, args *Args) {
p.middleware.Exec(sw, fact, args)
}
// execute runs the job without passing it through the
// middleware stack.
func (p *Pool) execute(fact string, args *Args) error {
f, ok := p.mux[fact]
if !ok {
return NewErrorFmt("bad type: %v", fact)
}
j, err := f.Make(args)
if err != nil {
return NewErrorFmt("make: %v", err)
}
if err := j.Run(); err != nil {
return NewErrorFmt("run: %v", err)
}
return nil
}