refactor: extract the state module and make the backoff error instead of panic

This PR:
- moves state.go to its own module
- moves the phpheaders test the phpheaders module
- simplifies backoff.go
- makes the backoff error instead of panic (so it can be tested)
- removes some unused C structs
This commit is contained in:
Alexander Stecher
2025-12-02 23:10:12 +01:00
committed by GitHub
parent 16e2bbb969
commit 98573ed7c0
23 changed files with 268 additions and 335 deletions

View File

@@ -5,6 +5,8 @@ import (
"runtime"
"sync"
"sync/atomic"
"github.com/dunglas/frankenphp/internal/state"
)
// representation of a non-worker PHP thread
@@ -13,7 +15,7 @@ import (
type regularThread struct {
contextHolder
state *threadState
state *state.ThreadState
thread *phpThread
}
@@ -34,25 +36,27 @@ func convertToRegularThread(thread *phpThread) {
// beforeScriptExecution returns the name of the script or an empty string on shutdown
func (handler *regularThread) beforeScriptExecution() string {
switch handler.state.get() {
case stateTransitionRequested:
switch handler.state.Get() {
case state.TransitionRequested:
detachRegularThread(handler.thread)
return handler.thread.transitionToNewHandler()
case stateTransitionComplete:
handler.state.set(stateReady)
case state.TransitionComplete:
handler.thread.updateContext(false)
handler.state.Set(state.Ready)
return handler.waitForRequest()
case stateReady:
case state.Ready:
return handler.waitForRequest()
case stateShuttingDown:
case state.ShuttingDown:
detachRegularThread(handler.thread)
// signal to stop
return ""
}
panic("unexpected state: " + handler.state.name())
panic("unexpected state: " + handler.state.Name())
}
func (handler *regularThread) afterScriptExecution(_ int) {
@@ -75,7 +79,7 @@ func (handler *regularThread) waitForRequest() string {
// clear any previously sandboxed env
clearSandboxedEnv(handler.thread)
handler.state.markAsWaiting(true)
handler.state.MarkAsWaiting(true)
var ch contextHolder
@@ -89,7 +93,7 @@ func (handler *regularThread) waitForRequest() string {
handler.ctx = ch.ctx
handler.contextHolder.frankenPHPContext = ch.frankenPHPContext
handler.state.markAsWaiting(false)
handler.state.MarkAsWaiting(false)
// set the scriptFilename that should be executed
return handler.contextHolder.frankenPHPContext.scriptFilename