Class: Replidog::ProxyHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/replidog/proxy_handler.rb

Instance Method Summary collapse

Constructor Details

#initializeProxyHandler

Returns a new instance of ProxyHandler.



5
6
7
8
# File 'lib/replidog/proxy_handler.rb', line 5

def initialize
  @proxies = Concurrent::Map.new(initial_capacity: 2)
  @class_to_proxy = Concurrent::Map.new(initial_capacity: 2)
end

Instance Method Details

#clear_active_slave_connections!Object



36
37
38
39
40
# File 'lib/replidog/proxy_handler.rb', line 36

def clear_active_slave_connections!
  @proxies.each_value do |proxy|
    proxy.clear_active_slave_connections!
  end
end

#clear_all_slave_connections!Object



48
49
50
51
52
# File 'lib/replidog/proxy_handler.rb', line 48

def clear_all_slave_connections!
  @proxies.each_value do |proxy|
    proxy.clear_all_slave_connections!
  end
end

#clear_query_cacheObject



68
69
70
71
72
73
# File 'lib/replidog/proxy_handler.rb', line 68

def clear_query_cache
  connection_pool_list_for(ActiveRecord::Base.connection_handler).each do |connection_pool|
    connection_pool.connection.clear_query_cache
  end
  @proxies.each_value(&:clear_query_cache_for_slaves)
end

#clear_reloadable_slave_connections!Object



42
43
44
45
46
# File 'lib/replidog/proxy_handler.rb', line 42

def clear_reloadable_slave_connections!
  @proxies.each_value do |proxy|
    proxy.clear_reloadable_slave_connections!
  end
end

#disable_query_cache!Object



61
62
63
64
65
66
# File 'lib/replidog/proxy_handler.rb', line 61

def disable_query_cache!
  connection_pool_list_for(ActiveRecord::Base.connection_handler).each do |connection_pool|
    connection_pool.connection.disable_query_cache!
  end
  @proxies.each_value(&:disable_query_cache_for_slaves!)
end

#enable_query_cache!Object



54
55
56
57
58
59
# File 'lib/replidog/proxy_handler.rb', line 54

def enable_query_cache!
  connection_pool_list_for(ActiveRecord::Base.connection_handler).each do |connection_pool|
    connection_pool.connection.enable_query_cache!
  end
  @proxies.each_value(&:enable_query_cache_for_slaves!)
end

#establish_connection(configuration, klass) ⇒ Object

Raises:

  • (RuntimeError)


10
11
12
13
14
# File 'lib/replidog/proxy_handler.rb', line 10

def establish_connection(configuration, klass)
  @class_to_proxy.clear
  raise RuntimeError, "Anonymous class is not allowed." unless klass.name
  @proxies[klass.name] = Proxy.new(self, configuration)
end

#remove_connection(klass) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/replidog/proxy_handler.rb', line 28

def remove_connection(klass)
  if proxy = @proxies.delete(klass.name)
    @class_to_proxy.clear
    proxy.clear_all_slave_connections!
    proxy.configuration.config
  end
end

#retrieve_proxy(klass) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/replidog/proxy_handler.rb', line 16

def retrieve_proxy(klass)
  @class_to_proxy[klass.name] ||=
    begin
      until proxy = @proxies[klass.name]
        klass = klass.superclass
        break unless klass <= ActiveRecord::Base
      end

      @class_to_proxy[klass.name] = proxy
    end
end