redis: Use native interfaces to configure socket keepalive

... which were added in dogpile.cache 1.3.2 [1].

[1] 1614416f53

Change-Id: Idd30ebe37c45de1aa69cd2b2ce398f442d9febc2
Signed-off-by: Takashi Kajinami <kajinamit@oss.nttdata.com>
This commit is contained in:
Takashi Kajinami
2026-01-26 12:06:21 +09:00
parent 90efeb2df5
commit 54cde900bb
2 changed files with 36 additions and 23 deletions

View File

@@ -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 "

View File

@@ -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
)