Class: Rails::Sharding::ConnectionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/sharding/connection_handler.rb

Class Method Summary collapse

Class Method Details

.connected?(shard_group, shard_name) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rails/sharding/connection_handler.rb', line 59

def self.connected?(shard_group, shard_name)
  connection_handler.connected?(connection_name(shard_group, shard_name))
end

.connection_pool(shard_group, shard_name) ⇒ Object

Raises:

  • (ActiveRecord::ConnectionNotEstablished)


38
39
40
41
42
43
44
45
46
# File 'lib/rails/sharding/connection_handler.rb', line 38

def self.connection_pool(shard_group, shard_name)
  if connection_pool = connection_handler.retrieve_connection_pool(connection_name(shard_group, shard_name))
    return connection_pool
  end

  # mimicking behavior of rails at:
  # https://github.com/rails/rails/blob/v5.0.0.1/activerecord/lib/active_record/connection_handling.rb#124
  raise ActiveRecord::ConnectionNotEstablished, "No connection pool for shard #{connection_name(shard_group, shard_name)}" if connection_pool.nil?
end

.establish_all_connectionsObject

Establishes connections to all shards in all shard groups. Despite the name, this actually only creates a connection pool with zero connections for each shard. The connections will be allocated for each thread when #retrieve_connection or #with_connection are called



8
9
10
11
12
13
14
# File 'lib/rails/sharding/connection_handler.rb', line 8

def self.establish_all_connections
  Core.shard_groups.each do |shard_group|
    Core.shard_names(shard_group).each do |shard_name|
      establish_connection(shard_group, shard_name)
    end
  end
end

.establish_connection(shard_group, shard_name, environment = nil) ⇒ Object

Establishes a connection to a single shard in a single shard group



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rails/sharding/connection_handler.rb', line 17

def self.establish_connection(shard_group, shard_name, environment=nil)
  self.setup unless defined? @@connection_handler

  configurations = (environment.nil? ? Core.configurations : Core.configurations(environment))

  shard_group_configurations = configurations[shard_group.to_s]
  if shard_group_configurations.nil?
    raise Errors::ConfigNotFoundError, "Cannot find configuration for shard_group '#{shard_group}' in environment '#{environment}' in #{Config.shards_config_file}"
  end

  resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(shard_group_configurations)
  begin
    connection_name = connection_name(shard_group, shard_name)
    connection_spec = resolver.spec(shard_name.to_sym, connection_name)
  rescue ActiveRecord::AdapterNotSpecified
    raise Errors::ConfigNotFoundError, "Cannot find configuration for shard '#{shard_group}:#{shard_name}' in environment '#{environment}' in #{Config.shards_config_file}"
  end

  connection_handler.establish_connection(connection_spec)
end

.remove_connection(shard_group, shard_name) ⇒ Object



73
74
75
# File 'lib/rails/sharding/connection_handler.rb', line 73

def self.remove_connection(shard_group, shard_name)
  connection_handler.remove_connection(connection_name(shard_group, shard_name))
end

.retrieve_connection(shard_group, shard_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/rails/sharding/connection_handler.rb', line 48

def self.retrieve_connection(shard_group, shard_name)
  connection_name = connection_name(shard_group, shard_name)
  connection = connection_handler.retrieve_connection(connection_name)

  if connection && Config.add_shard_tag_to_query_logs
    add_shard_tag_to_connection_log(connection, connection_name)
  else
    connection
  end
end

.with_connection(shard_group, shard_name, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/rails/sharding/connection_handler.rb', line 63

def self.with_connection(shard_group, shard_name, &block)
	connection_pool(shard_group, shard_name).with_connection do |connection|
    if connection && Config.add_shard_tag_to_query_logs
      connection_name = connection_name(shard_group, shard_name)
      add_shard_tag_to_connection_log(connection, connection_name)
    end
    block.call(connection)
  end
end