@@ -414,14 +414,25 @@ def __init__(
414414 stale data.
415415 When set to true, read commands will be assigned between the
416416 primary and its replications in a Round-Robin manner.
417- :cluster_error_retry_attempts: 'int'
417+ :cluster_error_retry_attempts: 'int'
418418 Retry command execution attempts when encountering ClusterDownError
419419 or ConnectionError
420- :retry_on_timeout: 'bool'
420+ :retry_on_timeout: 'bool'
421421 To specify a retry policy, first set `retry_on_timeout` to `True`
422422 then set `retry` to a valid `Retry` object
423- :retry: 'Retry'
423+ :retry: 'Retry'
424424 a `Retry` object
425+ :reinitialize_steps: 'int'
426+ Specifies the number of MOVED errors that need to occur before
427+ reinitializing the whole cluster topology. If a MOVED error occurs
428+ and the cluster does not need to be reinitialized on this current
429+ error handling, only the MOVED slot will be patched with the
430+ redirected node.
431+ To reinitialize the cluster on every MOVED error, set
432+ reinitialize_steps to 1.
433+ To avoid reinitializing the cluster on moved errors, set
434+ reinitialize_steps to 0.
435+
425436 :**kwargs:
426437 Extra arguments that will be sent into Redis instance when created
427438 (See Official redis-py doc for supported kwargs
@@ -727,7 +738,9 @@ def _determine_nodes(self, *args, **kwargs):
727738 return [node ]
728739
729740 def _should_reinitialized (self ):
730- # In order not to reinitialize the cluster, the user can set
741+ # To reinitialize the cluster on every MOVED error,
742+ # set reinitialize_steps to 1.
743+ # To avoid reinitializing the cluster on moved errors, set
731744 # reinitialize_steps to 0.
732745 if self .reinitialize_steps == 0 :
733746 return False
@@ -958,8 +971,8 @@ def _execute_command(self, target_node, *args, **kwargs):
958971 # redirected node output and try again. If MovedError exceeds
959972 # 'reinitialize_steps' number of times, we will force
960973 # reinitializing the tables, and then try again.
961- # 'reinitialize_steps' counter will increase faster when the
962- # same client object is shared between multiple threads. To
974+ # 'reinitialize_steps' counter will increase faster when
975+ # the same client object is shared between multiple threads. To
963976 # reduce the frequency you can set this variable in the
964977 # RedisCluster constructor.
965978 log .exception ("MovedError" )
@@ -1055,6 +1068,10 @@ def __repr__(self):
10551068 def __eq__ (self , obj ):
10561069 return isinstance (obj , ClusterNode ) and obj .name == self .name
10571070
1071+ def __del__ (self ):
1072+ if self .redis_connection is not None :
1073+ self .redis_connection .close ()
1074+
10581075
10591076class LoadBalancer :
10601077 """
@@ -1300,6 +1317,11 @@ def initialize(self):
13001317 startup_node .host , startup_node .port , ** copy_kwargs
13011318 )
13021319 self .startup_nodes [startup_node .name ].redis_connection = r
1320+ # Make sure cluster mode is enabled on this node
1321+ if bool (r .info ().get ("cluster_enabled" )) is False :
1322+ raise RedisClusterException (
1323+ "Cluster mode is not enabled on this node"
1324+ )
13031325 cluster_slots = r .execute_command ("CLUSTER SLOTS" )
13041326 startup_nodes_reachable = True
13051327 except (ConnectionError , TimeoutError ) as e :
@@ -1327,7 +1349,7 @@ def initialize(self):
13271349 message = e .__str__ ()
13281350 raise RedisClusterException (
13291351 'ERROR sending "cluster slots" command to redis '
1330- f"server: { startup_node } . error: { message } "
1352+ f"server { startup_node . name } . error: { message } "
13311353 )
13321354
13331355 # CLUSTER SLOTS command results in the following output:
0 commit comments