Class: Apartment::PoolManager
- Inherits:
-
Object
- Object
- Apartment::PoolManager
- Defined in:
- lib/apartment/pool_manager.rb
Instance Method Summary collapse
-
#clear ⇒ Object
Disconnect all pools before clearing to prevent connection leaks.
- #evict_by_role(role) ⇒ Object
-
#fetch_or_create(tenant_key) ⇒ Object
Fetch an existing pool or create one via the block.
- #get(tenant_key) ⇒ Object
- #idle_tenants(timeout:) ⇒ Object
-
#initialize ⇒ PoolManager
constructor
A new instance of PoolManager.
- #lru_tenants(count:) ⇒ Object
-
#remove(tenant_key) ⇒ Object
Delete pool first, then timestamp.
- #remove_tenant(tenant) ⇒ Object
-
#stats ⇒ Object
Basic stats.
-
#stats_for(tenant_key) ⇒ Object
Returns stats for a tenant pool.
- #tracked?(tenant_key) ⇒ Boolean
Constructor Details
#initialize ⇒ PoolManager
Returns a new instance of PoolManager.
7 8 9 10 |
# File 'lib/apartment/pool_manager.rb', line 7 def initialize @pools = Concurrent::Map.new @timestamps = Concurrent::Map.new end |
Instance Method Details
#clear ⇒ Object
Disconnect all pools before clearing to prevent connection leaks. Each pool’s disconnect! is individually rescued so one broken pool doesn’t prevent cleanup of others.
95 96 97 98 99 100 101 102 103 |
# File 'lib/apartment/pool_manager.rb', line 95 def clear @pools.each_pair do |key, pool| pool.disconnect! if pool.respond_to?(:disconnect!) rescue StandardError => e warn "[Apartment::PoolManager] Failed to disconnect pool '#{key}': #{e.class}: #{e.}" end @pools.clear @timestamps.clear end |
#evict_by_role(role) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/apartment/pool_manager.rb', line 46 def evict_by_role(role) suffix = ":#{role}" removed = [] @pools.each_key do |key| next unless key.end_with?(suffix) pool = remove(key) removed << [key, pool] if pool end removed end |
#fetch_or_create(tenant_key) ⇒ Object
Fetch an existing pool or create one via the block. Timestamp is updated after pool creation to avoid orphaned timestamps if the block raises.
14 15 16 17 18 |
# File 'lib/apartment/pool_manager.rb', line 14 def fetch_or_create(tenant_key, &) pool = @pools.compute_if_absent(tenant_key, &) touch(tenant_key) pool end |
#get(tenant_key) ⇒ Object
20 21 22 23 24 |
# File 'lib/apartment/pool_manager.rb', line 20 def get(tenant_key) pool = @pools[tenant_key] touch(tenant_key) if pool pool end |
#idle_tenants(timeout:) ⇒ Object
71 72 73 74 |
# File 'lib/apartment/pool_manager.rb', line 71 def idle_tenants(timeout:) cutoff = monotonic_now - timeout @timestamps.each_pair.filter_map { |key, ts| key if ts < cutoff } end |
#lru_tenants(count:) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/apartment/pool_manager.rb', line 76 def lru_tenants(count:) @timestamps.each_pair .sort_by { |_, ts| ts } .first(count) .map(&:first) end |
#remove(tenant_key) ⇒ Object
Delete pool first, then timestamp. This ordering prevents a concurrent #get from orphaning a timestamp (get checks @pools, skips touch if absent).
28 29 30 31 32 |
# File 'lib/apartment/pool_manager.rb', line 28 def remove(tenant_key) pool = @pools.delete(tenant_key) @timestamps.delete(tenant_key) pool end |
#remove_tenant(tenant) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/apartment/pool_manager.rb', line 34 def remove_tenant(tenant) prefix = "#{tenant}:" removed = [] @pools.each_key do |key| next unless key.start_with?(prefix) pool = remove(key) removed << [key, pool] if pool end removed end |
#stats ⇒ Object
Basic stats. Full observability (per-tenant breakdown, connection counts, eviction counters) deferred to Phase 3.
85 86 87 88 89 90 |
# File 'lib/apartment/pool_manager.rb', line 85 def stats { total_pools: @pools.size, tenants: @pools.keys, } end |
#stats_for(tenant_key) ⇒ Object
Returns stats for a tenant pool. Follows ActiveRecord’s convention of exposing computed durations (seconds_idle) rather than raw monotonic timestamps, which are meaningless outside the process.
65 66 67 68 69 |
# File 'lib/apartment/pool_manager.rb', line 65 def stats_for(tenant_key) return nil unless tracked?(tenant_key) { seconds_idle: monotonic_now - @timestamps[tenant_key] } end |
#tracked?(tenant_key) ⇒ Boolean
58 59 60 |
# File 'lib/apartment/pool_manager.rb', line 58 def tracked?(tenant_key) @pools.key?(tenant_key) end |