Skip to content

Commit

Permalink
[DoctrineBridge]: take into account call to db
Browse files Browse the repository at this point in the history
  • Loading branch information
alli83 committed Apr 24, 2024
1 parent c23efc5 commit b16e918
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 55 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<array>
<element key="0"><string>Cache\IntegrationTests</string></element>
<element key="1"><string>Symfony\Bridge\Doctrine\Middleware\Debug</string></element>
<element key="1"><string>Symfony\Bridge\Doctrine\Middleware\IdleConnection</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>
Expand Down

This file was deleted.

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;
}
}
54 changes: 54 additions & 0 deletions src/Symfony/Bridge/Doctrine/Middleware/IdleConnection/Listener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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) {
try {
$connection = $this->container->get("doctrine.dbal.{$name}_connection");
$connection->close();
} catch (\Exception) {
// ignore exceptions to remain fail-safe
}

$this->connectionExpiries->offsetUnset($name);
}
}
}

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)
->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);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Doctrine/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<arguments>
<array>
<element key="time-sensitive"><string>Symfony\Bridge\Doctrine\Middleware\Debug</string></element>
<element key="time-sensitive"><string>Symfony\Bridge\Doctrine\Middleware\IdleConnection</string></element>
</array>
</arguments>
</listener>
Expand Down

0 comments on commit b16e918

Please sign in to comment.