Skip to content

Commit 08dfd40

Browse files
committed
Add support for when cluster endpoint is not available
Modify `get_cluster_info` to include a timeout (defaults to 3 seconds) and gracefully degrade when the ElastiCache cluster configuration endpoint isn't available by returning the provided `host` and `port` in the `nodes` list. I also added an additional test to the protocol test suite to cover the additional functionality.
1 parent 2084c43 commit 08dfd40

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

django_elasticache/cluster_utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, cmd, response):
1717
'Unexpected response {} for command {}'.format(response, cmd))
1818

1919

20-
def get_cluster_info(host, port):
20+
def get_cluster_info(host, port, timeout=3):
2121
"""
2222
return dict with info about nodes in cluster and current version
2323
{
@@ -30,7 +30,7 @@ def get_cluster_info(host, port):
3030
"""
3131
client = Telnet(host, int(port))
3232
client.write(b'version\n')
33-
res = client.read_until(b'\r\n').strip()
33+
res = client.read_until(b'\r\n', timeout).strip()
3434
version_list = res.split(b' ')
3535
if len(version_list) not in [2, 3] or version_list[0] != b'VERSION':
3636
raise WrongProtocolData('version', res)
@@ -40,8 +40,18 @@ def get_cluster_info(host, port):
4040
else:
4141
cmd = b'get AmazonElastiCache:cluster\n'
4242
client.write(cmd)
43-
res = client.read_until(b'\n\r\nEND\r\n')
43+
res = client.read_until(b'\n\r\nEND\r\n', timeout)
4444
client.close()
45+
46+
if res == 'ERROR\r\n':
47+
return {
48+
'version': version,
49+
'nodes': [
50+
'{}:{}'.format(smart_text(host),
51+
smart_text(port))
52+
]
53+
}
54+
4555
ls = list(filter(None, re.compile(br'\r?\n').split(res)))
4656
if len(ls) != 4:
4757
raise WrongProtocolData(cmd, res)

tests/test_protocol.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
2424
]
2525

26+
TEST_PROTOCOL_4 = [
27+
b'VERSION 1.4.34',
28+
b'ERROR\r\n',
29+
]
30+
2631

2732
@patch('django_elasticache.cluster_utils.Telnet')
2833
def test_happy_path(Telnet):
@@ -75,3 +80,16 @@ def test_ubuntu_protocol(Telnet):
7580
call(b'version\n'),
7681
call(b'config get cluster\n'),
7782
])
83+
84+
85+
@patch('django_elasticache.cluster_utils.Telnet')
86+
def test_no_configuration_protocol_support(Telnet):
87+
client = Telnet.return_value
88+
client.read_until.side_effect = TEST_PROTOCOL_4
89+
info = get_cluster_info('test', 0)
90+
client.write.assert_has_calls([
91+
call(b'version\n'),
92+
call(b'config get cluster\n'),
93+
])
94+
eq_(info['version'], '1.4.34')
95+
eq_(info['nodes'], ['test:0'])

0 commit comments

Comments
 (0)