Skip to content

Commit

Permalink
Merge pull request #45916 from eileencodes/reduce-calls-to-pool-configs
Browse files Browse the repository at this point in the history
Reduce calls to pool_configs
  • Loading branch information
eileencodes committed Sep 8, 2022
2 parents f9030fe + bc951b4 commit e24fbf7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ def connection_pool_list(role = nil)
end
alias :connection_pools :connection_pool_list

def each_connection_pool(role = nil, &block) # :nodoc:
role = nil if role == :all
return enum_for(__method__, role) unless block_given?

connection_name_to_pool_manager.each_value do |manager|
manager.each_pool_config(role) do |pool_config|
yield pool_config.pool
end
end
end

def establish_connection(config, owner_name: Base, role: ActiveRecord::Base.current_role, shard: Base.current_shard)
owner_name = StringConnectionName.new(config.to_s) if config.is_a?(Symbol)

Expand Down Expand Up @@ -162,7 +173,7 @@ def active_connections?(role = nil)
role = ActiveRecord::Base.current_role
end

connection_pool_list(role).any?(&:active_connection?)
each_connection_pool(role).any?(&:active_connection?)
end

# Returns any connections in use by the current thread back to the pool,
Expand All @@ -174,7 +185,7 @@ def clear_active_connections!(role = nil)
role = ActiveRecord::Base.current_role
end

connection_pool_list(role).each(&:release_connection)
each_connection_pool(role).each(&:release_connection)
end

# Clears the cache which maps classes.
Expand All @@ -186,7 +197,7 @@ def clear_reloadable_connections!(role = nil)
role = ActiveRecord::Base.current_role
end

connection_pool_list(role).each(&:clear_reloadable_connections!)
each_connection_pool(role).each(&:clear_reloadable_connections!)
end

def clear_all_connections!(role = nil)
Expand All @@ -195,7 +206,7 @@ def clear_all_connections!(role = nil)
role = ActiveRecord::Base.current_role
end

connection_pool_list(role).each(&:disconnect!)
each_connection_pool(role).each(&:disconnect!)
end

# Disconnects all currently idle connections.
Expand All @@ -207,7 +218,7 @@ def flush_idle_connections!(role = nil)
role = ActiveRecord::Base.current_role
end

connection_pool_list(role).each(&:flush!)
each_connection_pool(role).each(&:flush!)
end

# Locate the connection of the nearest super class. This can be an
Expand Down
10 changes: 10 additions & 0 deletions activerecord/lib/active_record/connection_adapters/pool_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ def pool_configs(role = nil)
end
end

def each_pool_config(role = nil, &block)
if role
@role_to_shard_mapping[role].each_value(&block)
else
@role_to_shard_mapping.each_value do |shard_map|
shard_map.each_value(&block)
end
end
end

def remove_role(role)
@role_to_shard_mapping.delete(role)
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/connection_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def connected_to?(role:, shard: ActiveRecord::Base.default_shard)

# Clears the query cache for all connections associated with the current thread.
def clear_query_caches_for_current_thread
connection_handler.connection_pool_list(:all).each do |pool|
connection_handler.each_connection_pool do |pool|
pool.connection.clear_query_cache if pool.active_connection?
end
end
Expand Down
6 changes: 2 additions & 4 deletions activerecord/lib/active_record/query_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ def uncached(&block)
end

def self.run
pools = []
pools.concat(ActiveRecord::Base.connection_handler.connection_pool_list(:all).reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! })
pools
ActiveRecord::Base.connection_handler.each_connection_pool.reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! }
end

def self.complete(pools)
pools.each { |pool| pool.disable_query_cache! }

ActiveRecord::Base.connection_handler.connection_pool_list(:all).each do |pool|
ActiveRecord::Base.connection_handler.each_connection_pool do |pool|
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
end
end
Expand Down

0 comments on commit e24fbf7

Please sign in to comment.