Skip to content

Commit

Permalink
take into account call to db
Browse files Browse the repository at this point in the history
  • Loading branch information
alli83 committed Apr 30, 2024
1 parent eadaa66 commit 0a44ab6
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 37 deletions.
3 changes: 1 addition & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,9 @@ private function getDbalConnectionsNode(): ArrayNodeDefinition
->end()
->booleanNode('disable_type_comments')->end()
->scalarNode('server_version')->end()
->integerNode('check_connection_timing')->end()
->integerNode('idle_connection_ttl')->defaultValue(600)->end()
->scalarNode('driver_class')->end()
->scalarNode('wrapper_class')->end()
->scalarNode('check_connection_frequency')->defaultValue(30)->end()
->booleanNode('keep_slave')
->setDeprecated(
'doctrine/doctrine-bundle',
Expand Down
48 changes: 26 additions & 22 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Bridge\Doctrine\Middleware\IdleConnection\Listener;
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
use Symfony\Bridge\Doctrine\SchemaListener\DoctrineDbalCacheAdapterSchemaListener;
use Symfony\Bridge\Doctrine\SchemaListener\LockStoreSchemaListener;
Expand Down Expand Up @@ -83,7 +84,7 @@
*
* @final since 2.9
* @psalm-type DBALConfig = array{
* connections: array<string, array{logging: bool, profiling: bool, profiling_collect_backtrace: bool}>,
* connections: array<string, array{logging: bool, profiling: bool, profiling_collect_backtrace: bool, idle_connection_ttl: int}>,
* driver_schemes: array<string, string>,
* default_connection: string,
* types: array<string, string>,
Expand Down Expand Up @@ -174,12 +175,6 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
$config['default_connection'] = reset($keys);
}

if (! $container->hasParameter('kernel.runtime_mode') && ! $container->hasParameter('kernel.runtime_mode.worker')) {
$container->removeDefinition('doctrine.listeners.doctrine_connection_listener');
} else {
$container->getDefinition('doctrine.orm.listeners.doctrine_connection_listener')->setArgument(1, $config['check_connection_timing']);
}

$this->defaultConnection = $config['default_connection'];

$container->setAlias('database_connection', sprintf('doctrine.dbal.%s_connection', $this->defaultConnection));
Expand All @@ -202,13 +197,8 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
$connWithLogging = [];
$connWithProfiling = [];
$connWithBacktrace = [];
$timingByConnection = [];
$skipTiming = false;

if (! $container->hasParameter('kernel.runtime_mode') && ! $container->hasParameter('kernel.runtime_mode.worker')) {
$container->removeDefinition('doctrine.listeners.doctrine_connection_listener');
$skipTiming = true;
}
$ttlByConnection = [];
$connWithTtl = [];

foreach ($config['connections'] as $name => $connection) {
if ($connection['logging']) {
Expand All @@ -223,17 +213,14 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
}
}

if (! $skipTiming) {
$timingByConnection[] = ['name' => $connections[$name], 'timing' => $connection['check_connection_timing']];
if ($connection['idle_connection_ttl'] > 0) {
$connWithTtl[] = $name;
$ttlByConnection[$name] = $connection['idle_connection_ttl'];
}

$this->loadDbalConnection($name, $connection, $container);
}

if (! $skipTiming) {
$container->getDefinition('doctrine.orm.listeners.doctrine_connection_listener')->setArgument(1, $timingByConnection);
}

$container->registerForAutoconfiguration(MiddlewareInterface::class)->addTag('doctrine.middleware');

$container->registerAttributeForAutoconfiguration(AsMiddleware::class, static function (ChildDefinition $definition, AsMiddleware $attribute) {
Expand All @@ -250,7 +237,16 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
}
});

$this->registerDbalMiddlewares($container, $connWithLogging, $connWithProfiling, $connWithBacktrace);
$this->registerDbalMiddlewares($container, $connWithLogging, $connWithProfiling, $connWithBacktrace, $connWithTtl);

$container->getDefinition('doctrine.dbal.idle_connection_middleware')->setArgument(1, $ttlByConnection);

if (class_exists(Listener::class)) {
return;
}

$container->removeDefinition('doctrine.dbal.idle_connection_listener');
$container->removeDefinition('doctrine.dbal.idle_connection_middleware');
}

