Skip to content

Commit

Permalink
Redis Transport: Small improvements of SentinelChannel (#1253)
Browse files Browse the repository at this point in the history
* Redis Sentinel connection string now supports default sentinel port

* Raise ValueError when SentinelChannel is missing master_name transport option
  • Loading branch information
matusvalo authored Sep 24, 2020
1 parent f475285 commit faa1d86
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion kombu/transport/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,8 @@ def _sentinel_managed_pool(self, asynchronous=False):
for url in self.connection.client.alt:
url = _parse_url(url)
if url.scheme == 'sentinel':
sentinels.append((url.hostname, url.port))
port = url.port or self.connection.default_port
sentinels.append((url.hostname, port))

# Fallback for when only one sentinel is provided.
if not sentinels:
Expand All @@ -1148,6 +1149,11 @@ def _sentinel_managed_pool(self, asynchronous=False):

master_name = getattr(self, 'master_name', None)

if master_name is None:
raise ValueError(
"'master_name' transport option must be specified."
)

return sentinel_inst.master_for(
master_name,
self.Client,
Expand Down
12 changes: 12 additions & 0 deletions t/unit/transport/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,7 @@ def test_method_called(self):
def test_getting_master_from_sentinel(self):
with patch('redis.sentinel.Sentinel') as patched:
connection = Connection(
'sentinel://localhost/;'
'sentinel://localhost:65532/;'
'sentinel://user@localhost:65533/;'
'sentinel://:password@localhost:65534/;'
Expand All @@ -1422,6 +1423,7 @@ def test_getting_master_from_sentinel(self):
connection.channel()
patched.assert_called_once_with(
[
('localhost', 26379),
('localhost', 65532),
('localhost', 65533),
('localhost', 65534),
Expand Down Expand Up @@ -1472,3 +1474,13 @@ def test_can_create_connection(self):
)
with pytest.raises(ConnectionError):
connection.channel()

def test_missing_master_name_transport_option(self):
connection = Connection(
'sentinel://localhost:65534/',
)
with patch('redis.sentinel.Sentinel'), \
pytest.raises(ValueError) as excinfo:
connection.connect()
expected = "'master_name' transport option must be specified."
assert expected == excinfo.value.args[0]

0 comments on commit faa1d86

Please sign in to comment.