Skip to content

Commit f0899a0

Browse files
authored
Merge pull request gusdan#12 from uncovertruth/feature/ignore_cluster_errors
add ignore_cluster_errors params
2 parents 0bcfa72 + ebd2a61 commit f0899a0

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Your cache backend should look something like this::
5252
'OPTIONS': {
5353
'cluster_timeout': 1, # its used when get cluster info
5454
'ignore_exc': True, # pymemcache Client params
55+
'ignore_cluster_errors': True, # ignore get cluster info error
5556
}
5657
}
5758
}

django_elastipymemcache/cluster_utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ def __init__(self, cmd, response):
1919
'Unexpected response {} for command {}'.format(response, cmd))
2020

2121

22-
def get_cluster_info(host, port, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
22+
def get_cluster_info(
23+
host,
24+
port,
25+
ignore_cluster_errors=False,
26+
timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
2327
"""
2428
return dict with info about nodes in cluster and current version
2529
{
@@ -48,6 +52,14 @@ def get_cluster_info(host, port, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
4852
])
4953
client.close()
5054

55+
if res == b'ERROR\r\n' and ignore_cluster_errors:
56+
return {
57+
'version': version,
58+
'nodes': [
59+
(smart_text(host), int(port))
60+
]
61+
}
62+
5163
ls = list(filter(None, re.compile(br'\r?\n').split(res)))
5264
if len(ls) != 4:
5365
raise WrongProtocolData(cmd, res)

django_elastipymemcache/memcached.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def __init__(self, server, params):
7070
self._options = self._options or dict()
7171
self._cluster_timeout = self._options.get(
7272
'cluster_timeout', socket._GLOBAL_DEFAULT_TIMEOUT)
73+
self._ignore_cluster_errors = self._options.get(
74+
'ignore_cluster_errors', False)
7375

7476
def clear_cluster_nodes_cache(self):
7577
"""clear internal cache with list of nodes in cluster"""
@@ -85,6 +87,7 @@ def get_cluster_nodes(self):
8587
return get_cluster_info(
8688
server,
8789
port,
90+
self._ignore_cluster_errors,
8891
self._cluster_timeout
8992
)['nodes']
9093
except (OSError, socket.gaierror, socket.timeout) as err:
@@ -105,6 +108,7 @@ def _cache(self):
105108
options['deserializer'] = deserialize_pickle
106109
options.setdefault('ignore_exc', True)
107110
options.pop('cluster_timeout', None)
111+
options.pop('ignore_cluster_errors', None)
108112

109113
self._client = self._lib.Client(
110114
self.get_cluster_nodes(), **options)

tests/test_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_split_servers(get_cluster_info):
3131
backend._lib.Client = Mock()
3232
assert backend._cache
3333
get_cluster_info.assert_called_once_with(
34-
'h', '0', socket._GLOBAL_DEFAULT_TIMEOUT)
34+
'h', '0', False, socket._GLOBAL_DEFAULT_TIMEOUT)
3535
backend._lib.Client.assert_called_once_with(
3636
servers,
3737
deserializer=deserialize_pickle,
@@ -69,7 +69,7 @@ def test_node_info_cache(get_cluster_info):
6969
eq_(backend._cache.set.call_count, 2)
7070

7171
get_cluster_info.assert_called_once_with(
72-
'h', '0', socket._GLOBAL_DEFAULT_TIMEOUT)
72+
'h', '0', False, socket._GLOBAL_DEFAULT_TIMEOUT)
7373

7474

7575
@patch('django.conf.settings', global_settings)

0 commit comments

Comments
 (0)