/**
Expand Down Expand Up @@ -1196,12 +1192,14 @@ private function createArrayAdapterCachePool(ContainerBuilder $container, string
* @param string[] $connWithLogging
* @param string[] $connWithProfiling
* @param string[] $connWithBacktrace
* @param string[] $connWithTtl
*/
private function registerDbalMiddlewares(
ContainerBuilder $container,
array $connWithLogging,
array $connWithProfiling,
array $connWithBacktrace
array $connWithBacktrace,
array $connWithTtl
): void {
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('middlewares.xml');
Expand All @@ -1217,5 +1215,11 @@ private function registerDbalMiddlewares(
$debugMiddlewareAbstractDef
->addTag('doctrine.middleware', ['connection' => $connName]);
}

$idleConnectionMiddlewareAbstractDef = $container->getDefinition('doctrine.dbal.idle_connection_middleware');
foreach ($connWithTtl as $connName) {
$idleConnectionMiddlewareAbstractDef
->addTag('doctrine.middleware', ['connection' => $connName, 'priority' => 10]);
}
}
}
36 changes: 36 additions & 0 deletions Middleware/IdleConnectionMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Middleware;

use ArrayObject;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
use Symfony\Bridge\Doctrine\Middleware\IdleConnection\Driver as IdleConnectionDriver;

class IdleConnectionMiddleware implements Middleware, ConnectionNameAwareInterface
{
private ArrayObject $connectionExpiries;
/** @var array<string, int> */
private array $ttlByConnection;
private string $connectionName;

/**
* @param ArrayObject<string, int> $connectionExpiries
* @param array<string, int> $ttlByConnection
*/
public function __construct(ArrayObject $connectionExpiries, array $ttlByConnection)
{
$this->connectionExpiries = $connectionExpiries;
$this->ttlByConnection = $ttlByConnection;
}

public function setConnectionName(string $name): void
{
$this->connectionName = $name;
}

public function wrap(Driver $driver): Driver
{
return new IdleConnectionDriver($driver, $this->connectionExpiries, $this->ttlByConnection[$this->connectionName], $this->connectionName);

Check failure on line 34 in Middleware/IdleConnectionMiddleware.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Middleware/IdleConnectionMiddleware.php:34:20: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\Middleware\IdleConnection\Driver does not exist (see https://psalm.dev/019)
}
}
4 changes: 2 additions & 2 deletions Resources/config/dbal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@
<tag name="controller.service_arguments" />
</service>

<service id="doctrine.listeners.doctrine_connection_listener" class="Symfony\Bridge\Doctrine\Listener\ConnectionListener">
<service id="doctrine.dbal.idle_connection_listener" class="Symfony\Bridge\Doctrine\Middleware\IdleConnection\Listener">
<argument type="service" id="doctrine.dbal.connection_expiries" />
<argument type="service" id="service_container" />
<argument /> <!-- check timing -->
<tag name="kernel.event_subscriber" />
</service>

Expand Down
5 changes: 5 additions & 0 deletions Resources/config/middlewares.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="doctrine.dbal.connection_expiries" class="ArrayObject"/>
<service id="doctrine.dbal.logging_middleware" class="Doctrine\DBAL\Logging\Middleware" abstract="true">
<argument type="service" id="logger" />
<tag name="monolog.logger" channel="doctrine" />
Expand All @@ -17,5 +18,9 @@
<argument type="service" id="doctrine.debug_data_holder" />
<argument type="service" id="debug.stopwatch" on-invalid="null" />
</service>
<service id="doctrine.dbal.idle_connection_middleware" class="Doctrine\Bundle\DoctrineBundle\Middleware\IdleConnectionMiddleware" abstract="true">
<argument type="service" id="doctrine.dbal.connection_expiries" />
<argument /> <!-- check timing -->
</service>
</services>
</container>
4 changes: 4 additions & 0 deletions Tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public function testDbalLoadSinglePrimaryReplicaConnection(): void
'host' => 'localhost',
'unix_socket' => '/path/to/mysqld.sock',
'driverOptions' => [PDO::ATTR_STRINGIFY_FETCHES => 1],
'idle_connection_ttl' => 600,
],
$param['primary'],
);
Expand Down Expand Up @@ -336,6 +337,7 @@ public function testLoadSimpleSingleConnection(): void
'driver' => 'pdo_mysql',
'driverOptions' => [],
'defaultTableOptions' => [],
'idle_connection_ttl' => 600,
],
new Reference('doctrine.dbal.default_connection.configuration'),
method_exists(Connection::class, 'getEventManager')
Expand Down Expand Up @@ -375,6 +377,7 @@ public function testLoadSimpleSingleConnectionWithoutDbName(): void
'driver' => 'pdo_mysql',
'driverOptions' => [],
'defaultTableOptions' => [],
'idle_connection_ttl' => 600,
],
new Reference('doctrine.dbal.default_connection.configuration'),
method_exists(Connection::class, 'getEventManager')
Expand Down Expand Up @@ -414,6 +417,7 @@ public function testLoadSingleConnection(): void
'dbname' => 'sqlite_db',
'memory' => true,
'defaultTableOptions' => [],
'idle_connection_ttl' => 600,
],
new Reference('doctrine.dbal.default_connection.configuration'),
method_exists(Connection::class, 'getEventManager')
Expand Down
44 changes: 33 additions & 11 deletions Tests/DependencyInjection/Compiler/MiddlewarePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\MiddlewaresPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Doctrine\Bundle\DoctrineBundle\Middleware\ConnectionNameAwareInterface;
use Doctrine\Bundle\DoctrineBundle\Middleware\IdleConnectionMiddleware;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Bridge\Doctrine\Middleware\IdleConnection\Listener;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand Down Expand Up @@ -170,7 +172,8 @@ public function testAddMiddlewareOrderingWithDefaultPriority(): void

