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 17, 2024
1 parent eadaa66 commit b4122a1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 22 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('check_connection_timing')->defaultValue(30)->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
34 changes: 15 additions & 19 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,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 @@ -203,12 +197,7 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
$connWithProfiling = [];

Check failure on line 197 in DependencyInjection/DoctrineExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
$connWithBacktrace = [];

Check failure on line 198 in DependencyInjection/DoctrineExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
$timingByConnection = [];

Check failure on line 199 in DependencyInjection/DoctrineExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
$skipTiming = false;

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

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

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

Check failure on line 215 in DependencyInjection/DoctrineExtension.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

InvalidArrayOffset

DependencyInjection/DoctrineExtension.php:215:17: InvalidArrayOffset: Cannot access value on variable $connection using offset value of 'check_connection_timing', expecting 'logging', 'profiling' or 'profiling_collect_backtrace' (see https://psalm.dev/115)
$connWithTimingCheck[] = $name;
}

$timingByConnection[$connections[$name]] = $connection['check_connection_timing'];

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

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

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

Expand All @@ -250,7 +239,7 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
}
});

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

/**
Expand Down Expand Up @@ -1201,7 +1190,8 @@ private function registerDbalMiddlewares(
ContainerBuilder $container,
array $connWithLogging,
array $connWithProfiling,
array $connWithBacktrace
array $connWithBacktrace,
array $connWithTimingCheck
): void {
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('middlewares.xml');
Expand All @@ -1217,5 +1207,11 @@ private function registerDbalMiddlewares(
$debugMiddlewareAbstractDef
->addTag('doctrine.middleware', ['connection' => $connName]);
}

$checkTimingMiddlewareAbstractDef = $container->getDefinition('doctrine.connection.keep.alive_middleware');
foreach ($connWithTimingCheck as $connName) {
$checkTimingMiddlewareAbstractDef
->addTag('doctrine.middleware', ['connection' => $connName, 'priority' => 10]);
}
}
}
52 changes: 52 additions & 0 deletions Middleware/ConnectionKeepAlive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Middleware;

use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\Middleware;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
use Symfony\Bridge\Doctrine\ConnectionTimingWeakMap;

class ConnectionKeepAlive implements Middleware, ConnectionNameAwareInterface
{
private ConnectionTimingWeakMap $connectionTimingWeakMap;

Check failure on line 13 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Middleware/ConnectionKeepAlive.php:13:13: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\ConnectionTimingWeakMap does not exist (see https://psalm.dev/019)
private string $connectionName;

public function __construct(ConnectionTimingWeakMap $connectionTimingWeakMap, string $connectionName = 'default')

Check failure on line 16 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Middleware/ConnectionKeepAlive.php:16:33: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\ConnectionTimingWeakMap does not exist (see https://psalm.dev/019)
{
$this->connectionName = $connectionName;

Check failure on line 18 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space
$this->connectionTimingWeakMap = $connectionTimingWeakMap;
}

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

public function wrap(Driver $driver): Driver
{
return new class($driver, $this->connectionTimingWeakMap, $this->connectionName) extends AbstractDriverMiddleware {

Check failure on line 29 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Expected 1 space after class keyword; 0 found
private Driver $driver;
private ConnectionTimingWeakMap $connectionTimingWeakMap;

Check failure on line 31 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Middleware/ConnectionKeepAlive.php:31:21: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\ConnectionTimingWeakMap does not exist (see https://psalm.dev/019)
private string $connectionName;

public function __construct(Driver $driver, ConnectionTimingWeakMap $connectionTimingWeakMap, string $connectionName = 'default')

Check failure on line 34 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Middleware/ConnectionKeepAlive.php:34:57: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\ConnectionTimingWeakMap does not exist (see https://psalm.dev/019)
{
$this->connectionName = $connectionName;

Check failure on line 36 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space
$this->connectionTimingWeakMap = $connectionTimingWeakMap;
$this->driver = $driver;

Check failure on line 38 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 18 spaces but found 1 space

parent::__construct($driver);
}

public function connect(array $params): DriverConnection

Check failure on line 43 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Method class@anonymous::connect() does not have @param annotation for its traversable parameter $params.
{
$connection = parent::connect($params);
$this->connectionTimingWeakMap->setTimingPerConnection($connection, $this->connectionName);

Check failure on line 46 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Middleware/ConnectionKeepAlive.php:46:17: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\ConnectionTimingWeakMap does not exist (see https://psalm.dev/019)

return $connection;
}
};
}
}
3 changes: 2 additions & 1 deletion Resources/config/dbal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
<tag name="controller.service_arguments" />
</service>

<service id="doctrine.listeners.doctrine_connection_listener" class="Symfony\Bridge\Doctrine\Listener\ConnectionListener">
<service id="doctrine.listeners.doctrine_connection_listener" class="Symfony\Bridge\Doctrine\ConnectionListener">
<argument type="service" id="connection.weak_map" />
<argument type="service" id="service_container" />
<argument /> <!-- check timing -->
<tag name="kernel.event_subscriber" />
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/middlewares.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
<argument type="service" id="doctrine.debug_data_holder" />
<argument type="service" id="debug.stopwatch" on-invalid="null" />
</service>
<service id="doctrine.connection.keep.alive_middleware" class="Doctrine\Bundle\DoctrineBundle\Middleware\ConnectionKeepAlive" abstract="true">
<argument type="service" id="connection.weak_map" />
</service>
</services>
</container>

0 comments on commit b4122a1

Please sign in to comment.