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 19, 2024
1 parent 63b1b8a commit 7093f41
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 55 deletions.

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;
}
}
56 changes: 56 additions & 0 deletions src/Symfony/Bridge/Doctrine/Middleware/IdleConnection/Listener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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
{
$connectionExpiries = $this->connectionExpiries->getIterator();

$timestamp = time();

foreach ($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
}

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

public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}

0 comments on commit 7093f41

Please sign in to comment.