-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #53214 [DoctrineBridge] Idle connection listener for long run…
…ning runtime (alli83) This PR was merged into the 7.1 branch. Discussion ---------- [DoctrineBridge] Idle connection listener for long running runtime | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #51661 | License | MIT This pull request introduces a solution based on the RoadRunner bundle's Doctrine ORM/ODM middleware https://github.com/Baldinof/roadrunner-bundle/blob/3.x/src/Integration/Doctrine/DoctrineORMMiddleware.php#L22. It checks the status of Doctrine connection, then if the connection is initialized and connected, it performs a 'ping' to check its viability. If the ping fails, it closes the connection. linked to doctrine/DoctrineBundle#1739 Commits ------- f7cc44e [DoctrineBridge] Idle connection listener for long running runtime
- Loading branch information
Showing
7 changed files
with
192 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Symfony/Bridge/Doctrine/Middleware/IdleConnection/Driver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Middleware\IdleConnection; | ||
|
||
use Doctrine\DBAL\Driver as DriverInterface; | ||
use Doctrine\DBAL\Driver\Connection as ConnectionInterface; | ||
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware; | ||
|
||
final class Driver extends AbstractDriverMiddleware | ||
{ | ||
public function __construct( | ||
DriverInterface $driver, | ||
private \ArrayObject $connectionExpiries, | ||
private readonly int $ttl, | ||
private readonly string $connectionName, | ||
) { | ||
parent::__construct($driver); | ||
} | ||
|
||
public function connect(array $params): ConnectionInterface | ||
{ | ||
$timestamp = time(); | ||
$connection = parent::connect($params); | ||
$this->connectionExpiries[$this->connectionName] = $timestamp + $this->ttl; | ||
|
||
return $connection; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Symfony/Bridge/Doctrine/Middleware/IdleConnection/Listener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Middleware\IdleConnection; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
final class Listener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @param \ArrayObject<string, int> $connectionExpiries | ||
*/ | ||
public function __construct( | ||
private readonly \ArrayObject $connectionExpiries, | ||
private ContainerInterface $container, | ||
) { | ||
} | ||
|
||
public function onKernelRequest(RequestEvent $event): void | ||
{ | ||
$timestamp = time(); | ||
|
||
foreach ($this->connectionExpiries as $name => $expiry) { | ||
if ($timestamp >= $expiry) { | ||
// unset before so that we won't retry in case of any failure | ||
$this->connectionExpiries->offsetUnset($name); | ||
|
||
try { | ||
$connection = $this->container->get("doctrine.dbal.{$name}_connection"); | ||
$connection->close(); | ||
} catch (\Exception) { | ||
// ignore exceptions to remain fail-safe | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
KernelEvents::REQUEST => 'onKernelRequest', | ||
]; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Symfony/Bridge/Doctrine/Tests/Middleware/IdleConnection/DriverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\Middleware\IdleConnection; | ||
|
||
use Doctrine\DBAL\Driver as DriverInterface; | ||
use Doctrine\DBAL\Driver\Connection as ConnectionInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Doctrine\Middleware\IdleConnection\Driver; | ||
|
||
class DriverTest extends TestCase | ||
{ | ||
/** | ||
* @group time-sensitive | ||
*/ | ||
public function testConnect() | ||
{ | ||
$driverMock = $this->createMock(DriverInterface::class); | ||
$connectionMock = $this->createMock(ConnectionInterface::class); | ||
|
||
$driverMock->expects($this->once()) | ||
->method('connect') | ||
->willReturn($connectionMock); | ||
|
||
$connectionExpiries = new \ArrayObject(); | ||
|
||
$driver = new Driver($driverMock, $connectionExpiries, 60, 'default'); | ||
$connection = $driver->connect([]); | ||
|
||
$this->assertSame($connectionMock, $connection); | ||
$this->assertArrayHasKey('default', $connectionExpiries); | ||
$this->assertSame(time() + 60, $connectionExpiries['default']); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Bridge/Doctrine/Tests/Middleware/IdleConnection/ListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Middleware\IdleConnection; | ||
|
||
use Doctrine\DBAL\Connection as ConnectionInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Doctrine\Middleware\IdleConnection\Listener; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
|
||
class ListenerTest extends TestCase | ||
{ | ||
public function testOnKernelRequest() | ||
{ | ||
$containerMock = $this->createMock(ContainerInterface::class); | ||
$connectionExpiries = new \ArrayObject(['connectionone' => time() - 30, 'connectiontwo' => time() + 40]); | ||
|
||
$connectionOneMock = $this->getMockBuilder(ConnectionInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$containerMock->expects($this->exactly(1)) | ||
->method('get') | ||
->with('doctrine.dbal.connectionone_connection') | ||
->willReturn($connectionOneMock); | ||
|
||
$listener = new Listener($connectionExpiries, $containerMock); | ||
|
||
$listener->onKernelRequest($this->createMock(RequestEvent::class)); | ||
|
||
$this->assertArrayNotHasKey('connectionone', (array) $connectionExpiries); | ||
$this->assertArrayHasKey('connectiontwo', (array) $connectionExpiries); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters