2727if TYPE_CHECKING :
2828 from aws_advanced_python_wrapper .driver_dialect import DriverDialect
2929 from aws_advanced_python_wrapper .plugin_service import PluginService
30+ from aws_advanced_python_wrapper .hostinfo import HostInfo
3031 from aws_advanced_python_wrapper .pep249 import Connection
3132
3233 from aws_advanced_python_wrapper .utils .rds_url_type import RdsUrlType
3536from _weakrefset import WeakSet
3637
3738from aws_advanced_python_wrapper .errors import FailoverError
38- from aws_advanced_python_wrapper .hostinfo import HostInfo , HostRole
3939from aws_advanced_python_wrapper .pep249_methods import DbApiMethod
4040from aws_advanced_python_wrapper .plugin import Plugin , PluginFactory
4141from aws_advanced_python_wrapper .utils .log import Logger
@@ -83,10 +83,10 @@ def _prune_connections(cls):
8383 to_remove .append (conn )
8484 except Exception :
8585 pass
86-
86+
8787 for conn in to_remove :
8888 conn_set .discard (conn )
89-
89+
9090 # Remove empty connection sets
9191 if not conn_set :
9292 del cls ._opened_connections [host ]
@@ -105,8 +105,8 @@ def populate_opened_connection_set(self, host_info: HostInfo, conn: Connection):
105105 self ._track_connection (host_info .as_alias (), conn )
106106 return
107107
108- instance_endpoint : Optional [str ] = next (( alias for alias in aliases if self . _rds_utils . is_rds_instance ( self . _rds_utils . remove_port ( alias ))),
109- None )
108+ instance_endpoint : Optional [str ] = next (
109+ ( alias for alias in aliases if self . _rds_utils . is_rds_instance ( self . _rds_utils . remove_port ( alias ))), None )
110110 if not instance_endpoint :
111111 logger .debug ("OpenedConnectionTracker.UnableToPopulateOpenedConnectionSet" )
112112 return
@@ -143,12 +143,15 @@ def invalidate_all_connections(self, host_info: Optional[HostInfo] = None, host:
143143 self ._log_connection_set (instance_endpoint , connection_set )
144144 self ._invalidate_connections (connection_set )
145145
146- def remove_connection_tracking (self , host_info : HostInfo , connection : Connection ):
146+ def remove_connection_tracking (self , host_info : HostInfo , connection : Connection | None ):
147+ if not connection :
148+ return
149+
147150 if self ._rds_utils .is_rds_instance (host_info .host ):
148151 host = host_info .as_alias ()
149152 else :
150153 host = next ((alias for alias in host_info .as_aliases ()
151- if self ._rds_utils .is_rds_instance (self ._rds_utils .remove_port (alias ))), None )
154+ if self ._rds_utils .is_rds_instance (self ._rds_utils .remove_port (alias ))), "" )
152155
153156 if not host :
154157 return
@@ -168,7 +171,7 @@ def _task(connection_set: WeakSet):
168171 while connection_set is not None :
169172 try :
170173 conn_reference = connection_set .pop ()
171- except KeyError as e :
174+ except KeyError :
172175 # connection_set is empty
173176 # use KeyError instead of len() to determine whether connection_set is empty to prevent data race
174177 break
@@ -182,7 +185,6 @@ def _task(connection_set: WeakSet):
182185 # Swallow this exception, current connection should be useless anyway
183186 pass
184187
185-
186188 def _invalidate_connections (self , connection_set : WeakSet ):
187189 invalidate_connection_thread : Thread = Thread (daemon = True , target = self ._task ,
188190 args = [connection_set ]) # type: ignore
@@ -286,7 +288,8 @@ def execute(self, target: object, method_name: str, execute_func: Callable, *arg
286288
287289 except Exception as e :
288290 if isinstance (e , FailoverError ):
289- AuroraConnectionTrackerPlugin ._host_list_refresh_end_time_nano = perf_counter_ns () + AuroraConnectionTrackerPlugin ._TOPOLOGY_CHANGES_EXPECTED_TIME_NANO
291+ AuroraConnectionTrackerPlugin ._host_list_refresh_end_time_nano = (
292+ perf_counter_ns () + AuroraConnectionTrackerPlugin ._TOPOLOGY_CHANGES_EXPECTED_TIME_NANO )
290293 # Calling this method may effectively close/abort a current connection
291294 self ._check_writer_changed (True )
292295 raise
0 commit comments