frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: ZealPHP – Dangers Isolating Request-State in Coroutines?

2•sibidharan•47m ago
I built ZealPHP (https://php.zeal.ninja), a runtime experiment with OpenSwoole - to isolate what leaks between coroutines. The idea is to bring back the good old PHP coding style, but with coroutines, making async a default safe choice.

Github: https://github.com/sibidharan/zealphp

The goal is not to replace Laravel or Symfony. The goal is to replace the execution model of PHP application servers.

OpenSwoole provides the coroutine runtime, HTTP/WebSocket server, workers, timers shared memory, workers etc. ZealPHP adds the application layer: routing, middlewares, sessions, request-scoped superglobals, templating, file-based APIs, isolation-contract, SSE+WS, yield anywhere and some CLI helpers.

A new route can be just a few lines instead of writing apps around raw Openswoole server callbacks. And the old code, just works with 97% Apache+mod_php parity from our weighted assessment. So you have document root and everything where the files are bring served from, you can config everything that you can do via Apache and Nginx. So now PHP is the HTTP server, not a worker that dies.

The hard migration problem is traditional PHP APIs like header(), session_start(), $_GET, $_SESSION etc assume request-local state, while long running coroutines server has many concurrent requests inside one process, that causes leaks. It's mitigated via a custom built PHP extension: ext-zealphp, purpose built to isolate state across coroutine context switches.

The extension is doing the heavy lifting, please look at the isolation contract: https://github.com/sibidharan/ext-zealphp - it binds with on_resume, on_yield, on_close events from Openswoole and performs the isolation

Does it look solid? Whatever mentioned in the Github works now! We still deal with op-cache corruption while testing with Wordpress in coroutines mode, which I am actively working to fix. Thats the hardest, it requires help from upstream at the moment.

However, the framework is useable: One `php app.php` can give you HTTP, WS, SSE, shared memory, task workers from same application server, no seperate Node.js sidecars. The app runs in many modes and the experimental mode "coroutine-legacy" attempts to run the old PHP mental model code in coroutines mode from document root. Of course there are limitations I am trying to solve actively.

I also created a Symfony bridge, CGI compatibility mode for existing apps. I have also created lessons for HN to try: https://php.zeal.ninja/learn

It is alpha now. If you have run long-lived PHP with Swoole/OpenSwoole, Octane, FrankenPHP, RR or ReactPHP, what broke first in production??

Is there any other existing frameworks that does this kind of request-state isolation?