feat: Adds automatic thread scaling at runtime and php_ini configuration in Caddyfile (#1266)

Adds option to scale threads at runtime

Adds php_ini configuration in Caddyfile
This commit is contained in:
Alliballibaba2
2025-02-19 20:39:33 +01:00
committed by GitHub
parent 965fa6570c
commit 072151dfee
46 changed files with 1772 additions and 208 deletions

View File

@@ -51,6 +51,8 @@ Optionally, the number of threads to create and [worker scripts](worker.md) to s
{
frankenphp {
num_threads <num_threads> # Sets the number of PHP threads to start. Default: 2x the number of available CPUs.
max_threads <num_threads> # Limits the number of additional PHP threads that can be started at runtime. Default: num_threads. Can be set to 'auto'.
php_ini <key> <value> # Set a php.ini directive. Can be used several times to set multiple directives.
worker {
file <path> # Sets the path to the worker script.
num <num> # Sets the number of PHP threads to start, defaults to 2x the number of available CPUs.
@@ -227,6 +229,23 @@ To load [additional PHP configuration files](https://www.php.net/manual/en/confi
the `PHP_INI_SCAN_DIR` environment variable can be used.
When set, PHP will load all the file with the `.ini` extension present in the given directories.
You can also change the PHP configuration using the `php_ini` directive in the `Caddyfile`:
```caddyfile
{
frankenphp {
php_ini memory_limit 256M
# or
php_ini {
memory_limit 256M
max_execution_time 15
}
}
}
```
## Enable the Debug Mode
When using the Docker image, set the `CADDY_GLOBAL_OPTIONS` environment variable to `debug` to enable the debug mode:

View File

@@ -16,6 +16,16 @@ To find the right values, it's best to run load tests simulating real traffic.
To configure the number of threads, use the `num_threads` option of the `php_server` and `php` directives.
To change the number of workers, use the `num` option of the `worker` section of the `frankenphp` directive.
### `max_threads`
While it's always better to know exactly what your traffic will look like, real-life applications tend to be more
unpredictable. The `max_threads` allows FrankenPHP to automatically spawn additional threads at runtime up to the specified limit.
`max_threads` can help you
figure out how many threads you need to handle your traffic and can make the server more resilient to latency spikes.
If set to `auto`, the limit will be estimated based on the `memory_limit` in your `php.ini`. If not able to do so,
`auto` will instead default to 2x `num_threads`.
`max_threads is similar to PHP FPM's [pm.max_children](https://www.php.net/manual/en/install.fpm.configuration.php#pm.max-children).
## Worker Mode
Enabling [the worker mode](worker.md) dramatically improves performance,

View File

@@ -128,6 +128,16 @@ A workaround to using this type of code in worker mode is to restart the worker
The previous worker snippet allows configuring a maximum number of request to handle by setting an environment variable named `MAX_REQUESTS`.
### Restart Workers Manually
While it's possible to restart workers [on file changes](config.md#watching-for-file-changes), it's also possible to restart all workers
gracefully via the [Caddy admin API](https://caddyserver.com/docs/api). If the admin is enabled in your
[Caddyfile](config.md#caddyfile-config), you can ping the restart endpoint with a simple POST request like this:
```console
curl -X POST http://localhost:2019/frankenphp/workers/restart
```
### Worker Failures
If a worker script crashes with a non-zero exit code, FrankenPHP will restart it with an exponential backoff strategy.