From a9aa0128c10e706df2fbd0ab4378a6eb490251a9 Mon Sep 17 00:00:00 2001 From: ebrovcin Date: Sat, 22 Feb 2025 23:30:07 +0200 Subject: [PATCH] Add force_master_ip support to async Sentinel client --- redis/asyncio/sentinel.py | 10 +++++++++- tests/test_asyncio/conftest.py | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/redis/asyncio/sentinel.py b/redis/asyncio/sentinel.py index f1d2cab3f1..0389539fcf 100644 --- a/redis/asyncio/sentinel.py +++ b/redis/asyncio/sentinel.py @@ -198,6 +198,7 @@ def __init__( sentinels, min_other_sentinels=0, sentinel_kwargs=None, + force_master_ip=None, **connection_kwargs, ): # if sentinel_kwargs isn't defined, use the socket_* options from @@ -214,6 +215,7 @@ def __init__( ] self.min_other_sentinels = min_other_sentinels self.connection_kwargs = connection_kwargs + self._force_master_ip = force_master_ip async def execute_command(self, *args, **kwargs): """ @@ -277,7 +279,13 @@ async def discover_master(self, service_name: str): sentinel, self.sentinels[0], ) - return state["ip"], state["port"] + + ip = ( + self._force_master_ip + if self._force_master_ip is not None + else state["ip"] + ) + return ip, state["port"] error_info = "" if len(collected_errors) > 0: diff --git a/tests/test_asyncio/conftest.py b/tests/test_asyncio/conftest.py index fb6c51140e..f385778ed2 100644 --- a/tests/test_asyncio/conftest.py +++ b/tests/test_asyncio/conftest.py @@ -152,8 +152,10 @@ async def sentinel_setup(local_cache, request): for ip, port in (endpoint.split(":") for endpoint in sentinel_ips.split(",")) ] kwargs = request.param.get("kwargs", {}) if hasattr(request, "param") else {} + force_master_ip = request.param.get("force_master_ip", None) sentinel = Sentinel( sentinel_endpoints, + force_master_ip=force_master_ip, socket_timeout=0.1, client_cache=local_cache, protocol=3,