Permalink
Browse files

reuse connection if already connected to a server in the cluster

  • Loading branch information...
1 parent cfcd77f commit b2a4856e119a17eea0ef8cc508cf7925ea42bc34 @salimane committed Dec 2, 2012
Showing with 34 additions and 31 deletions.
  1. +30 −25 rediscluster/cluster_client.py
  2. +4 −6 tests/cluster_commands.py
View
55 rediscluster/cluster_client.py
@@ -89,38 +89,43 @@ def __init__(self, cluster={}, db=0):
self.cluster = cluster
self.no_servers = len(cluster['master_of'])
- slaves = dictvalues(cluster['master_of'])
self.redises = {}
+ redises_cons = {}
+
# connect to all servers
for alias, server in iteritems(cluster['nodes']):
- info = {}
- try:
- self.__redis = redis.StrictRedis(db=db, **server)
- info = self.__redis.info()
- if alias in slaves and info['role'] == 'master':
- raise redis.DataError(
- "rediscluster: server %s is not a slave." % (server,))
- except Exception as e:
- # if node is slave and is down, replace its connection with its master's
+ server_str = str(server)
+ if server_str in redises_cons:
+ self.redises[alias] = redises_cons[server_str]
+ else:
+ info = {}
try:
- ms = [k for k, v in iteritems(cluster['master_of'])
- if v == alias and (('role' in info and info['role'] == 'slave') or cluster['nodes'][k] == cluster['nodes'][v])][0]
+ ms = [k for k, v in iteritems(
+ cluster['master_of']) if v == alias][0]
except IndexError:
ms = None
+ try:
+ self.__redis = redis.StrictRedis(db=db, **server)
+ info = self.__redis.info()
+ if ms is not None and info['role'] == 'master' and cluster['nodes'][ms] is not cluster['nodes'][alias]:
+ raise redis.DataError(
+ "rediscluster: server %s is not a slave." % (server,))
+ except redis.RedisError as e:
+ # if node is slave and is down, replace its connection with its master's
+ if ms is not None and (('role' in info and info['role'] == 'slave') or cluster['nodes'][ms] == cluster['nodes'][alias]):
+ try:
+ self.__redis = redis.StrictRedis(
+ db=db, **cluster['nodes'][ms])
+ self.__redis.info()
+ except redis.RedisError as e:
+ raise redis.ConnectionError("rediscluster cannot connect to: %s %s" % (cluster['nodes'][ms], e))
- if ms is not None:
- try:
- self.__redis = redis.StrictRedis(
- db=db, **cluster['nodes'][ms])
- self.__redis.info()
- except Exception as e:
- raise redis.ConnectionError("rediscluster cannot connect to: %s %s" % (cluster['nodes'][ms], e))
-
- else:
- raise redis.ConnectionError(
- "rediscluster cannot connect to: %s %s" % (server, e))
+ else:
+ raise redis.ConnectionError(
+ "rediscluster cannot connect to: %s %s" % (server, e))
- self.redises[alias] = self.__redis
+ self.redises[alias] = self.__redis
+ redises_cons[server_str] = self.__redis
def __getattr__(self, name, *args, **kwargs):
"""
@@ -136,7 +141,7 @@ def function(*args, **kwargs):
# since we don't have "first item" in dict,
# this list is needed in order to check hash_tag in mset({"a{a}": "a", "b":"b"})
list_ht = []
- if isinstance(args[0], basestring) or isinstance(args[0], bytes):
+ if isinstance(args[0], (basestring, bytes)):
key_type = 'string'
list_ht.append(args[0])
else:
View
10 tests/cluster_commands.py
@@ -45,10 +45,9 @@ def test_dbsize(self):
sizeno = 0
sizehash = {}
for node, size in iteritems(self.client.dbsize()):
- if size and ((self.client.cluster['nodes'][node]['host'] + str(self.client.cluster['nodes'][node]['port'])) not in sizehash):
+ if size and not(str(self.client.cluster['nodes'][node]) in sizehash):
sizeno += size
- sizehash[self.client.cluster['nodes'][node]['host']
- + str(self.client.cluster['nodes'][node]['port'])] = size
+ sizehash[str(self.client.cluster['nodes'][node])] = size
self.assertEquals(sizeno, 2 * len(sizehash))
def test_getnodefor(self):
@@ -144,10 +143,9 @@ def test_info(self):
self.assert_(isinstance(info, dict))
try:
k = info['db4']['keys']
- if k and ((self.client.cluster['nodes'][node]['host'] + str(self.client.cluster['nodes'][node]['port'])) not in knohash):
+ if k and str(self.client.cluster['nodes'][node]) not in knohash:
kno += k
- knohash[self.client.cluster['nodes'][node]['host']
- + str(self.client.cluster['nodes'][node]['port'])] = k
+ knohash[str(self.client.cluster['nodes'][node])] = k
except KeyError:
pass
self.assertEquals(kno, 2 * len(knohash))

0 comments on commit b2a4856

Please sign in to comment.