diff --git a/oslo_cache/core.py b/oslo_cache/core.py index 1b9d9cdc..6fa67bbe 100644 --- a/oslo_cache/core.py +++ b/oslo_cache/core.py @@ -400,19 +400,12 @@ def _build_cache_config(conf: cfg.ConfigOpts) -> dict[str, Any]: 'dogpile.cache.redis', 'dogpile.cache.redis_sentinel', ): - socket_keepalive_options = { + conf_dict[f'{prefix}.arguments.socket_keepalive'] = True + conf_dict[f'{prefix}.arguments.socket_keepalive_options'] = { socket.TCP_KEEPIDLE: conf.cache.socket_keepalive_idle, socket.TCP_KEEPINTVL: conf.cache.socket_keepalive_interval, socket.TCP_KEEPCNT: conf.cache.socket_keepalive_count, } - conf_dict.setdefault( - f'{prefix}.arguments.connection_kwargs', {} - ).update( - { - 'socket_keepalive': True, - 'socket_keepalive_options': socket_keepalive_options, - } - ) else: raise exception.ConfigurationError( "Socket keepalive is not supported by the " diff --git a/oslo_cache/tests/unit/test_cache_basics.py b/oslo_cache/tests/unit/test_cache_basics.py index 4fb7906a..0ddf6d09 100644 --- a/oslo_cache/tests/unit/test_cache_basics.py +++ b/oslo_cache/tests/unit/test_cache_basics.py @@ -821,6 +821,10 @@ class CacheRegionTest(test_cache.BaseTestCase): self.assertEqual( 1.0, config_dict['test_prefix.arguments.socket_timeout'] ) + self.assertNotIn('test_prefix.arguments.socket_keepalive', config_dict) + self.assertNotIn( + 'test_prefix.arguments.socket_keepalive_options', config_dict + ) def test_cache_config_builder_redis_with_db(self): """Validate we build a sane dogpile.cache dictionary config.""" @@ -839,6 +843,10 @@ class CacheRegionTest(test_cache.BaseTestCase): self.assertEqual( 1.0, config_dict['test_prefix.arguments.socket_timeout'] ) + self.assertNotIn('test_prefix.arguments.socket_keepalive', config_dict) + self.assertNotIn( + 'test_prefix.arguments.socket_keepalive_options', config_dict + ) def test_cache_config_builder_redis_with_sock_to(self): """Validate we build a sane dogpile.cache dictionary config.""" @@ -857,6 +865,10 @@ class CacheRegionTest(test_cache.BaseTestCase): self.assertEqual( 10.0, config_dict['test_prefix.arguments.socket_timeout'] ) + self.assertNotIn('test_prefix.arguments.socket_keepalive', config_dict) + self.assertNotIn( + 'test_prefix.arguments.socket_keepalive_options', config_dict + ) def test_cache_config_builder_redis_with_keepalive(self): """Validate we build a sane dogpile.cache dictionary config.""" @@ -875,16 +887,14 @@ class CacheRegionTest(test_cache.BaseTestCase): self.assertEqual( 1.0, config_dict['test_prefix.arguments.socket_timeout'] ) + self.assertTrue(config_dict['test_prefix.arguments.socket_keepalive']) self.assertEqual( { - 'socket_keepalive': True, - 'socket_keepalive_options': { - socket.TCP_KEEPIDLE: 1, - socket.TCP_KEEPINTVL: 1, - socket.TCP_KEEPCNT: 1, - }, + socket.TCP_KEEPIDLE: 1, + socket.TCP_KEEPINTVL: 1, + socket.TCP_KEEPCNT: 1, }, - config_dict['test_prefix.arguments.connection_kwargs'], + config_dict['test_prefix.arguments.socket_keepalive_options'], ) def test_cache_config_builder_redis_with_keepalive_params(self): @@ -907,16 +917,14 @@ class CacheRegionTest(test_cache.BaseTestCase): self.assertEqual( 1.0, config_dict['test_prefix.arguments.socket_timeout'] ) + self.assertTrue(config_dict['test_prefix.arguments.socket_keepalive']) self.assertEqual( { - 'socket_keepalive': True, - 'socket_keepalive_options': { - socket.TCP_KEEPIDLE: 2, - socket.TCP_KEEPINTVL: 3, - socket.TCP_KEEPCNT: 4, - }, + socket.TCP_KEEPIDLE: 2, + socket.TCP_KEEPINTVL: 3, + socket.TCP_KEEPCNT: 4, }, - config_dict['test_prefix.arguments.connection_kwargs'], + config_dict['test_prefix.arguments.socket_keepalive_options'], ) def test_cache_config_builder_redis_with_auth(self): @@ -975,6 +983,10 @@ class CacheRegionTest(test_cache.BaseTestCase): self.assertEqual( 1.0, config_dict['test_prefix.arguments.socket_timeout'] ) + self.assertNotIn('test_prefix.arguments.socket_keepalive', config_dict) + self.assertNotIn( + 'test_prefix.arguments.socket_keepalive_options', config_dict + ) self.assertNotIn( 'test_prefix.arguments.connection_kwargs', config_dict ) @@ -1004,6 +1016,10 @@ class CacheRegionTest(test_cache.BaseTestCase): self.assertEqual( 1.0, config_dict['test_prefix.arguments.socket_timeout'] ) + self.assertNotIn('test_prefix.arguments.socket_keepalive', config_dict) + self.assertNotIn( + 'test_prefix.arguments.socket_keepalive_options', config_dict + ) self.assertNotIn( 'test_prefix.arguments.connection_kwargs', config_dict ) @@ -1033,6 +1049,10 @@ class CacheRegionTest(test_cache.BaseTestCase): self.assertEqual( 10.0, config_dict['test_prefix.arguments.socket_timeout'] ) + self.assertNotIn('test_prefix.arguments.socket_keepalive', config_dict) + self.assertNotIn( + 'test_prefix.arguments.socket_keepalive_options', config_dict + ) self.assertNotIn( 'test_prefix.arguments.connection_kwargs', config_dict )