-
-
Notifications
You must be signed in to change notification settings - Fork 251
/
worker.go
329 lines (278 loc) · 8.13 KB
/
worker.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package frankenphp
// #include <stdlib.h>
// #include "frankenphp.h"
import "C"
import (
"fmt"
"github.com/dunglas/frankenphp/internal/fastabs"
"net/http"
"path/filepath"
"sync"
"time"
"github.com/dunglas/frankenphp/internal/watcher"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type worker struct {
fileName string
num int
env PreparedEnv
requestChan chan *http.Request
threads []*phpThread
threadMutex sync.RWMutex
ready chan struct{}
}
var (
watcherIsEnabled bool
workerShutdownWG sync.WaitGroup
workersDone chan interface{}
workers = make(map[string]*worker)
)
func initWorkers(opt []workerOpt) error {
workersDone = make(chan interface{})
ready := sync.WaitGroup{}
for _, o := range opt {
worker, err := newWorker(o)
worker.threads = make([]*phpThread, 0, o.num)
if err != nil {
return err
}
for i := 0; i < worker.num; i++ {
go worker.startNewWorkerThread()
}
ready.Add(1)
go func() {
for i := 0; i < worker.num; i++ {
<-worker.ready
}
ready.Done()
}()
}
ready.Wait()
return nil
}
func newWorker(o workerOpt) (*worker, error) {
absFileName, err := fastabs.FastAbs(o.fileName)
if err != nil {
return nil, fmt.Errorf("worker filename is invalid %q: %w", o.fileName, err)
}
// if the worker already exists, return it,
// it's necessary since we don't want to destroy the channels when restarting on file changes
if w, ok := workers[absFileName]; ok {
return w, nil
}
if o.env == nil {
o.env = make(PreparedEnv, 1)
}
o.env["FRANKENPHP_WORKER\x00"] = "1"
w := &worker{
fileName: absFileName,
num: o.num,
env: o.env,
requestChan: make(chan *http.Request),
ready: make(chan struct{}, o.num),
}
workers[absFileName] = w
return w, nil
}
func (worker *worker) startNewWorkerThread() {
workerShutdownWG.Add(1)
defer workerShutdownWG.Done()
backoff := &exponentialBackoff{
maxBackoff: 1 * time.Second,
minBackoff: 100 * time.Millisecond,
maxConsecutiveFailures: 6,
}
for {
// if the worker can stay up longer than backoff*2, it is probably an application error
backoff.wait()
metrics.StartWorker(worker.fileName)
// Create main dummy request
r, err := http.NewRequest(http.MethodGet, filepath.Base(worker.fileName), nil)
if err != nil {
panic(err)
}
r, err = NewRequestWithContext(
r,
WithRequestDocumentRoot(filepath.Dir(worker.fileName), false),
WithRequestPreparedEnv(worker.env),
)
if err != nil {
panic(err)
}
if c := logger.Check(zapcore.DebugLevel, "starting"); c != nil {
c.Write(zap.String("worker", worker.fileName), zap.Int("num", worker.num))
}
if err := ServeHTTP(nil, r); err != nil {
panic(err)
}
fc := r.Context().Value(contextKey).(*FrankenPHPContext)
// if we are done, exit the loop that restarts the worker script
select {
case _, ok := <-workersDone:
if !ok {
metrics.StopWorker(worker.fileName, StopReasonShutdown)
if c := logger.Check(zapcore.DebugLevel, "terminated"); c != nil {
c.Write(zap.String("worker", worker.fileName))
}
return
}
// continue on since the channel is still open
default:
// continue on since the channel is still open
}
// on exit status 0 we just run the worker script again
if fc.exitStatus == 0 {
// TODO: make the max restart configurable
if c := logger.Check(zapcore.InfoLevel, "restarting"); c != nil {
c.Write(zap.String("worker", worker.fileName))
}
metrics.StopWorker(worker.fileName, StopReasonRestart)
backoff.recordSuccess()
continue
}
// on exit status 1 we log the error and apply an exponential backoff when restarting
if backoff.recordFailure() {
if !watcherIsEnabled {
panic(fmt.Errorf("workers %q: too many consecutive failures", worker.fileName))
}
logger.Warn("many consecutive worker failures", zap.String("worker", worker.fileName), zap.Int("failures", backoff.failureCount))
}
metrics.StopWorker(worker.fileName, StopReasonCrash)
}
// unreachable
}
func (worker *worker) handleRequest(r *http.Request) {
worker.threadMutex.RLock()
// dispatch requests to all worker threads in order
for _, thread := range worker.threads {
select {
case thread.requestChan <- r:
worker.threadMutex.RUnlock()
return
default:
}
}
worker.threadMutex.RUnlock()
// if no thread was available, fan the request out to all threads
// TODO: theoretically there could be autoscaling of threads here
worker.requestChan <- r
}
func stopWorkers() {
close(workersDone)
}
func drainWorkers() {
watcher.DrainWatcher()
watcherIsEnabled = false
stopWorkers()
workerShutdownWG.Wait()
workers = make(map[string]*worker)
}
func restartWorkersOnFileChanges(workerOpts []workerOpt) error {
var directoriesToWatch []string
for _, w := range workerOpts {
directoriesToWatch = append(directoriesToWatch, w.watch...)
}
watcherIsEnabled = len(directoriesToWatch) > 0
if !watcherIsEnabled {
return nil
}
restartWorkers := func() {
restartWorkers(workerOpts)
}
if err := watcher.InitWatcher(directoriesToWatch, restartWorkers, getLogger()); err != nil {
return err
}
return nil
}
func restartWorkers(workerOpts []workerOpt) {
stopWorkers()
workerShutdownWG.Wait()
if err := initWorkers(workerOpts); err != nil {
logger.Error("failed to restart workers when watching files")
panic(err)
}
logger.Info("workers restarted successfully")
}
func assignThreadToWorker(thread *phpThread) {
fc := thread.mainRequest.Context().Value(contextKey).(*FrankenPHPContext)
worker, ok := workers[fc.scriptFilename]
if !ok {
panic("worker not found for script: " + fc.scriptFilename)
}
thread.worker = worker
thread.requestChan = make(chan *http.Request)
worker.threadMutex.Lock()
worker.threads = append(worker.threads, thread)
worker.threadMutex.Unlock()
}
//export go_frankenphp_worker_handle_request_start
func go_frankenphp_worker_handle_request_start(threadIndex C.uintptr_t) C.bool {
thread := phpThreads[threadIndex]
// we assign a worker to the thread if it doesn't have one already
if thread.worker == nil {
assignThreadToWorker(thread)
}
thread.readiedOnce.Do(func() {
// inform metrics that the worker is ready
metrics.ReadyWorker(thread.worker.fileName)
})
select {
case thread.worker.ready <- struct{}{}:
default:
}
if c := logger.Check(zapcore.DebugLevel, "waiting for request"); c != nil {
c.Write(zap.String("worker", thread.worker.fileName))
}
var r *http.Request
select {
case <-workersDone:
if c := logger.Check(zapcore.DebugLevel, "shutting down"); c != nil {
c.Write(zap.String("worker", thread.worker.fileName))
}
thread.worker = nil
C.frankenphp_reset_opcache()
return C.bool(false)
case r = <-thread.worker.requestChan:
case r = <-thread.requestChan:
}
thread.workerRequest = r
if c := logger.Check(zapcore.DebugLevel, "request handling started"); c != nil {
c.Write(zap.String("worker", thread.worker.fileName), zap.String("url", r.RequestURI))
}
if err := updateServerContext(thread, r, false, true); err != nil {
// Unexpected error
if c := logger.Check(zapcore.DebugLevel, "unexpected error"); c != nil {
c.Write(zap.String("worker", thread.worker.fileName), zap.String("url", r.RequestURI), zap.Error(err))
}
fc := r.Context().Value(contextKey).(*FrankenPHPContext)
rejectRequest(fc.responseWriter, err.Error())
maybeCloseContext(fc)
thread.workerRequest = nil
thread.Unpin()
return go_frankenphp_worker_handle_request_start(threadIndex)
}
return C.bool(true)
}
//export go_frankenphp_finish_request
func go_frankenphp_finish_request(threadIndex C.uintptr_t, isWorkerRequest bool) {
thread := phpThreads[threadIndex]
r := thread.getActiveRequest()
fc := r.Context().Value(contextKey).(*FrankenPHPContext)
if isWorkerRequest {
thread.workerRequest = nil
}
maybeCloseContext(fc)
if c := fc.logger.Check(zapcore.DebugLevel, "request handling finished"); c != nil {
var fields []zap.Field
if isWorkerRequest {
fields = append(fields, zap.String("worker", fc.scriptFilename), zap.String("url", r.RequestURI))
} else {
fields = append(fields, zap.String("url", r.RequestURI))
}
c.Write(fields...)
}
if isWorkerRequest {
thread.Unpin()
}
}