-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DoctrineBridge] Idle connection listener for long running runtime #53214
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
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', | ||
]; | ||
} | ||
} |
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']); | ||
} | ||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't comment on old, already merged PRs. If you feel strongly about this, send a PR instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, was just a general suggestion, though. |
||
->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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,12 @@ | |
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"> | ||
<arguments> | ||
<array> | ||
<element key="time-sensitive"><string>Symfony\Bridge\Doctrine\Middleware\Debug</string></element> | ||
<element key="time-sensitive"> | ||
<array> | ||
<element key="0"><string>Symfony\Bridge\Doctrine\Middleware\Debug</string></element> | ||
<element key="1"><string>Symfony\Bridge\Doctrine\Middleware\Debug</string></element> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicolas-grekas Isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, PR welcome There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 65ccca0 |
||
</array> | ||
</element> | ||
</array> | ||
</arguments> | ||
</listener> | ||
|
Uh oh!
There was an error while loading. Please reload this page.