Skip to content
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

[DoctrineBridge] Idle connection listener for long running runtime #53214

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@
<array>
<element key="0"><string>Cache\IntegrationTests</string></element>
<element key="1"><string>Symfony\Bridge\Doctrine\Middleware\Debug</string></element>
<element key="2"><string>Symfony\Component\Cache</string></element>
<element key="3"><string>Symfony\Component\Cache\Tests\Fixtures</string></element>
<element key="4"><string>Symfony\Component\Cache\Tests\Traits</string></element>
<element key="5"><string>Symfony\Component\Cache\Traits</string></element>
<element key="6"><string>Symfony\Component\Console</string></element>
<element key="7"><string>Symfony\Component\HttpFoundation</string></element>
<element key="8"><string>Symfony\Component\Uid</string></element>
<element key="2"><string>Symfony\Bridge\Doctrine\Middleware\IdleConnection</string></element>
<element key="3"><string>Symfony\Component\Cache</string></element>
<element key="4"><string>Symfony\Component\Cache\Tests\Fixtures</string></element>
<element key="5"><string>Symfony\Component\Cache\Tests\Traits</string></element>
<element key="6"><string>Symfony\Component\Cache\Traits</string></element>
<element key="7"><string>Symfony\Component\Console</string></element>
<element key="8"><string>Symfony\Component\HttpFoundation</string></element>
<element key="9"><string>Symfony\Component\Uid</string></element>
</array>
</element>
</array>
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Deprecate the `DoctrineExtractor::getTypes()` method, use `DoctrineExtractor::getType()` instead
* Allow `EntityValueResolver` to return a list of entities
* Add support for auto-closing idle connections

7.0
---
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Bridge/Doctrine/Middleware/IdleConnection/Driver.php
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 src/Symfony/Bridge/Doctrine/Middleware/IdleConnection/Listener.php
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()
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
{
$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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just do $this->createMock(ConnectionInterface::class) to spare two lines of code.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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);
}
}
7 changes: 6 additions & 1 deletion src/Symfony/Bridge/Doctrine/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas Isn't Symfony\Bridge\Doctrine\Middleware\IdleConnection?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, PR welcome

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 65ccca0

</array>
</element>
</array>
</arguments>
</listener>
Expand Down