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 18, 2024
1 parent eadaa66 commit 87ac9a4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 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
36 changes: 16 additions & 20 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 @@ -202,13 +196,8 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
$connWithLogging = [];

Check failure on line 196 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 8 spaces but found 3 spaces
$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 6 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 6 spaces but found 1 space
$timingByConnection = [];
$skipTiming = false;

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

Check failure on line 200 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 4 spaces but found 1 space

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;
}

$timeToLiveByConnection[$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, $timeToLiveByConnection);

$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]);
}
}
}
30 changes: 30 additions & 0 deletions Middleware/ConnectionKeepAlive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Middleware;

use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
use Symfony\Bridge\Doctrine\ConnectionTimingWeakMap;

Check failure on line 7 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Type Symfony\Bridge\Doctrine\ConnectionTimingWeakMap is not used in this file.
use Symfony\Bridge\Doctrine\Middleware\ConnectionKeepAliveDriver;

class ConnectionKeepAlive implements Middleware, ConnectionNameAwareInterface
{
private \ArrayObject $connectionTimings;

Check failure on line 12 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Class \ArrayObject should not be referenced via a fully qualified name, but via a use statement.
private string $connectionName;

public function __construct(\ArrayObject $connectionTimings, string $connectionName = 'default')

Check failure on line 15 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Class \ArrayObject should not be referenced via a fully qualified name, but via a use statement.
{
$this->connectionName = $connectionName;

Check failure on line 17 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 4 spaces but found 1 space
$this->connectionTimings = $connectionTimings;
}

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

public function wrap(Driver $driver): Driver
{
return new ConnectionKeepAliveDriver($driver, $this->connectionTimings, $this->connectionName);

Check failure on line 28 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

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

<service id="doctrine.listeners.doctrine_connection_listener" class="Symfony\Bridge\Doctrine\Listener\ConnectionListener">
<argument type="service" id="service_container" />
<service id="doctrine.listeners.doctrine_connection_listener" class="Symfony\Bridge\Doctrine\DoctrineConnectionListener">
<argument type="service" id="connection.timing" />
<argument type="service" id="doctrine" />
<argument /> <!-- check timing -->
<tag name="kernel.event_subscriber" />
</service>
Expand Down
4 changes: 4 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="connection.timing" class="Symfony\Bridge\Doctrine\ConnectionTiming"/>
<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,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.timing" />
</service>
</services>
</container>

0 comments on commit 87ac9a4

Please sign in to comment.