Skip to content

Commit d22f365

Browse files
committed
wip
1 parent d49f244 commit d22f365

File tree

13 files changed

+145
-83
lines changed

13 files changed

+145
-83
lines changed

system/Cache/Handlers/MemcachedHandler.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,6 @@ public function isSupported(): bool
221221

222222
public function ping(): bool
223223
{
224-
if (! isset($this->memcached)) {
225-
return false;
226-
}
227-
228224
$version = $this->memcached->getVersion();
229225

230226
if ($this->memcached instanceof Memcached) {
@@ -240,20 +236,18 @@ public function ping(): bool
240236

241237
if ($this->memcached instanceof Memcache) {
242238
// Memcache extension returns string version
243-
return is_string($version) && $version !== false && $version !== '';
239+
return is_string($version) && $version !== '';
244240
}
245241

246242
return false;
247243
}
248244

249245
public function reconnect(): bool
250246
{
251-
if (isset($this->memcached)) {
252-
if ($this->memcached instanceof Memcached) {
253-
$this->memcached->quit();
254-
} elseif ($this->memcached instanceof Memcache) {
255-
$this->memcached->close();
256-
}
247+
if ($this->memcached instanceof Memcached) {
248+
$this->memcached->quit();
249+
} elseif ($this->memcached instanceof Memcache) {
250+
$this->memcached->close();
257251
}
258252

259253
try {

system/Cache/Handlers/PredisHandler.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ public function isSupported(): bool
207207

208208
public function ping(): bool
209209
{
210-
if (! isset($this->redis)) {
211-
return false;
212-
}
213-
214210
try {
215211
$result = $this->redis->ping();
216212

@@ -219,19 +215,17 @@ public function ping(): bool
219215
}
220216

221217
return $result === 'PONG';
222-
} catch (Exception $e) {
218+
} catch (Exception) {
223219
return false;
224220
}
225221
}
226222

227223
public function reconnect(): bool
228224
{
229-
if (isset($this->redis)) {
230-
try {
231-
$this->redis->disconnect();
232-
} catch (Exception $e) {
233-
// Connection already dead, that's fine
234-
}
225+
try {
226+
$this->redis->disconnect();
227+
} catch (Exception $e) {
228+
// Connection already dead, that's fine
235229
}
236230

237231
try {

system/Cache/Handlers/RedisHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ public function ping(): bool
229229
try {
230230
$result = $this->redis->ping();
231231

232-
return $result === true || $result === '+PONG' || $result === 'PONG';
233-
} catch (RedisException $e) {
232+
return in_array($result, [true, '+PONG', 'PONG'], true);
233+
} catch (RedisException) {
234234
return false;
235235
}
236236
}
@@ -240,7 +240,7 @@ public function reconnect(): bool
240240
if (isset($this->redis)) {
241241
try {
242242
$this->redis->close();
243-
} catch (RedisException $e) {
243+
} catch (RedisException) {
244244
// Connection already dead, that's fine
245245
}
246246
}

system/CodeIgniter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ class CodeIgniter
9595
/**
9696
* Current response.
9797
*
98-
* @var ResponseInterface
98+
* @var ResponseInterface|null
9999
*/
100100
protected $response;
101101

102102
/**
103103
* Router to use.
104104
*
105-
* @var Router
105+
* @var Router|null
106106
*/
107107
protected $router;
108108

@@ -116,14 +116,14 @@ class CodeIgniter
116116
/**
117117
* Controller method to invoke.
118118
*
119-
* @var string
119+
* @var string|null
120120
*/
121121
protected $method;
122122

123123
/**
124124
* Output handler to use.
125125
*
126-
* @var string
126+
* @var string|null
127127
*/
128128
protected $output;
129129

system/Commands/Worker/Views/Caddyfile.tpl

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
# Number of workers (default: 2x CPU cores)
1717
# Adjust based on your server capacity
18-
num 2
18+
num 16
1919
}
2020
}
2121

@@ -31,18 +31,6 @@
3131
# Enable compression
3232
encode zstd br gzip
3333
34-
# Security headers
35-
header {
36-
# Prevent clickjacking
37-
X-Frame-Options "SAMEORIGIN"
38-
# Prevent MIME type sniffing
39-
X-Content-Type-Options "nosniff"
40-
# Enable XSS protection
41-
X-XSS-Protection "1; mode=block"
42-
# Remove server header
43-
-Server
44-
}
45-
4634
# Route all PHP requests through the worker
4735
php_server {
4836
# Use frankenphp-worker.php as the entry point
@@ -54,28 +42,3 @@
5442
# Serve static files
5543
file_server
5644
}
57-
58-
# HTTPS configuration (uncomment and configure for production)
59-
# :443 {
60-
# # TLS certificate paths
61-
# tls /path/to/cert.pem /path/to/key.pem
62-
#
63-
# root * public
64-
# encode zstd br gzip
65-
#
66-
# header {
67-
# X-Frame-Options "SAMEORIGIN"
68-
# X-Content-Type-Options "nosniff"
69-
# X-XSS-Protection "1; mode=block"
70-
# -Server
71-
# # Enable HSTS for HTTPS
72-
# Strict-Transport-Security "max-age=31536000; includeSubDomains"
73-
# }
74-
#
75-
# php_server {
76-
# index frankenphp-worker.php
77-
# try_files {path} frankenphp-worker.php
78-
# }
79-
#
80-
# file_server
81-
# }

system/Commands/Worker/Views/frankenphp-worker.php.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ $workerConfig = config('WorkerMode');
6969
*/
7070

7171
$handler = function () use ($app, $workerConfig) {
72+
// Validate database connections
73+
DatabaseConfig::validateForWorkerMode($workerConfig);
74+
7275
// Reset request-specific state
7376
$app->resetForWorkerMode();
7477

system/Commands/Worker/WorkerInstall.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* This file is part of CodeIgniter 4 framework.
57
*
@@ -8,7 +10,6 @@
810
* For the full copyright and license information, please view
911
* the LICENSE file that was distributed with this source code.
1012
*/
11-
1213
namespace CodeIgniter\Commands\Worker;
1314

1415
use CodeIgniter\CLI\BaseCommand;

system/Commands/Worker/WorkerUninstall.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* This file is part of CodeIgniter 4 framework.
57
*
@@ -8,7 +10,6 @@
810
* For the full copyright and license information, please view
911
* the LICENSE file that was distributed with this source code.
1012
*/
11-
1213
namespace CodeIgniter\Commands\Worker;
1314

1415
use CodeIgniter\CLI\BaseCommand;
@@ -57,7 +58,7 @@ public function run(array $params)
5758
}
5859

5960
// No files to remove
60-
if (empty($existing)) {
61+
if ($existing === []) {
6162
CLI::write('No worker mode files found to remove.', 'yellow');
6263
CLI::newLine();
6364

system/Config/BaseService.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,31 @@ public static function reset(bool $initAutoloader = true)
374374
}
375375
}
376376

377+
/**
378+
* Validate cache connections for worker mode at the start of a request.
379+
* Checks if cache connection is alive and reconnects if needed.
380+
*
381+
* This should be called at the beginning of each request in worker mode,
382+
* before the application runs.
383+
*/
384+
public static function validateForWorkerMode(WorkerMode $config): void
385+
{
386+
if ($config->cacheStrategy !== 'keep-alive' || ! isset(static::$instances['cache'])) {
387+
return;
388+
}
389+
390+
$cache = static::$instances['cache'];
391+
392+
if (! $cache->ping()) {
393+
$cache->reconnect();
394+
}
395+
}
396+
377397
/**
378398
* Resets all services except those in the persistent list.
379399
* Used for worker mode to preserve expensive-to-initialize services.
400+
*
401+
* Called at the END of each request to clean up state.
380402
*/
381403
public static function resetForWorkerMode(WorkerMode $config): void
382404
{
@@ -392,11 +414,6 @@ public static function resetForWorkerMode(WorkerMode $config): void
392414
foreach (static::$instances as $serviceName => $service) {
393415
if ($serviceName === 'cache') {
394416
if ($config->cacheStrategy === 'keep-alive') {
395-
// Ping the connection and reconnect if needed
396-
if (! $service->ping()) {
397-
$service->reconnect();
398-
}
399-
400417
// Persist cache instance in keep-alive mode
401418
$persistentInstances[$serviceName] = $service;
402419
}

system/Database/Config.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,33 @@ protected static function ensureFactory()
154154
static::$factory = new Database();
155155
}
156156

157+
/**
158+
* Validate database connections for worker mode at the start of a request.
159+
* Checks if connections are alive and reconnects if needed.
160+
*
161+
* This should be called at the beginning of each request in worker mode,
162+
* before the application runs.
163+
*/
164+
public static function validateForWorkerMode(WorkerMode $config): void
165+
{
166+
if ($config->databaseStrategy !== 'keep-alive') {
167+
return;
168+
}
169+
170+
foreach (static::$instances as $connection) {
171+
if (! $connection->ping()) {
172+
$connection->reconnect();
173+
}
174+
}
175+
}
176+
157177
/**
158178
* Reset database connections for worker mode.
159-
* Behavior controlled by WorkerMode config.
160179
*
161180
* This method handles connection state management between requests
162181
* in long-running worker processes.
182+
*
183+
* Called at the END of each request to clean up state.
163184
*/
164185
public static function resetForWorkerMode(WorkerMode $config): void
165186
{
@@ -184,11 +205,6 @@ public static function resetForWorkerMode(WorkerMode $config): void
184205
if ($connection->transDepth > 0) {
185206
log_message('warning', "Database connection has uncommitted transactions after cleanup: {$group}");
186207
}
187-
188-
if (! $connection->ping()) {
189-
log_message('info', "Database connection dead, reconnecting: {$group}");
190-
$connection->reconnect();
191-
}
192208
break;
193209
}
194210
}

0 commit comments

Comments
 (0)