Class: Replidog::Proxy

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

Defined Under Namespace

Classes: ConnectionPoolCreater

Constant Summary collapse

REPLICABLE_METHOD_NAMES =
[/^select(?:_\w+)$/]
REPLICABLE_METHOD_NAMES_REGEXP =
/\A#{Regexp.union(REPLICABLE_METHOD_NAMES)}\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler, configuration) ⇒ Proxy

Returns a new instance of Proxy.



14
15
16
17
18
# File 'lib/replidog/proxy.rb', line 14

def initialize(handler, configuration)
  @handler = handler
  @configuration = configuration
  @lock = Mutex.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



107
108
109
110
111
112
113
# File 'lib/replidog/proxy.rb', line 107

def method_missing(method_name, *args, &block)
  if current_connection_name
    current_connection.send(method_name, *args, &block)
  else
    connection_by_method_name(method_name).send(method_name, *args, &block)
  end
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



12
13
14
# File 'lib/replidog/proxy.rb', line 12

def configuration
  @configuration
end

#index=(value) ⇒ Object

Sets the attribute index

Parameters:

  • value

    the value to set the attribute index to.



10
11
12
# File 'lib/replidog/proxy.rb', line 10

def index=(value)
  @index = value
end

Instance Method Details

#clear_active_slave_connections!Object



56
57
58
59
60
# File 'lib/replidog/proxy.rb', line 56

def clear_active_slave_connections!
  slave_connection_pool_table.each_value do |pool|
    pool.release_connection
  end
end

#clear_all_slave_connections!Object



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

def clear_all_slave_connections!
  slave_connection_pool_table.each_value do |pool|
    pool.automatic_reconnect = false
    pool.disconnect!
  end
end

#clear_query_cacheObject



95
96
97
# File 'lib/replidog/proxy.rb', line 95

def clear_query_cache
  @handler.clear_query_cache
end

#clear_query_cache_for_slavesObject



99
100
101
102
103
# File 'lib/replidog/proxy.rb', line 99

def clear_query_cache_for_slaves
  slave_connection_pool_table.values.each do |pool|
    pool.connection.clear_query_cache
  end
end

#clear_reloadable_slave_connections!Object



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

def clear_reloadable_slave_connections!
  slave_connection_pool_table.each_value do |pool|
    pool.clear_reloadable_connections!
  end
end

#connected?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/replidog/proxy.rb', line 52

def connected?
  current_model.connection_handler.connected?(current_model) && slave_connection_pool_table.values.any?(&:connected?)
end

#current_connection_nameObject



28
29
30
# File 'lib/replidog/proxy.rb', line 28

def current_connection_name
  Thread.current['replidog.current_connection_name']
end

#current_connection_name=(connection_name) ⇒ Object



32
33
34
# File 'lib/replidog/proxy.rb', line 32

def current_connection_name=(connection_name)
  Thread.current['replidog.current_connection_name'] = connection_name
end

#current_modelObject



20
21
22
# File 'lib/replidog/proxy.rb', line 20

def current_model
  Thread.current['replidog.current_model']
end

#current_model=(model) ⇒ Object



24
25
26
# File 'lib/replidog/proxy.rb', line 24

def current_model=(model)
  Thread.current['replidog.current_model'] = model.is_a?(ActiveRecord::Base) ? model.class : model
end

#disable_query_cache!Object



85
86
87
# File 'lib/replidog/proxy.rb', line 85

def disable_query_cache!
  @handler.disable_query_cache!
end

#disable_query_cache_for_slaves!Object



89
90
91
92
93
# File 'lib/replidog/proxy.rb', line 89

def disable_query_cache_for_slaves!
  slave_connection_pool_table.values.each do |pool|
    pool.connection.disable_query_cache!
  end
end

#enable_query_cache!Object



75
76
77
# File 'lib/replidog/proxy.rb', line 75

def enable_query_cache!
  @handler.enable_query_cache!
end

#enable_query_cache_for_slaves!Object



79
80
81
82
83
# File 'lib/replidog/proxy.rb', line 79

def enable_query_cache_for_slaves!
  slave_connection_pool_table.each_value do |pool|
    pool.connection.enable_query_cache!
  end
end

#lock(locks = true) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/replidog/proxy.rb', line 44

def lock(locks = true)
  old_connection_name = current_connection_name
  self.current_connection_name ||= :master
  current_connection.lock(locks)
ensure
  self.current_connection_name = old_connection_name
end

#transaction(options = {}, &block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/replidog/proxy.rb', line 36

def transaction(options = {}, &block)
  old_connection_name = current_connection_name
  self.current_connection_name ||= :master
  current_connection.transaction(options, &block)
ensure
  self.current_connection_name = old_connection_name
end