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 19, 2024
1 parent eadaa66 commit f567a16
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 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
37 changes: 16 additions & 21 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 = [];

foreach ($config['connections'] as $name => $connection) {
if ($connection['logging']) {
Expand All @@ -223,15 +212,13 @@ 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;
}

$this->loadDbalConnection($name, $connection, $container);
}
$timeToLiveByConnection[$name] = $connection['check_connection_timing'];

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

$container->registerForAutoconfiguration(MiddlewareInterface::class)->addTag('doctrine.middleware');
Expand All @@ -250,7 +237,8 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
}
});

$this->registerDbalMiddlewares($container, $connWithLogging, $connWithProfiling, $connWithBacktrace);
$this->registerDbalMiddlewares($container, $connWithLogging, $connWithProfiling, $connWithBacktrace, $connWithTimingCheck);
$container->getDefinition('doctrine.connection.keep.alive_middleware')->setArgument(1, $timeToLiveByConnection);
}

/**
Expand Down Expand Up @@ -1201,7 +1189,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 +1206,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]);
}
}
}
38 changes: 38 additions & 0 deletions Middleware/ConnectionKeepAlive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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 $connectionExpiries;

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;
private array $timeToLiveByConnection;

Check failure on line 14 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

@var annotation of property \Doctrine\Bundle\DoctrineBundle\Middleware\ConnectionKeepAlive::$timeToLiveByConnection does not specify type hint for its items.

/**

Check failure on line 16 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Found multi-line doc comment with single line content, use one-line doc comment instead.
* @param array<string, int> $timeToLiveByConnection
*/
public function __construct(
\ArrayObject $connectionExpiries,

Check failure on line 20 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.
array $timeToLiveByConnection,
string $connectionName = 'default'
) {
$this->timeToLiveByConnection = $timeToLiveByConnection;
$this->connectionName = $connectionName;

Check failure on line 25 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 9 spaces but found 1 space
$this->connectionExpiries = $connectionExpiries;

Check failure on line 26 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 5 spaces but found 1 space
}

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

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

Check failure on line 36 in Middleware/ConnectionKeepAlive.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm

UndefinedClass

Middleware/ConnectionKeepAlive.php:36:20: UndefinedClass: Class, interface or enum named Symfony\Bridge\Doctrine\Middleware\ConnectionKeepAliveDriver 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.listeners.doctrine_connection_listener" class="Symfony\Bridge\Doctrine\DoctrineConnectionListener">
<argument type="service" id="doctrine.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.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.connection.keep.alive_middleware" class="Doctrine\Bundle\DoctrineBundle\Middleware\ConnectionKeepAlive" abstract="true">
<argument type="service" id="doctrine.connection_expiries" />
<argument /> <!-- check timing -->
</service>
</services>
</container>

0 comments on commit f567a16

Please sign in to comment.