Skip to content

Commit 5e89b70

Browse files
committed
Fix FrankenPHP worker mode stalls with explicit workers, wider jitter and Redis sessions
- Set explicit 12 workers in Caddyfile instead of relying on default - Widen worker restart jitter from ±20% to ±50% to prevent thundering herd - Add Redis PHP extension to Dockerfile - Switch session handler from file-based to Redis (db 4) to eliminate file locking - Add REDIS_HOST env and host.docker.internal to both containers
1 parent 3226cb0 commit 5e89b70

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

config/app_custom.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
'logger' => ErrorLogger::class,
5050
],
5151

52+
'Cache' => [
53+
'session' => [
54+
'className' => \Cake\Cache\Engine\RedisEngine::class,
55+
'host' => env('REDIS_HOST', '127.0.0.1'),
56+
'database' => 4,
57+
'prefix' => 'sandbox_session_',
58+
'duration' => '+1 week',
59+
],
60+
],
61+
5262
'Log' => [
5363
'debug' => [
5464
//'className' => 'File',
@@ -95,7 +105,10 @@
95105
],
96106

97107
'Session' => [
98-
//'defaults' => 'database',
108+
'defaults' => 'cache',
109+
'handler' => [
110+
'config' => 'session',
111+
],
99112
'timeout' => 60000,
100113
'ini' => [
101114
'session.cookie_lifetime' => WEEK,

docker/Caddyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@
3434

3535
# PHP handling via FrankenPHP in worker mode
3636
php_server {
37-
worker /app/webroot/worker.php
37+
worker /app/webroot/worker.php 12
3838
}
3939
}

docker/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ RUN install-php-extensions \
1515
pdo_mysql \
1616
zip \
1717
opcache \
18-
gd
18+
gd \
19+
redis
1920

2021
# PHP configuration
2122
RUN echo "upload_max_filesize = 32M" >> /usr/local/etc/php/conf.d/uploads.ini \

docker/docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ services:
1818
- MERCURE_PUBLISHER_JWT_KEY=${MERCURE_PUBLISHER_JWT_KEY}
1919
- MERCURE_SUBSCRIBER_JWT_KEY=${MERCURE_SUBSCRIBER_JWT_KEY}
2020
- MERCURE_EXTRA_DIRECTIVES=anonymous
21+
- REDIS_HOST=host.docker.internal
22+
extra_hosts:
23+
- "host.docker.internal:host-gateway"
2124
security_opt:
2225
- no-new-privileges:true
2326
cap_add:
@@ -38,6 +41,9 @@ services:
3841
environment:
3942
- MERCURE_PUBLISHER_JWT_KEY=${MERCURE_PUBLISHER_JWT_KEY}
4043
- MERCURE_SUBSCRIBER_JWT_KEY=${MERCURE_SUBSCRIBER_JWT_KEY}
44+
- REDIS_HOST=host.docker.internal
45+
extra_hosts:
46+
- "host.docker.internal:host-gateway"
4147
security_opt:
4248
- no-new-privileges:true
4349

webroot/worker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
$server = new Server($app);
3131

3232
// Max requests before worker restarts (prevents memory leaks)
33-
// Add jitter to prevent all workers restarting simultaneously (thundering herd)
33+
// Add wide jitter to prevent all workers restarting simultaneously (thundering herd)
3434
$maxRequests = (int)($_SERVER['MAX_REQUESTS'] ?? 500);
35-
$maxRequests = random_int((int)($maxRequests * 0.8), (int)($maxRequests * 1.2));
35+
$maxRequests = random_int((int)($maxRequests * 0.5), (int)($maxRequests * 1.5));
3636

3737
// Handle requests in a loop
3838
for ($requestCount = 0; $requestCount < $maxRequests; $requestCount++) {

0 commit comments

Comments
 (0)