Add DoctrinePingConnectionsHandler to check and reconnect connections before handling request#322
Add DoctrinePingConnectionsHandler to check and reconnect connections before handling request#322supersmile2009 wants to merge 5 commits into
Conversation
b96501a to
a72f371
Compare
|
Please add some description of why is it need, and what problem does it solve. Also, you have a typo in first commit: Does it solve the problems like https://github.com/k911/swoole-bundle/blob/develop/src/Bridge/Doctrine/ORM/EntityManagerHandler.php but in a more general way? |
Codecov Report
@@ Coverage Diff @@
## develop #322 +/- ##
=============================================
- Coverage 85.64% 85.06% -0.59%
- Complexity 644 659 +15
=============================================
Files 91 93 +2
Lines 2027 2082 +55
=============================================
+ Hits 1736 1771 +35
- Misses 291 311 +20
|
a72f371 to
ae2d0f3
Compare
|
Hi @k911
Commit message is fixed.
Nope. It checks that DB connection is alive before processing request, while EntityManagerHandler resets the internal state of the EntityManager after request is handled. BTW I have some improvements for the EntityManagerHandler too, I'll send a PR within a few days. |
@supersmile2009 I was referring to this part: https://github.com/k911/swoole-bundle/blob/develop/src/Bridge/Doctrine/ORM/EntityManagerHandler.php#L30-L33
And yes, please if there is a way to avoid adding deprecated features it would be great. |
ae2d0f3 to
6de386d
Compare
6de386d to
2cc9a70
Compare
|
Hi @k911. Old EntityManagerHandler isn't used and is marked as deprecated. Since it's not internal, it can't be removed without bumping a major version. When |
|
Code Climate has analyzed commit 2cc9a70 and detected 0 issues on this pull request. The test coverage on the diff in this pull request is 66.6% (50% is the threshold). This pull request will bring the total coverage in the repository to 85.1% (-0.5% change). View more on Code Climate. |
|
Hi @supersmile2009, thanks for PR and your hard work. I've run CI here and unfortunately I see two failures at least:
|
| ->defaultNull() | ||
| ->setDeprecated( | ||
| 'k911/swoole-bundle', | ||
| '0.8.4', |
There was a problem hiding this comment.
I can change that after merge, however it'll be at least 0.9.0 version. So just a note for me.
If workers stays idle for a long time period, connection to the DB may time out and get closed.
It's a fairly common practice to test a connection and attempt a reconnect if connection is dead before processing a message from the queue in long lived workers.
See examples:
https://github.com/php-enqueue/enqueue-dev/blob/master/pkg/enqueue-bundle/Consumption/Extension/DoctrinePingConnectionExtension.php
https://github.com/symfony/doctrine-bridge/blob/master/Messenger/DoctrinePingConnectionMiddleware.php
In this PR I suggest using the same approach when handling requests with Swoole.
You may notice that solution in symfony/doctrine-bridge is slightly different. It was changed recently because
Doctrine\DBAL\Connection::ping()had been deprecated.So technically this PR relies on the deprecated feature. But I believe by the time doctrine 3 is released with that feature removed we will see a much better solution in Symfony. The most reliable solution would probably be something like https://github.com/facile-it/doctrine-mysql-come-back - a connection wrapper, which reconnects on error. We may see some similar connection management solution in Symfony instead of current
pingmethod avoidance workaround by the time Doctrine 3 is released, so I wouldn't consider using this deprecated method a too bad solution for now (unless it's a huge lib/framework like Symfony where you want to keep things as future-proof as possible at all times).But if you don't want deprecated features to be used, I could change this PR to use the same solution as in symfony/doctrine-bridge.