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 99ad155d0a..d9cccf1b92 100644 --- a/tests/test_asyncio/conftest.py +++ b/tests/test_asyncio/conftest.py @@ -151,8 +151,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,