$this->assertMiddlewareInjected($container, 'conn1', PHP7Middleware::class);
$this->assertMiddlewareInjected($container, 'conn1', ConnectionAwarePHP7Middleware::class, true);
$this->assertMiddlewareOrdering($container, 'conn1', [PHP7Middleware::class, ConnectionAwarePHP7Middleware::class]);
$expectedMiddlewares = class_exists(Listener::class) ? [IdleConnectionMiddleware::class, PHP7Middleware::class, ConnectionAwarePHP7Middleware::class] : [PHP7Middleware::class, ConnectionAwarePHP7Middleware::class];

Check failure on line 175 in Tests/DependencyInjection/Compiler/MiddlewarePassTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Function class_exists() should not be referenced via a fallback global name, but via a use statement.
$this->assertMiddlewareOrdering($container, 'conn1', $expectedMiddlewares);
}

public function testAddMiddlewareOrderingWithExplicitPriority(): void
Expand All @@ -193,7 +196,8 @@ public function testAddMiddlewareOrderingWithExplicitPriority(): void

$this->assertMiddlewareInjected($container, 'conn1', PHP7Middleware::class);
$this->assertMiddlewareInjected($container, 'conn1', ConnectionAwarePHP7Middleware::class, true);
$this->assertMiddlewareOrdering($container, 'conn1', [ConnectionAwarePHP7Middleware::class, PHP7Middleware::class]);
$expectedMiddlewares = class_exists(Listener::class) ? [IdleConnectionMiddleware::class, ConnectionAwarePHP7Middleware::class, PHP7Middleware::class] : [ConnectionAwarePHP7Middleware::class, PHP7Middleware::class];

Check failure on line 199 in Tests/DependencyInjection/Compiler/MiddlewarePassTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Function class_exists() should not be referenced via a fallback global name, but via a use statement.
$this->assertMiddlewareOrdering($container, 'conn1', $expectedMiddlewares);
}

public function testAddMiddlewareOrderingWithExplicitPriorityAndConnection(): void
Expand Down Expand Up @@ -222,7 +226,8 @@ public function testAddMiddlewareOrderingWithExplicitPriorityAndConnection(): vo
$this->assertMiddlewareInjected($container, 'conn1', ConnectionAwarePHP7Middleware::class, true);
$this->assertMiddlewareInjected($container, 'conn2', PHP7Middleware::class);
$this->assertMiddlewareNotInjected($container, 'conn2', ConnectionAwarePHP7Middleware::class);
$this->assertMiddlewareOrdering($container, 'conn1', [ConnectionAwarePHP7Middleware::class, PHP7Middleware::class]);
$expectedMiddlewares = class_exists(Listener::class) ? [IdleConnectionMiddleware::class, ConnectionAwarePHP7Middleware::class, PHP7Middleware::class] : [ConnectionAwarePHP7Middleware::class, PHP7Middleware::class];

Check failure on line 229 in Tests/DependencyInjection/Compiler/MiddlewarePassTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Function class_exists() should not be referenced via a fallback global name, but via a use statement.
$this->assertMiddlewareOrdering($container, 'conn1', $expectedMiddlewares);
}

public function testAddMiddlewareOrderingWithExplicitPriorityPerConnection(): void
Expand Down Expand Up @@ -252,8 +257,10 @@ public function testAddMiddlewareOrderingWithExplicitPriorityPerConnection(): vo
$this->assertMiddlewareInjected($container, 'conn1', ConnectionAwarePHP7Middleware::class, true);
$this->assertMiddlewareInjected($container, 'conn2', PHP7Middleware::class);
$this->assertMiddlewareInjected($container, 'conn2', ConnectionAwarePHP7Middleware::class, true);
$this->assertMiddlewareOrdering($container, 'conn1', [ConnectionAwarePHP7Middleware::class, PHP7Middleware::class]);
$this->assertMiddlewareOrdering($container, 'conn2', [PHP7Middleware::class, ConnectionAwarePHP7Middleware::class]);
$expectedMiddlewares = class_exists(Listener::class) ? [IdleConnectionMiddleware::class, ConnectionAwarePHP7Middleware::class, PHP7Middleware::class] : [ConnectionAwarePHP7Middleware::class, PHP7Middleware::class];

