Skip to content

Commit c8a490a

Browse files
committed
Merge branch 'main' into workers
2 parents 06ae9fc + 7c7afe1 commit c8a490a

17 files changed

Lines changed: 64 additions & 58 deletions

.github/workflows/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
- name: Run integrations tests
6969
run: ./reload_test.sh
7070
- name: Lint Go code
71-
uses: golangci/golangci-lint-action@v6
71+
uses: golangci/golangci-lint-action@v7
7272
if: matrix.php-versions == '8.4'
7373
with:
7474
version: latest

build-static.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,20 @@ if [ -n "${DEBUG_SYMBOLS}" ]; then
5858
fi
5959
# php version to build
6060
if [ -z "${PHP_VERSION}" ]; then
61-
export PHP_VERSION="8.4"
61+
get_latest_php_version() {
62+
input="$1"
63+
json=$(curl -s "https://www.php.net/releases/index.php?json&version=$input")
64+
latest=$(echo "$json" | jq -r '.version')
65+
66+
if [[ "$latest" == "$input"* ]]; then
67+
echo "$latest"
68+
else
69+
echo "$input"
70+
fi
71+
}
72+
73+
PHP_VERSION="$(get_latest_php_version "8")"
74+
export PHP_VERSION
6275
fi
6376
# default extension set
6477
defaultExtensions="apcu,bcmath,bz2,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,ftp,gd,gmp,gettext,iconv,igbinary,imagick,intl,ldap,mbregex,mbstring,mysqli,mysqlnd,opcache,openssl,parallel,pcntl,pdo,pdo_mysql,pdo_pgsql,pdo_sqlite,pgsql,phar,posix,protobuf,readline,redis,session,shmop,simplexml,soap,sockets,sodium,sqlite3,ssh2,sysvmsg,sysvsem,sysvshm,tidy,tokenizer,xlswriter,xml,xmlreader,xmlwriter,zip,zlib,yaml,zstd"
@@ -138,9 +151,9 @@ if type "brew" >/dev/null 2>&1; then
138151
fi
139152

140153
if [ "${SPC_REL_TYPE}" = "binary" ]; then
141-
mkdir static-php-cli/
154+
mkdir -p static-php-cli/
142155
cd static-php-cli/
143-
curl -o spc -fsSL "https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-$(uname -m)"
156+
curl -o spc -fsSL "https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-${arch}"
144157
chmod +x spc
145158
spcCommand="./spc"
146159
elif [ -d "static-php-cli/src" ]; then

caddy/admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (admin *FrankenPHPAdmin) restartWorkers(w http.ResponseWriter, r *http.Requ
4444
return nil
4545
}
4646

