-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: FrankenPHP Worker Mode #9889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.7
Are you sure you want to change the base?
Conversation
paulbalandan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to read up on this, but sharing initial comments.
| * This configuration controls how CodeIgniter behaves when running | ||
| * in worker mode (with FrankenPHP). | ||
| */ | ||
| class WorkerMode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this config need to extend BaseConfig?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep in mind that these values will change in Registrars and env.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't think so. Although overrides from the .env file might be acceptable in theory, allowing Registrars would be misleading, as the values are only used once and not on a per-request basis. I would rather leave it as it is.
system/Config/BaseService.php
Outdated
| * This should be called at the beginning of each request in worker mode, | ||
| * before the application runs. | ||
| */ | ||
| public static function validateForWorkerMode(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first glance, the name is not descriptive of what it does actually. From its name, I am expecting that this validates something when using worker mode but is actually reconnecting the cache if needed. Can you rename this along that behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method name comes from previous iterations (I had a few), where more things were happening.
I will use "reconnect" in the method name, although I'm not sure if that won't be misleading, as we first check if the connection is still valid and reconnect only if it's not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about "reconnectIfNeededForWorkerMode". verbose but it gets the idea through
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's pretty long, and we still don't know what it's about... The current one is reconnectCacheForWorkerMode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be simpler without ForWorkerMode part, but I would like to keep it.
system/Database/Config.php
Outdated
| * This should be called at the beginning of each request in worker mode, | ||
| * before the application runs. | ||
| */ | ||
| public static function validateForWorkerMode(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same commentary as above but for database connections.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
|
👋 Hi, @michalsn! |
Description
This PR adds experimental support for FrankenPHP Worker Mode, allowing to handle multiple HTTP requests within the same PHP process. Instead of bootstrapping the framework for every request (traditional PHP-FPM model), the worker boots once and reuses resources across requests, resulting in 2-3x performance improvements for typical database-driven applications.
How it works
New features
app/Config/WorkerMode.php- Configuration for persistent services and garbage collectionphp spark worker:install- Generates Caddyfile and worker entry pointphp spark worker:uninstall- Removes worker mode filesOptimizations
Database:
BaseConnection::ping()method for connection health checkspg_ping(), other drivers useSELECT 1DatabaseConfig::validateForWorkerMode()- validates connections at request startDatabaseConfig::cleanupForWorkerMode()- detects uncommitted transactions and rolls backSession handlers:
PersistsConnectiontrait for Redis and Memcached handlersServices and state management:
Boot::bootWorker()- one-time initialization for worker modeCodeIgniter::resetForWorkerMode()- resets request-specific stateServices::resetForWorkerMode()- resets non-persistent services between requestsServices::validateForWorkerMode()- validates cache connectionsEvents::cleanupForWorkerMode()- clears event performance dataDebug Toolbar:
Toolbar::reset()- resets collectors between requests (development only)No impact on traditional PHP-FPM
All worker mode methods are only called from the worker entry point (
public/frankenphp-worker.php). When running in traditional PHP-FPM mode viapublic/index.php, none of this code is executed. The changes introduce zero performance overhead for applications not using worker mode.Benchmarks
I also prepared a set of simple benchmarks to evaluate worker mode performance: https://github.com/michalsn/benchmark-codeigniter-frankenphp
Based on existing benchmarks, nginx with PHP-FPM should perform similarly to FrankenPHP running in classic mode. When I attempted to verify this, the results showed roughly 60% of classic mode performance. However, these tests were run in a local development environment that is not properly tuned, which likely skewed the results.
For this reason, I decided not to include the benchmark numbers for nginx with PHP-FPM. In a real-world, properly configured environment, nginx is expected to perform similarly to FrankenPHP in classic mode.
Checklist: