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
|
package roadrunner
import (
"fmt"
"github.com/pkg/errors"
"log"
"os/exec"
"sync"
"time"
)
const (
// StopRequest can be sent by worker to indicate that restart is required.
StopRequest = "{\"stop\": true}"
)
// Pool controls worker creation, destruction and task routing.
type Pool struct {
// pool behaviour
cfg Config
// worker command creator
cmd func() *exec.Cmd
// creates and connects to workers
factory Factory
// active task executions
tasks sync.WaitGroup
// workers circular allocation buffer
free chan *Worker
// protects state of worker list, does not affect allocation
muw sync.RWMutex
// all registered workers
workers []*Worker
}
// NewPool creates new worker pool and task multiplexer. Pool will initiate with one worker.
func NewPool(cmd func() *exec.Cmd, factory Factory, cfg Config) (*Pool, error) {
if err := cfg.Valid(); err != nil {
return nil, errors.Wrap(err, "config error")
}
p := &Pool{
cfg: cfg,
cmd: cmd,
factory: factory,
workers: make([]*Worker, 0, cfg.NumWorkers),
free: make(chan *Worker, cfg.NumWorkers),
}
//todo: watch for error from workers!!!
// constant number of workers simplify logic
for i := uint64(0); i < p.cfg.NumWorkers; i++ {
// to test if worker ready
w, err := p.createWorker()
if err != nil {
p.Destroy()
return nil, err
}
// worker watcher
go func(w *Worker) {
if err := w.Wait(); err != nil {
// todo: register error
log.Println(err)
//todo: automatic replace
}
}(w)
p.free <- w
}
return p, nil
}
// Config returns associated pool configuration. Immutable.
func (p *Pool) Config() Config {
return p.cfg
}
// Workers returns worker list associated with the pool.
func (p *Pool) Workers() (workers []*Worker) {
p.muw.RLock()
defer p.muw.RUnlock()
for _, w := range p.workers {
workers = append(workers, w)
}
return workers
}
// Exec one task with given payload and context, returns result or error.
func (p *Pool) Exec(rqs *Payload) (rsp *Payload, err error) {
p.tasks.Add(1)
defer p.tasks.Done()
w, err := p.allocateWorker()
if err != nil {
return nil, errors.Wrap(err, "unable to allocate worker")
}
rsp, err = w.Exec(rqs)
if err != nil {
// soft job errors are allowed
if _, jobError := err.(JobError); jobError {
p.free <- w
return nil, err
}
go p.replaceWorker(w, err)
return nil, err
}
// worker want's to be terminated
if rsp.Body == nil && rsp.Head != nil && string(rsp.Head) == StopRequest {
go p.replaceWorker(w, err)
return p.Exec(rqs)
}
if p.cfg.MaxExecutions != 0 && w.State().NumExecs() >= p.cfg.MaxExecutions {
go p.replaceWorker(w, p.cfg.MaxExecutions)
} else {
p.free <- w
}
return rsp, nil
}
// Destroy all underlying workers (but let them to complete the task).
func (p *Pool) Destroy() {
p.tasks.Wait()
var wg sync.WaitGroup
for _, w := range p.Workers() {
wg.Add(1)
go func(w *Worker) {
defer wg.Done()
p.destroyWorker(w)
}(w)
}
wg.Wait()
}
// finds free worker in a given time interval or creates new if allowed.
func (p *Pool) allocateWorker() (w *Worker, err error) {
select {
case w = <-p.free:
return w, nil
default:
// enable timeout handler
}
timeout := time.NewTimer(p.cfg.AllocateTimeout)
select {
case <-timeout.C:
return nil, fmt.Errorf("worker timeout (%s)", p.cfg.AllocateTimeout)
case w := <-p.free:
timeout.Stop()
return w, nil
}
}
// replaces dead or expired worker with new instance
func (p *Pool) replaceWorker(w *Worker, caused interface{}) {
go p.destroyWorker(w)
nw, _ := p.createWorker()
p.free <- nw
}
// destroy and remove worker from the pool.
func (p *Pool) destroyWorker(w *Worker) {
// detaching
p.muw.Lock()
for i, wc := range p.workers {
if wc == w {
p.workers = p.workers[:i+1]
break
}
}
p.muw.Unlock()
go w.Stop()
select {
case <-w.waitDone:
// worker is dead
case <-time.NewTimer(time.Second * 10).C:
// failed to stop process
if err := w.Kill(); err != nil {
//todo: can't kill or already killed?
}
}
}
// creates new worker using associated factory. automatically
// adds worker to the worker list (background)
func (p *Pool) createWorker() (*Worker, error) {
w, err := p.factory.SpawnWorker(p.cmd())
if err != nil {
return nil, err
}
p.muw.Lock()
defer p.muw.Unlock()
p.workers = append(p.workers, w)
return w, nil
}
|