Class: Motel::ConnectionAdapters::ConnectionHandler

Inherits:
ActiveRecord::ConnectionAdapters::ConnectionHandler
  • Object
show all
Defined in:
lib/motel/connection_adapters/connection_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tenants_source = Sources::Default.new) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



10
11
12
13
14
15
16
17
18
19
# File 'lib/motel/connection_adapters/connection_handler.rb', line 10

def initialize(tenants_source = Sources::Default.new)
  @owner_to_pool = ThreadSafe::Cache.new(:initial_capacity => 2) do |h,k|
    h[k] = ThreadSafe::Cache.new(:initial_capacity => 2)
  end
  @class_to_pool = ThreadSafe::Cache.new(:initial_capacity => 2) do |h,k|
    h[k] = ThreadSafe::Cache.new
  end

  @tenants_source = tenants_source
end

Instance Attribute Details

#tenants_sourceObject

Returns the value of attribute tenants_source.



8
9
10
# File 'lib/motel/connection_adapters/connection_handler.rb', line 8

def tenants_source
  @tenants_source
end

Instance Method Details

#active_tenantsObject



71
72
73
# File 'lib/motel/connection_adapters/connection_handler.rb', line 71

def active_tenants
   owner_to_pool.keys
end

#connected?(tenant_name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/motel/connection_adapters/connection_handler.rb', line 55

def connected?(tenant_name)
  conn = retrieve_connection_pool(tenant_name)
  conn && conn.connected?
end

#establish_connection(tenant_name, spec = nil) ⇒ Object



21
22
23
24
25
# File 'lib/motel/connection_adapters/connection_handler.rb', line 21

def establish_connection(tenant_name, spec = nil)
  spec ||= connection_especification(tenant_name)
  @class_to_pool.clear
  owner_to_pool[tenant_name] = ::ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
end

#pool_for(tenant_name) ⇒ Object



60
61
62
63
64
# File 'lib/motel/connection_adapters/connection_handler.rb', line 60

def pool_for(tenant_name)
  owner_to_pool.fetch(tenant_name) {
    establish_connection tenant_name
  }
end

#pool_from_any_process_for(tenant_name) ⇒ Object



66
67
68
69
# File 'lib/motel/connection_adapters/connection_handler.rb', line 66

def pool_from_any_process_for(tenant_name)
  owner_to_pool = @owner_to_pool.values.find { |v| v[tenant_name] }
  owner_to_pool && owner_to_pool[tenant_name]
end

#remove_connection(tenant_name) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/motel/connection_adapters/connection_handler.rb', line 46

def remove_connection(tenant_name)
  if pool = owner_to_pool.delete(tenant_name)
    @class_to_pool.clear
    pool.automatic_reconnect = false
    pool.disconnect!
    pool.spec.config
  end
end

#retrieve_connection(tenant_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/motel/connection_adapters/connection_handler.rb', line 27

def retrieve_connection(tenant_name)
  pool = retrieve_connection_pool(tenant_name)

  unless pool.connection
    establish_connection tenant_name
    pool = retrieve_connection_pool(tenant_name)
  end

  pool.connection
end

#retrieve_connection_pool(tenant_name) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/motel/connection_adapters/connection_handler.rb', line 38

def retrieve_connection_pool(tenant_name)
  class_to_pool[tenant_name] ||= begin
    pool = pool_for(tenant_name)

    class_to_pool[tenant_name] = pool
  end
end