Class: ActiveRecord::ConnectionAdapters::PoolManager

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/connection_adapters/pool_manager.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initializePoolManager

Returns a new instance of PoolManager.



6
7
8
# File 'activerecord/lib/active_record/connection_adapters/pool_manager.rb', line 6

def initialize
  @role_to_shard_mapping = Hash.new { |h, k| h[k] = {} }
end

Instance Method Details

#each_pool_config(role = nil, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'activerecord/lib/active_record/connection_adapters/pool_manager.rb', line 26

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

#get_pool_config(role, shard) ⇒ Object



44
45
46
# File 'activerecord/lib/active_record/connection_adapters/pool_manager.rb', line 44

def get_pool_config(role, shard)
  @role_to_shard_mapping[role][shard]
end

#pool_configs(role = nil) ⇒ Object



18
19
20
21
22
23
24
# File 'activerecord/lib/active_record/connection_adapters/pool_manager.rb', line 18

def pool_configs(role = nil)
  if role
    @role_to_shard_mapping[role].values
  else
    @role_to_shard_mapping.flat_map { |_, shard_map| shard_map.values }
  end
end

#remove_pool_config(role, shard) ⇒ Object



40
41
42
# File 'activerecord/lib/active_record/connection_adapters/pool_manager.rb', line 40

def remove_pool_config(role, shard)
  @role_to_shard_mapping[role].delete(shard)
end

#remove_role(role) ⇒ Object



36
37
38
# File 'activerecord/lib/active_record/connection_adapters/pool_manager.rb', line 36

def remove_role(role)
  @role_to_shard_mapping.delete(role)
end

#role_namesObject



14
15
16
# File 'activerecord/lib/active_record/connection_adapters/pool_manager.rb', line 14

def role_names
  @role_to_shard_mapping.keys
end

#set_pool_config(role, shard, pool_config) ⇒ Object



48
49
50
51
52
53
54
# File 'activerecord/lib/active_record/connection_adapters/pool_manager.rb', line 48

def set_pool_config(role, shard, pool_config)
  if pool_config
    @role_to_shard_mapping[role][shard] = pool_config
  else
    raise ArgumentError, "The `pool_config` for the :#{role} role and :#{shard} shard was `nil`. Please check your configuration. If you want your writing role to be something other than `:writing` set `config.active_record.writing_role` in your application configuration. The same setting should be applied for the `reading_role` if applicable."
  end
end

#shard_namesObject



10
11
12
# File 'activerecord/lib/active_record/connection_adapters/pool_manager.rb', line 10

def shard_names
  @role_to_shard_mapping.values.flat_map { |shard_map| shard_map.keys }
end