2828 ClusterTopologyMonitor , ClusterTopologyMonitorImpl )
2929from aws_advanced_python_wrapper .utils .decorators import \
3030 preserve_transaction_status_with_timeout
31- from aws_advanced_python_wrapper .utils .sliding_expiration_cache import \
32- SlidingExpirationCacheWithCleanupThread
31+ from aws_advanced_python_wrapper .utils .sliding_expiration_cache_container import \
32+ SlidingExpirationCacheContainer
3333
3434if TYPE_CHECKING :
3535 from aws_advanced_python_wrapper .driver_dialect import DriverDialect
@@ -476,6 +476,7 @@ def __init__(self, dialect: db_dialect.TopologyAwareDatabaseDialect, props: Prop
476476
477477 self .instance_template : HostInfo = instance_template
478478 self ._max_timeout_sec = WrapperProperties .AUXILIARY_QUERY_TIMEOUT_SEC .get_int (props )
479+ self ._thread_pool = ThreadPoolContainer .get_thread_pool (self ._executor_name )
479480
480481 def _validate_host_pattern (self , host : str ):
481482 if not self ._rds_utils .is_dns_pattern_valid (host ):
@@ -507,7 +508,7 @@ def query_for_topology(
507508 an empty tuple will be returned.
508509 """
509510 query_for_topology_func_with_timeout = preserve_transaction_status_with_timeout (
510- ThreadPoolContainer . get_thread_pool ( self ._executor_name ) , self ._max_timeout_sec , driver_dialect , conn )(self ._query_for_topology )
511+ self ._thread_pool , self ._max_timeout_sec , driver_dialect , conn )(self ._query_for_topology )
511512 x = query_for_topology_func_with_timeout (conn )
512513 return x
513514
@@ -570,7 +571,7 @@ def create_host(
570571 def get_host_role (self , connection : Connection , driver_dialect : DriverDialect ) -> HostRole :
571572 try :
572573 cursor_execute_func_with_timeout = preserve_transaction_status_with_timeout (
573- ThreadPoolContainer . get_thread_pool ( self ._executor_name ) , self ._max_timeout_sec , driver_dialect , connection )(self ._get_host_role )
574+ self ._thread_pool , self ._max_timeout_sec , driver_dialect , connection )(self ._get_host_role )
574575 result = cursor_execute_func_with_timeout (connection )
575576 if result is not None :
576577 is_reader = result [0 ]
@@ -593,7 +594,7 @@ def get_host_id(self, connection: Connection, driver_dialect: DriverDialect) ->
593594 """
594595
595596 cursor_execute_func_with_timeout = preserve_transaction_status_with_timeout (
596- ThreadPoolContainer . get_thread_pool ( self ._executor_name ) , self ._max_timeout_sec , driver_dialect , connection )(self ._get_host_id )
597+ self ._thread_pool , self ._max_timeout_sec , driver_dialect , connection )(self ._get_host_id )
597598 result = cursor_execute_func_with_timeout (connection )
598599 if result :
599600 host_id : str = result [0 ]
@@ -608,7 +609,7 @@ def _get_host_id(self, conn: Connection):
608609 def get_writer_host_if_connected (self , connection : Connection , driver_dialect : DriverDialect ) -> Optional [str ]:
609610 try :
610611 cursor_execute_func_with_timeout = preserve_transaction_status_with_timeout (
611- ThreadPoolContainer . get_thread_pool ( self ._executor_name ) , self ._max_timeout_sec , driver_dialect , connection )(self ._get_writer_id )
612+ self ._thread_pool , self ._max_timeout_sec , driver_dialect , connection )(self ._get_writer_id )
612613 result = cursor_execute_func_with_timeout (connection )
613614 if result :
614615 host_id : str = result [0 ]
@@ -752,13 +753,9 @@ def _create_multi_az_host(self, record: Tuple, writer_id: str) -> HostInfo:
752753
753754
754755class MonitoringRdsHostListProvider (RdsHostListProvider ):
755- _CACHE_CLEANUP_NANO = 1 * 60 * 1_000_000_000 # 1 minute
756- _MONITOR_CLEANUP_NANO = 15 * 60 * 1_000_000_000 # 15 minutes
757-
758- _monitors : ClassVar [SlidingExpirationCacheWithCleanupThread [str , ClusterTopologyMonitor ]] = \
759- SlidingExpirationCacheWithCleanupThread (_CACHE_CLEANUP_NANO ,
760- should_dispose_func = lambda monitor : monitor .can_dispose (),
761- item_disposal_func = lambda monitor : monitor .close ())
756+ _CACHE_CLEANUP_NANO : ClassVar [int ] = 1 * 60 * 1_000_000_000 # 1 minute
757+ _MONITOR_CLEANUP_NANO : ClassVar [int ] = 15 * 60 * 1_000_000_000 # 15 minutes
758+ _MONITOR_CACHE_NAME : ClassVar [str ] = "cluster_topology_monitors"
762759
763760 def __init__ (
764761 self ,
@@ -772,6 +769,13 @@ def __init__(
772769 self ._high_refresh_rate_ns = (
773770 WrapperProperties .CLUSTER_TOPOLOGY_HIGH_REFRESH_RATE_MS .get_int (self ._props ) * 1_000_000 )
774771
772+ self ._monitors = SlidingExpirationCacheContainer .get_or_create_cache (
773+ name = MonitoringRdsHostListProvider ._MONITOR_CACHE_NAME ,
774+ cleanup_interval_ns = MonitoringRdsHostListProvider ._CACHE_CLEANUP_NANO ,
775+ should_dispose_func = lambda monitor : monitor .can_dispose (),
776+ item_disposal_func = lambda monitor : monitor .close ()
777+ )
778+
775779 def _get_monitor (self ) -> Optional [ClusterTopologyMonitor ]:
776780 return self ._monitors .compute_if_absent_with_disposal (self .get_cluster_id (),
777781 lambda k : ClusterTopologyMonitorImpl (
@@ -803,7 +807,3 @@ def force_monitoring_refresh(self, should_verify_writer: bool, timeout_sec: int)
803807 return ()
804808
805809 return monitor .force_refresh (should_verify_writer , timeout_sec )
806-
807- @staticmethod
808- def release_resources ():
809- MonitoringRdsHostListProvider ._monitors .clear ()
0 commit comments