@@ -3546,30 +3546,6 @@ class UserTypeDoesNotExist(Exception):
3546
3546
pass
3547
3547
3548
3548
3549
- class _ControlReconnectionHandler (_ReconnectionHandler ):
3550
- """
3551
- Internal
3552
- """
3553
-
3554
- def __init__ (self , control_connection , * args , ** kwargs ):
3555
- _ReconnectionHandler .__init__ (self , * args , ** kwargs )
3556
- self .control_connection = weakref .proxy (control_connection )
3557
-
3558
- def try_reconnect (self ):
3559
- return self .control_connection ._reconnect_internal ()
3560
-
3561
- def on_reconnection (self , connection ):
3562
- self .control_connection ._set_new_connection (connection )
3563
-
3564
- def on_exception (self , exc , next_delay ):
3565
- # TODO only overridden to add logging, so add logging
3566
- if isinstance (exc , AuthenticationFailed ):
3567
- return False
3568
- else :
3569
- log .debug ("Error trying to reconnect control connection: %r" , exc )
3570
- return True
3571
-
3572
-
3573
3549
def _watch_callback (obj_weakref , method_name , * args , ** kwargs ):
3574
3550
"""
3575
3551
A callback handler for the ControlConnection that tolerates
@@ -3662,6 +3638,7 @@ def __init__(self, cluster, timeout,
3662
3638
3663
3639
self ._reconnection_handler = None
3664
3640
self ._reconnection_lock = RLock ()
3641
+ self ._reconnection_pending = False
3665
3642
3666
3643
self ._event_schedule_times = {}
3667
3644
@@ -3818,33 +3795,43 @@ def reconnect(self):
3818
3795
if self ._is_shutdown :
3819
3796
return
3820
3797
3798
+ if self ._reconnection_pending :
3799
+ return
3800
+ self ._reconnection_pending = True
3801
+
3821
3802
self ._submit (self ._reconnect )
3822
3803
3823
- def _reconnect (self ):
3804
+ def _reconnect (self , schedule = None ):
3824
3805
log .debug ("[control connection] Attempting to reconnect" )
3806
+ if self ._is_shutdown :
3807
+ return
3808
+
3825
3809
try :
3826
3810
self ._set_new_connection (self ._reconnect_internal ())
3811
+ self ._reconnection_pending = False
3812
+ return
3827
3813
except NoHostAvailable :
3828
- # make a retry schedule (which includes backoff)
3829
- schedule = self ._cluster .reconnection_policy .new_schedule ()
3814
+ log .debug ("[control connection] Reconnection plan is exhausted, scheduling new reconnection attempt" )
3815
+ except Exception as ex :
3816
+ log .debug ("[control connection] Unexpected exception during reconnect, scheduling new reconnection attempt: %s" , ex )
3830
3817
3831
- with self ._reconnection_lock :
3818
+ if schedule is None :
3819
+ schedule = self ._cluster .reconnection_policy .new_schedule ()
3832
3820
3833
- # cancel existing reconnection attempts
3834
- if self ._reconnection_handler :
3835
- self ._reconnection_handler .cancel ()
3821
+ try :
3822
+ next_delay = next (schedule )
3823
+ except StopIteration :
3824
+ # the schedule has been exhausted
3825
+ schedule = self ._cluster .reconnection_policy .new_schedule ()
3826
+ try :
3827
+ next_delay = next (schedule )
3828
+ except StopIteration :
3829
+ next_delay = 0
3836
3830
3837
- # when a connection is successfully made, _set_new_connection
3838
- # will be called with the new connection and then our
3839
- # _reconnection_handler will be cleared out
3840
- self ._reconnection_handler = _ControlReconnectionHandler (
3841
- self , self ._cluster .scheduler , schedule ,
3842
- self ._get_and_set_reconnection_handler ,
3843
- new_handler = None )
3844
- self ._reconnection_handler .start ()
3845
- except Exception :
3846
- log .debug ("[control connection] error reconnecting" , exc_info = True )
3847
- raise
3831
+ if next_delay == 0 :
3832
+ self ._submit (self ._reconnect )
3833
+ else :
3834
+ self ._cluster .scheduler .schedule (next_delay , partial (self ._reconnect , schedule ))
3848
3835
3849
3836
def _get_and_set_reconnection_handler (self , new_handler ):
3850
3837
"""
0 commit comments