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.
-
#each_pair ⇒ Object
Yields each tracked pool as [tenant_key, pool].
- #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
-
#peek(tenant_key) ⇒ Object
Read a pool without updating its idle timestamp.
-
#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.
110 111 112 113 114 115 116 117 118 |
# File 'lib/apartment/pool_manager.rb', line 110 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 |
#each_pair ⇒ Object
Yields each tracked pool as [tenant_key, pool]. Snapshot semantics follow Concurrent::Map#each_pair: keys observed during iteration are those present at the time the iterator visits them. Read-only; do not mutate the manager from inside the block.
103 104 105 |
# File 'lib/apartment/pool_manager.rb', line 103 def each_pair(&) @pools.each_pair(&) end |
#evict_by_role(role) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/apartment/pool_manager.rb', line 53 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
78 79 80 81 |
# File 'lib/apartment/pool_manager.rb', line 78 def idle_tenants(timeout:) cutoff = monotonic_now - timeout @timestamps.each_pair.filter_map { |key, ts| key if ts < cutoff } end |
#lru_tenants(count:) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/apartment/pool_manager.rb', line 83 def lru_tenants(count:) @timestamps.each_pair .sort_by { |_, ts| ts } .first(count) .map(&:first) end |
#peek(tenant_key) ⇒ Object
Read a pool without updating its idle timestamp. PoolReaper uses this to inspect an eviction candidate; get would reset the very idleness the reaper is measuring.
29 30 31 |
# File 'lib/apartment/pool_manager.rb', line 29 def peek(tenant_key) @pools[tenant_key] 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).
35 36 37 38 39 |
# File 'lib/apartment/pool_manager.rb', line 35 def remove(tenant_key) pool = @pools.delete(tenant_key) @timestamps.delete(tenant_key) pool end |
#remove_tenant(tenant) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/apartment/pool_manager.rb', line 41 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.
92 93 94 95 96 97 |
# File 'lib/apartment/pool_manager.rb', line 92 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.
72 73 74 75 76 |
# File 'lib/apartment/pool_manager.rb', line 72 def stats_for(tenant_key) return nil unless tracked?(tenant_key) { seconds_idle: monotonic_now - @timestamps[tenant_key] } end |
#tracked?(tenant_key) ⇒ Boolean
65 66 67 |
# File 'lib/apartment/pool_manager.rb', line 65 def tracked?(tenant_key) @pools.key?(tenant_key) end |