dunglas / frankenphp

🧟 The modern PHP app server
https://frankenphp.dev
MIT License
6.94k stars 243 forks source link

[Feature request] Ability to periodically ping TCP connections in worker mode #233

Closed bpolaszek closed 6 months ago

bpolaszek commented 1 year ago

Hey there,

I tried playing with FrankenPHP a little (in worker mode) and I was wondering if it exposes some PHP API to hook into the worker process.

My main concern is that in worker mode, FrankenPHP receives a first request which performs some DB queries, thus opening a DB connection which will be reused for further requests. Except, no traffic occurs for a while, and when comes the time for the worker to process another request, the DB connection is gone, resulting in the app throwing an exception (it may reconnect at this moment of course, but it has a cost).

In ReactPHP world, this could be easily handled by some snippet like this:

use React\EventLoop\Loop;

// Ensure connection remains open
Loop::addPeriodicTimer(10, fn () => $db->query('SELECT 1'));

Is there something similar in FrankenPHP? Or is there something planned about this in the near future?

(Side note: it's not really an "issue", maybe you could enable Discussions on this repository ?)

DubbleClick commented 1 year ago

I think it's acceptable to handle this at the start of a request. If the connection has gone stale, you could recreate it. Shouldn't happen during active hours and if it were to happen once every thousand requests, it wouldn't be perceptible anyway.

A symfony bundle for roadrunner handles it with a ping at the start of a request: https://github.com/Baldinof/roadrunner-bundle/blob/3.x/src/Integration/Doctrine/DoctrineORMMiddleware.php#L53

That being said, integrations of all kind would be a nice thing.

bpolaszek commented 1 year ago

Yes, probably - maybe that's something that documentation should tell, because PHP developers are rarely faced with that kind of issues, and with FrankenPhp this is likely to happen. By default Doctrine just crashes and no requests can be served anymore.

DubbleClick commented 1 year ago

PHP developers are rarely faced with that kind of issues

Hah, imagine my confusion when I took over a php project and realised that a third party server is firing up my code and killing the process after! Definitely the biggest contrast to every other language I've been using.

dunglas commented 11 months ago

We definitely need to fix this Doctrine behavior at least in Symfony (work in progress) and to improve the documentation to make it clear that similar issues can happen if devs aren't careful.

dunglas commented 6 months ago

The DB problem has been fixed by Symfony in https://github.com/symfony/symfony/pull/53214.

It's now possible to handle this kind of use case userland in the worker script it self by following @DubbleClick's strategy.