Check failure on line 260 in Tests/DependencyInjection/Compiler/MiddlewarePassTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Function class_exists() should not be referenced via a fallback global name, but via a use statement.
$this->assertMiddlewareOrdering($container, 'conn1', $expectedMiddlewares);
$expectedMiddlewares = class_exists(Listener::class) ? [IdleConnectionMiddleware::class, PHP7Middleware::class, ConnectionAwarePHP7Middleware::class] : [PHP7Middleware::class, ConnectionAwarePHP7Middleware::class];

Check failure on line 262 in Tests/DependencyInjection/Compiler/MiddlewarePassTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Function class_exists() should not be referenced via a fallback global name, but via a use statement.
$this->assertMiddlewareOrdering($container, 'conn2', $expectedMiddlewares);
}

public function testAddMiddlewareOrderingWithInheritedPriorityPerConnection(): void
Expand Down Expand Up @@ -292,8 +299,10 @@ public function testAddMiddlewareOrderingWithInheritedPriorityPerConnection(): v
$this->assertMiddlewareInjected($container, 'conn2', PHP7Middleware::class);
$this->assertMiddlewareNotInjected($container, 'conn2', ConnectionAwarePHP7Middleware::class);
$this->assertMiddlewareInjected($container, 'conn2', 'some_middleware_class');
$this->assertMiddlewareOrdering($container, 'conn1', [ConnectionAwarePHP7Middleware::class, 'some_middleware_class', PHP7Middleware::class]);
$this->assertMiddlewareOrdering($container, 'conn2', [PHP7Middleware::class, 'some_middleware_class']);
$expectedMiddlewares = class_exists(Listener::class) ? [IdleConnectionMiddleware::class, ConnectionAwarePHP7Middleware::class, 'some_middleware_class', PHP7Middleware::class] : [ConnectionAwarePHP7Middleware::class, 'some_middleware_class', PHP7Middleware::class];

Check failure on line 302 in Tests/DependencyInjection/Compiler/MiddlewarePassTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Function class_exists() should not be referenced via a fallback global name, but via a use statement.
$this->assertMiddlewareOrdering($container, 'conn1', $expectedMiddlewares);
$expectedMiddlewares = class_exists(Listener::class) ? [IdleConnectionMiddleware::class, PHP7Middleware::class, 'some_middleware_class'] : [PHP7Middleware::class, 'some_middleware_class'];

Check failure on line 304 in Tests/DependencyInjection/Compiler/MiddlewarePassTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Function class_exists() should not be referenced via a fallback global name, but via a use statement.
$this->assertMiddlewareOrdering($container, 'conn2', $expectedMiddlewares);
}

/** @requires PHP 8 */
Expand Down Expand Up @@ -327,15 +336,28 @@ public function testAddMiddlewareOrderingWithAttributeForAutoconfiguration(): vo
$this->assertMiddlewareInjected($container, 'conn2', AutoconfiguredMiddleware::class);
$this->assertMiddlewareInjected($container, 'conn2', AutoconfiguredMiddlewareWithConnection::class);
$this->assertMiddlewareInjected($container, 'conn2', AutoconfiguredMiddlewareWithPriority::class);
$this->assertMiddlewareOrdering($container, 'conn1', [
$expectedMiddlewares = class_exists(Listener::class) ? [

Check failure on line 339 in Tests/DependencyInjection/Compiler/MiddlewarePassTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Function class_exists() should not be referenced via a fallback global name, but via a use statement.
IdleConnectionMiddleware::class,
AutoconfiguredMiddlewareWithPriority::class,
AutoconfiguredMiddleware::class,
]);
$this->assertMiddlewareOrdering($container, 'conn2', [
] :
[
AutoconfiguredMiddlewareWithPriority::class,
AutoconfiguredMiddleware::class,
];
$this->assertMiddlewareOrdering($container, 'conn1', $expectedMiddlewares);
$expectedMiddlewares = class_exists(Listener::class) ? [

Check failure on line 349 in Tests/DependencyInjection/Compiler/MiddlewarePassTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Function class_exists() should not be referenced via a fallback global name, but via a use statement.
IdleConnectionMiddleware::class,
AutoconfiguredMiddlewareWithPriority::class,
AutoconfiguredMiddleware::class,
AutoconfiguredMiddlewareWithConnection::class,
]);
] :
[
AutoconfiguredMiddlewareWithPriority::class,
AutoconfiguredMiddleware::class,
AutoconfiguredMiddlewareWithConnection::class,
];
$this->assertMiddlewareOrdering($container, 'conn2', $expectedMiddlewares);
}

private function createContainer(callable $func, bool $addConnections = true): ContainerBuilder
Expand Down
Loading

0 comments on commit 0a44ab6

Please sign in to comment.