From 8741a5012e7b39e86bb6cbd84b231ef66378a8a2 Mon Sep 17 00:00:00 2001 From: Allison Guilhem Date: Wed, 17 Apr 2024 17:30:02 +1000 Subject: [PATCH] take into account call to db --- DependencyInjection/Configuration.php | 3 +- DependencyInjection/DoctrineExtension.php | 37 ++++++++++------------ Middleware/ConnectionKeepAlive.php | 38 +++++++++++++++++++++++ Resources/config/dbal.xml | 4 +-- Resources/config/middlewares.xml | 4 +++ 5 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 Middleware/ConnectionKeepAlive.php diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 68e40fc1..65e4259c 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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', diff --git a/DependencyInjection/DoctrineExtension.php b/DependencyInjection/DoctrineExtension.php index f08570e8..03d447e3 100644 --- a/DependencyInjection/DoctrineExtension.php +++ b/DependencyInjection/DoctrineExtension.php @@ -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)); @@ -202,13 +196,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; - } + $timeToLiveByConnection = []; + $connWithTimingCheck = []; foreach ($config['connections'] as $name => $connection) { if ($connection['logging']) { @@ -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']) { + $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'); @@ -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')->setArguments([new \ArrayObject(), $timeToLiveByConnection]); } /** @@ -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'); @@ -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]); + } } } diff --git a/Middleware/ConnectionKeepAlive.php b/Middleware/ConnectionKeepAlive.php new file mode 100644 index 00000000..d8c0bbed --- /dev/null +++ b/Middleware/ConnectionKeepAlive.php @@ -0,0 +1,38 @@ + $timeToLiveByConnection + */ + public function __construct( + \ArrayObject $connectionExpiries, + array $timeToLiveByConnection, + string $connectionName = 'default' + ) { + $this->timeToLiveByConnection = $timeToLiveByConnection; + $this->connectionName = $connectionName; + $this->connectionExpiries = $connectionExpiries; + } + + 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); + } +} diff --git a/Resources/config/dbal.xml b/Resources/config/dbal.xml index 3057d3b3..ed55f57b 100644 --- a/Resources/config/dbal.xml +++ b/Resources/config/dbal.xml @@ -101,9 +101,9 @@ - + + - diff --git a/Resources/config/middlewares.xml b/Resources/config/middlewares.xml index d6bf92bc..4298c0d8 100644 --- a/Resources/config/middlewares.xml +++ b/Resources/config/middlewares.xml @@ -17,5 +17,9 @@ + + + +