chore: cs improvements

This commit is contained in:
Kévin Dunglas
2026-02-11 15:42:57 +01:00
parent 82d6696c9d
commit e2062af083
3 changed files with 15 additions and 6 deletions

View File

@@ -3,12 +3,14 @@ package caddy
import (
"encoding/json"
"fmt"
"net/http"
"github.com/caddyserver/caddy/v2"
"github.com/dunglas/frankenphp"
"net/http"
)
type FrankenPHPAdmin struct{}
type FrankenPHPAdmin struct {
}
// if the id starts with "admin.api" the module will register AdminRoutes via module.Routes()
func (FrankenPHPAdmin) CaddyModule() caddy.ModuleInfo {

View File

@@ -141,13 +141,15 @@ func (ts *ThreadState) notifySubscribers(nextState State) {
ts.subscribers = ts.subscribers[:n]
}
// block until the thread reaches a certain state
// WaitFor blocks until the thread reaches a certain state
func (ts *ThreadState) WaitFor(states ...State) {
ts.mu.Lock()
if slices.Contains(states, ts.currentState) {
ts.mu.Unlock()
return
}
sub := stateSubscriber{
states: states,
ch: make(chan struct{}),
@@ -157,7 +159,7 @@ func (ts *ThreadState) WaitFor(states ...State) {
<-sub.ch
}
// safely request a state change from a different goroutine
// RequestSafeStateChange safely requests a state change from a different goroutine
func (ts *ThreadState) RequestSafeStateChange(nextState State) bool {
ts.mu.Lock()
switch ts.currentState {

View File

@@ -25,7 +25,7 @@ type phpThread struct {
sandboxedEnv map[string]*C.zend_string
}
// interface that defines how the callbacks from the C thread should be handled
// threadHandler defines how the callbacks from the C thread should be handled
type threadHandler interface {
name() string
beforeScriptExecution() string
@@ -68,6 +68,7 @@ func (thread *phpThread) shutdown() {
if !thread.state.RequestSafeStateChange(state.ShuttingDown) {
// already shutting down or done, wait for the C thread to finish
thread.state.WaitFor(state.Done, state.Reserved)
return
}
@@ -81,17 +82,19 @@ func (thread *phpThread) shutdown() {
}
}
// change the thread handler safely
// setHandler changes the thread handler safely
// must be called from outside the PHP thread
func (thread *phpThread) setHandler(handler threadHandler) {
thread.handlerMu.Lock()
defer thread.handlerMu.Unlock()
if !thread.state.RequestSafeStateChange(state.TransitionRequested) {
// no state change allowed == shutdown or done
return
}
close(thread.drainChan)
thread.state.WaitFor(state.TransitionInProgress)
thread.handler = handler
thread.drainChan = make(chan struct{})
@@ -125,6 +128,7 @@ func (thread *phpThread) name() string {
thread.handlerMu.RLock()
name := thread.handler.name()
thread.handlerMu.RUnlock()
return name
}
@@ -135,6 +139,7 @@ func (thread *phpThread) pinString(s string) *C.char {
if sData == nil {
return nil
}
thread.Pin(sData)
return (*C.char)(unsafe.Pointer(sData))