@@ -243,14 +243,24 @@ def re_auth(self):
243243
244244 @abstractmethod
245245 def mark_for_reconnect (self ):
246+ """
247+ Mark the connection to be reconnected on the next command.
248+ This is useful when a connection is moved to a different node.
249+ """
246250 pass
247251
248252 @abstractmethod
249253 def should_reconnect (self ):
254+ """
255+ Returns True if the connection should be reconnected.
256+ """
250257 pass
251258
252259 @abstractmethod
253260 def reset_should_reconnect (self ):
261+ """
262+ Reset the internal flag to False.
263+ """
254264 pass
255265
256266
@@ -1560,71 +1570,61 @@ def _get_socket(self) -> Optional[socket.socket]:
15601570 "Maintenance notifications are not supported by this connection type"
15611571 )
15621572
1563- @property
1564- def maintenance_state (self ) -> MaintenanceState :
1565- if isinstance (self ._conn , MaintNotificationsAbstractConnection ):
1566- return self ._conn .maintenance_state
1567- else :
1573+ def _get_maint_notifications_connection_instance (
1574+ self , connection
1575+ ) -> MaintNotificationsAbstractConnection :
1576+ """
1577+ Validate that connection instance supports maintenance notifications.
1578+ With this helper method we ensure that we are working
1579+ with the correct connection type.
1580+ After twe validate that connection instance supports maintenance notifications
1581+ we can safely return the connection instance
1582+ as MaintNotificationsAbstractConnection.
1583+ """
1584+ if not isinstance (connection , MaintNotificationsAbstractConnection ):
15681585 raise NotImplementedError (
15691586 "Maintenance notifications are not supported by this connection type"
15701587 )
1588+ else :
1589+ return connection
1590+
1591+ @property
1592+ def maintenance_state (self ) -> MaintenanceState :
1593+ con = self ._get_maint_notifications_connection_instance (self ._conn )
1594+ return con .maintenance_state
15711595
15721596 @maintenance_state .setter
15731597 def maintenance_state (self , state : MaintenanceState ):
1574- if isinstance (self ._conn , MaintNotificationsAbstractConnection ):
1575- self ._conn .maintenance_state = state
1576- else :
1577- raise NotImplementedError (
1578- "Maintenance notifications are not supported by this connection type"
1579- )
1598+ con = self ._get_maint_notifications_connection_instance (self ._conn )
1599+ con .maintenance_state = state
15801600
15811601 def getpeername (self ):
1582- if isinstance (self ._conn , MaintNotificationsAbstractConnection ):
1583- return self ._conn .getpeername ()
1584- else :
1585- raise NotImplementedError (
1586- "Maintenance notifications are not supported by this connection type"
1587- )
1602+ con = self ._get_maint_notifications_connection_instance (self ._conn )
1603+ return con .getpeername ()
15881604
15891605 def get_resolved_ip (self ):
1590- if isinstance (self ._conn , MaintNotificationsAbstractConnection ):
1591- return self ._conn .get_resolved_ip ()
1592- else :
1593- raise NotImplementedError (
1594- "Maintenance notifications are not supported by this connection type"
1595- )
1606+ con = self ._get_maint_notifications_connection_instance (self ._conn )
1607+ return con .get_resolved_ip ()
15961608
15971609 def update_current_socket_timeout (self , relaxed_timeout : Optional [float ] = None ):
1598- if isinstance (self ._conn , MaintNotificationsAbstractConnection ):
1599- self ._conn .update_current_socket_timeout (relaxed_timeout )
1600- else :
1601- raise NotImplementedError (
1602- "Maintenance notifications are not supported by this connection type"
1603- )
1610+ con = self ._get_maint_notifications_connection_instance (self ._conn )
1611+ con .update_current_socket_timeout (relaxed_timeout )
16041612
16051613 def set_tmp_settings (
16061614 self ,
16071615 tmp_host_address : Optional [str ] = None ,
16081616 tmp_relaxed_timeout : Optional [float ] = None ,
16091617 ):
1610- if isinstance (self ._conn , MaintNotificationsAbstractConnection ):
1611- self ._conn .set_tmp_settings (tmp_host_address , tmp_relaxed_timeout )
1612- else :
1613- raise NotImplementedError (
1614- "Maintenance notifications are not supported by this connection type"
1615- )
1618+ con = self ._get_maint_notifications_connection_instance (self ._conn )
1619+ con .set_tmp_settings (tmp_host_address , tmp_relaxed_timeout )
16161620
16171621 def reset_tmp_settings (
16181622 self ,
16191623 reset_host_address : bool = False ,
16201624 reset_relaxed_timeout : bool = False ,
16211625 ):
1622- if isinstance (self ._conn , MaintNotificationsAbstractConnection ):
1623- self ._conn .reset_tmp_settings (reset_host_address , reset_relaxed_timeout )
1624- else :
1625- raise NotImplementedError (
1626- "Maintenance notifications are not supported by this connection type"
1627- )
1626+ con = self ._get_maint_notifications_connection_instance (self ._conn )
1627+ con .reset_tmp_settings (reset_host_address , reset_relaxed_timeout )
16281628
16291629 def _connect (self ):
16301630 self ._conn ._connect ()
0 commit comments