Class: Makara::ConnectionWrapper
- Inherits:
-
Object
- Object
- Makara::ConnectionWrapper
- Defined in:
- lib/makara/connection_wrapper.rb
Constant Summary collapse
- SQL_REPLACE =
invalid queries caused by connections switching that needs to be replaced
{"SET client_min_messages TO ''".freeze => "SET client_min_messages TO 'warning'".freeze}.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#initial_error ⇒ Object
Returns the value of attribute initial_error.
Instance Method Summary collapse
-
#_makara_blacklist! ⇒ Object
blacklist this node for @config seconds.
-
#_makara_blacklisted? ⇒ Boolean
has this node been blacklisted?.
- #_makara_connected? ⇒ Boolean
- #_makara_connection ⇒ Object
-
#_makara_custom_error_matchers ⇒ Object
custom error messages.
-
#_makara_name ⇒ Object
the name of this node.
-
#_makara_weight ⇒ Object
the weight of the current node.
-
#_makara_whitelist! ⇒ Object
release the blacklist.
- #execute(*args) ⇒ Object
-
#initialize(proxy, connection, config) ⇒ ConnectionWrapper
constructor
A new instance of ConnectionWrapper.
-
#method_missing(m, *args, &block) ⇒ Object
we want to forward all private methods, since we could have kicked out from a private scenario.
Constructor Details
#initialize(proxy, connection, config) ⇒ ConnectionWrapper
Returns a new instance of ConnectionWrapper.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/makara/connection_wrapper.rb', line 16 def initialize(proxy, connection, config) @config = config.symbolize_keys @connection = connection @proxy = proxy if connection.nil? _makara_blacklist! else _makara_decorate_connection(connection) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
we want to forward all private methods, since we could have kicked out from a private scenario
97 98 99 100 101 102 103 |
# File 'lib/makara/connection_wrapper.rb', line 97 def method_missing(m, *args, &block) if _makara_connection.respond_to?(m) _makara_connection.public_send(m, *args, &block) else # probably private method _makara_connection.__send__(m, *args, &block) end end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
11 12 13 |
# File 'lib/makara/connection_wrapper.rb', line 11 def config @config end |
#initial_error ⇒ Object
Returns the value of attribute initial_error.
11 12 13 |
# File 'lib/makara/connection_wrapper.rb', line 11 def initial_error @initial_error end |
Instance Method Details
#_makara_blacklist! ⇒ Object
blacklist this node for @config seconds
44 45 46 47 48 |
# File 'lib/makara/connection_wrapper.rb', line 44 def _makara_blacklist! @connection.disconnect! if @connection @connection = nil @blacklisted_until = Time.now.to_i + @config[:blacklist_duration] unless @config[:disable_blacklist] end |
#_makara_blacklisted? ⇒ Boolean
has this node been blacklisted?
39 40 41 |
# File 'lib/makara/connection_wrapper.rb', line 39 def _makara_blacklisted? @blacklisted_until.to_i > Time.now.to_i end |
#_makara_connected? ⇒ Boolean
60 61 62 63 64 |
# File 'lib/makara/connection_wrapper.rb', line 60 def _makara_connected? _makara_connection.present? rescue Makara::Errors::BlacklistConnection false end |
#_makara_connection ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/makara/connection_wrapper.rb', line 66 def _makara_connection current = @connection if current current else # blacklisted connection or initial error new_connection = @proxy.graceful_connection_for(@config) # Already wrapped because of initial failure if new_connection.is_a?(Makara::ConnectionWrapper) _makara_blacklist! raise Makara::Errors::BlacklistConnection.new(self, new_connection.initial_error) else @connection = new_connection _makara_decorate_connection(new_connection) new_connection end end end |
#_makara_custom_error_matchers ⇒ Object
custom error messages
56 57 58 |
# File 'lib/makara/connection_wrapper.rb', line 56 def _makara_custom_error_matchers @custom_error_matchers ||= (@config[:connection_error_matchers] || []) end |
#_makara_name ⇒ Object
the name of this node
34 35 36 |
# File 'lib/makara/connection_wrapper.rb', line 34 def _makara_name @config[:name] end |
#_makara_weight ⇒ Object
the weight of the current node
29 30 31 |
# File 'lib/makara/connection_wrapper.rb', line 29 def _makara_weight @config[:weight] || 1 end |
#_makara_whitelist! ⇒ Object
release the blacklist
51 52 53 |
# File 'lib/makara/connection_wrapper.rb', line 51 def _makara_whitelist! @blacklisted_until = nil end |
#execute(*args) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/makara/connection_wrapper.rb', line 86 def execute(*args) SQL_REPLACE.each do |find, replace| if args[0] == find args[0] = replace end end _makara_connection.execute(*args) end |