47-
func (admin *FrankenPHPAdmin) threads(w http.ResponseWriter, r *http.Request) error {
47+
func (admin *FrankenPHPAdmin) threads(w http.ResponseWriter, _ *http.Request) error {
4848
debugState := frankenphp.DebugState()
4949
prettyJson, err := json.MarshalIndent(debugState, "", " ")
5050
if err != nil {

cgi.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func packCgiVariable(key *C.zend_string, value string) C.ht_key_value_pair {
168168
return C.ht_key_value_pair{key, toUnsafeChar(value), C.size_t(len(value))}
169169
}
170170

171-
func addHeadersToServer(thread *phpThread, fc *frankenPHPContext, trackVarsArray *C.zval) {
171+
func addHeadersToServer(fc *frankenPHPContext, trackVarsArray *C.zval) {
172172
for field, val := range fc.request.Header {
173173
if k := mainThread.commonHeaders[field]; k != nil {
174174
v := strings.Join(val, ", ")
@@ -197,7 +197,7 @@ func go_register_variables(threadIndex C.uintptr_t, trackVarsArray *C.zval) {
197197
fc := thread.getRequestContext()
198198

199199
addKnownVariablesToServer(thread, fc, trackVarsArray)
200-
addHeadersToServer(thread, fc, trackVarsArray)
200+
addHeadersToServer(fc, trackVarsArray)
201201

202202
// The Prepared Environment is registered last and can overwrite any previous values
203203
addPreparedEnvToServer(fc, trackVarsArray)

docs/config.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
FrankenPHP, Caddy as well as the Mercure and Vulcain modules can be configured using [the formats supported by Caddy](https://caddyserver.com/docs/getting-started#your-first-config).
44

5-
In [the Docker images](docker.md), the `Caddyfile` is located at `/etc/caddy/Caddyfile`.
6-
The static binary will look for the `Caddyfile` in the directory in which it is started.
5+
In [the Docker images](docker.md), the `Caddyfile` is located at `/etc/caddy/Caddyfile`. The static binary will look for the `Caddyfile` in the directory where the `frankenphp run` command is executed. You can specify a custom path with the `-c` or `--config` option.
76

87
PHP itself can be configured [using a `php.ini` file](https://www.php.net/manual/en/configuration.file.php).
98

@@ -103,7 +102,9 @@ other.example.com {
103102
```
104103

105104
Using the `php_server` directive is generally what you need,
106-
but if you need full control, you can use the lower level `php` directive:
105+
but if you need full control, you can use the lower-level `php` directive.
106+
The `php` directive passes all input to PHP, instead of first checking whether
107+
it's a PHP file or not. Read more about it in the [performance page](performance.md).
107108

108109
Using the `php_server` directive is equivalent to this configuration:
109110

docs/fr/config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ other.example.com {
104104

105105
L'utilisation de la directive `php_server` est généralement suffisante,
106106
mais si vous avez besoin d'un contrôle total, vous pouvez utiliser la directive `php`, qui permet un plus grand niveau de finesse dans la configuration.
107+
La directive `php` transmet toutes les entrées à PHP, au lieu de vérifier d'abord si
108+
c'est un fichier PHP ou pas. En savoir plus à ce sujet dans la [page performances](performance.md).
107109

108110
Utiliser la directive `php_server` est équivalent à cette configuration :
109111

frankenphp.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ type contextKeyStruct struct{}
5555
var contextKey = contextKeyStruct{}
5656

5757
var (
58-
InvalidRequestError = errors.New("not a FrankenPHP request")
59-
AlreadyStartedError = errors.New("FrankenPHP is already started")
60-
InvalidPHPVersionError = errors.New("FrankenPHP is only compatible with PHP 8.2+")
61-
MainThreadCreationError = errors.New("error creating the main thread")
62-
RequestContextCreationError = errors.New("error during request context creation")
63-
ScriptExecutionError = errors.New("error during PHP script execution")
64-
NotRunningError = errors.New("FrankenPHP is not running. For proper configuration visit: https://frankenphp.dev/docs/config/#caddyfile-config")
58+
ErrInvalidRequest = errors.New("not a FrankenPHP request")
59+
ErrAlreadyStarted = errors.New("FrankenPHP is already started")
60+
ErrInvalidPHPVersion = errors.New("FrankenPHP is only compatible with PHP 8.2+")
61+
ErrMainThreadCreation = errors.New("error creating the main thread")
62+
ErrRequestContextCreation = errors.New("error during request context creation")
63+
ErrScriptExecution = errors.New("error during PHP script execution")
64+
ErrNotRunning = errors.New("FrankenPHP is not running. For proper configuration visit: https://frankenphp.dev/docs/config/#caddyfile-config")
6565

6666
isRunning bool
6767

@@ -189,7 +189,7 @@ func calculateMaxThreads(opt *opt) (int, int, int, error) {
189189
return opt.numThreads, numWorkers, opt.maxThreads, nil
190190
}
191191

192-
if !numThreadsIsSet && !maxThreadsIsSet {
192+
if !numThreadsIsSet {
193193
if numWorkers >= maxProcs {
194194
// Start at least as many threads as workers, and keep a free thread to handle requests in non-worker mode
195195
opt.numThreads = numWorkers + 1
@@ -218,7 +218,7 @@ func calculateMaxThreads(opt *opt) (int, int, int, error) {
218218
// Init starts the PHP runtime and the configured workers.
219219
func Init(options ...Option) error {
220220
if isRunning {
221-
return AlreadyStartedError
221+
return ErrAlreadyStarted
222222
}
223223
isRunning = true
224224

@@ -265,7 +265,7 @@ func Init(options ...Option) error {
265265
config := Config()
266266

267267
if config.Version.MajorVersion < 8 || (config.Version.MajorVersion == 8 && config.Version.MinorVersion < 2) {
268-
return InvalidPHPVersionError
268+
return ErrInvalidPHPVersion
269269
}
270270

271271
if config.ZTS {
@@ -381,7 +381,7 @@ func updateServerContext(thread *phpThread, fc *frankenPHPContext, isWorkerReque
381381
)
382382

383383
if ret > 0 {
384-
return RequestContextCreationError
384+
return ErrRequestContextCreation
385385
}
386386

387387
return nil
@@ -390,12 +390,12 @@ func updateServerContext(thread *phpThread, fc *frankenPHPContext, isWorkerReque
390390
// ServeHTTP executes a PHP script according to the given context.
391391
func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error {
392392
if !isRunning {
393-
return NotRunningError
393+
return ErrNotRunning
394394
}
395395

396396
fc, ok := fromContext(request.Context())
397397
if !ok {
398-
return InvalidRequestError
398+
return ErrInvalidRequest
399399
}
400400

401401
fc.responseWriter = responseWriter

frankenphp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ func testFileUpload(t *testing.T, opts *testOptions) {
754754
_, err := part.Write([]byte("bar"))
755755
require.NoError(t, err)
756756

757-
writer.Close()
757+
require.NoError(t, writer.Close())
758758

759759
req := httptest.NewRequest("POST", "http://example.com/file-upload.php", requestBody)
760760
req.Header.Add("Content-Type", writer.FormDataContentType())

internal/watcher/watcher.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,23 @@ var failureMu = sync.Mutex{}
3737
var watcherIsActive = atomic.Bool{}
3838

3939
var (
40+
ErrAlreadyStarted = errors.New("the watcher is already running")
41+
ErrUnableToStartWatching = errors.New("unable to start the watcher")
42+
4043
// the currently active file watcher
4144
activeWatcher *watcher
4245
// after stopping the watcher we will wait for eventual reloads to finish
4346
reloadWaitGroup sync.WaitGroup
4447
// we are passing the logger from the main package to the watcher
45-
logger *zap.Logger
46-
AlreadyStartedError = errors.New("the watcher is already running")
47-
UnableToStartWatching = errors.New("unable to start the watcher")
48+
logger *zap.Logger
4849
)
4950

5051
func InitWatcher(filePatterns []string, callback func(), zapLogger *zap.Logger) error {
5152
if len(filePatterns) == 0 {
5253
return nil
5354
}
5455
if watcherIsActive.Load() {
55-
return AlreadyStartedError
56+
return ErrAlreadyStarted
5657
}
5758
watcherIsActive.Store(true)
5859
logger = zapLogger
@@ -142,7 +143,7 @@ func startSession(w *watchPattern) (C.uintptr_t, error) {
142143
}
143144
logger.Error("couldn't start watching", zap.String("dir", w.dir))
144145

145-
return watchSession, UnableToStartWatching
146+
return watchSession, ErrUnableToStartWatching
146147
}
147148

148149
func stopSession(session C.uintptr_t) {
@@ -186,7 +187,6 @@ func listenForFileEvents(triggerWatcher chan string, stopWatcher chan struct{})
186187
for {
187188
select {
188189
case <-stopWatcher:
189-
break
190190
case lastChangedFile = <-triggerWatcher:
191191
timer.Reset(debounceDuration)
192192
case <-timer.C:

metrics.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
const (
1212
StopReasonCrash = iota
1313
StopReasonRestart
14-
StopReasonShutdown
14+
//StopReasonShutdown
1515
)
1616

1717
type StopReason int
@@ -74,9 +74,9 @@ func (n nullMetrics) StartWorkerRequest(string) {
7474
func (n nullMetrics) Shutdown() {
7575
}
7676

77-
func (n nullMetrics) QueuedWorkerRequest(name string) {}
77+
func (n nullMetrics) QueuedWorkerRequest(string) {}
7878

79-
func (n nullMetrics) DequeuedWorkerRequest(name string) {}
79+
func (n nullMetrics) DequeuedWorkerRequest(string) {}
8080

8181
func (n nullMetrics) QueuedRequest() {}
8282
func (n nullMetrics) DequeuedRequest() {}
@@ -127,9 +127,10 @@ func (m *PrometheusMetrics) StopWorker(name string, reason StopReason) {
127127
m.totalWorkers.WithLabelValues(name).Dec()
128128
m.readyWorkers.WithLabelValues(name).Dec()
129129

130-
if reason == StopReasonCrash {
130+
switch reason {
131+
case StopReasonCrash:
131132
m.workerCrashes.WithLabelValues(name).Inc()
132-
} else if reason == StopReasonRestart {
133+
case StopReasonRestart:
133134
m.workerRestarts.WithLabelValues(name).Inc()
134135
}
135136
}

0 commit comments

Comments
 (0)