Skip to content

Commit d0b259d

Browse files
ensure worker failures do not count fatal errors during the request (#1336)
* ensure worker failures do not count fatal errors during the request * only count towards the backoff if it was not in a request
1 parent 0681c63 commit d0b259d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

thread-worker.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type workerThread struct {
2121
fakeRequest *http.Request
2222
workerRequest *http.Request
2323
backoff *exponentialBackoff
24+
inRequest bool // true if the worker is currently handling a request
2425
}
2526

2627
func convertToWorkerThread(thread *phpThread, worker *worker) {
@@ -130,14 +131,15 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {
130131

131132
// on exit status 1 we apply an exponential backoff when restarting
132133
metrics.StopWorker(worker.fileName, StopReasonCrash)
133-
if handler.backoff.recordFailure() {
134+
if !handler.inRequest && handler.backoff.recordFailure() {
134135
if !watcherIsEnabled {
135136
logger.Panic("too many consecutive worker failures", zap.String("worker", worker.fileName), zap.Int("failures", handler.backoff.failureCount))
136137
}
137138
logger.Warn("many consecutive worker failures", zap.String("worker", worker.fileName), zap.Int("failures", handler.backoff.failureCount))
138139
}
139140
}
140141

142+
// waitForWorkerRequest is called during frankenphp_handle_request in the php worker script.
141143
func (handler *workerThread) waitForWorkerRequest() bool {
142144
// unpin any memory left over from previous requests
143145
handler.thread.Unpin()
@@ -172,6 +174,7 @@ func (handler *workerThread) waitForWorkerRequest() bool {
172174
if c := logger.Check(zapcore.DebugLevel, "request handling started"); c != nil {
173175
c.Write(zap.String("worker", handler.worker.fileName), zap.String("url", r.RequestURI))
174176
}
177+
handler.inRequest = true
175178

176179
if err := updateServerContext(handler.thread, r, false, true); err != nil {
177180
// Unexpected error or invalid request
@@ -185,15 +188,20 @@ func (handler *workerThread) waitForWorkerRequest() bool {
185188

186189
return handler.waitForWorkerRequest()
187190
}
191+
188192
return true
189193
}
190194

195+
// go_frankenphp_worker_handle_request_start is called at the start of every php request served.
196+
//
191197
//export go_frankenphp_worker_handle_request_start
192198
func go_frankenphp_worker_handle_request_start(threadIndex C.uintptr_t) C.bool {
193199
handler := phpThreads[threadIndex].handler.(*workerThread)
194200
return C.bool(handler.waitForWorkerRequest())
195201
}
196202

203+
// go_frankenphp_finish_worker_request is called at the end of every php request served.
204+
//
197205
//export go_frankenphp_finish_worker_request
198206
func go_frankenphp_finish_worker_request(threadIndex C.uintptr_t) {
199207
thread := phpThreads[threadIndex]
@@ -202,6 +210,7 @@ func go_frankenphp_finish_worker_request(threadIndex C.uintptr_t) {
202210

203211
maybeCloseContext(fc)
204212
thread.handler.(*workerThread).workerRequest = nil
213+
thread.handler.(*workerThread).inRequest = false
205214

206215
if c := fc.logger.Check(zapcore.DebugLevel, "request handling finished"); c != nil {
207216
c.Write(zap.String("worker", fc.scriptFilename), zap.String("url", r.RequestURI))

0 commit comments

Comments
 (0)