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_in_transaction? ⇒ Boolean
-
#_makara_name ⇒ Object
the name of this node.
- #_makara_shard_id ⇒ Object
-
#_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.
- #respond_to_missing?(m, include_private = false) ⇒ Boolean
Constructor Details
#initialize(proxy, connection, config) ⇒ ConnectionWrapper
Returns a new instance of ConnectionWrapper.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/makara/connection_wrapper.rb', line 15 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
104 105 106 |
# File 'lib/makara/connection_wrapper.rb', line 104 def method_missing(m, *args, &block) _makara_connection.send(m, *args, &block) end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
10 11 12 |
# File 'lib/makara/connection_wrapper.rb', line 10 def config @config end |
#initial_error ⇒ Object
Returns the value of attribute initial_error.
10 11 12 |
# File 'lib/makara/connection_wrapper.rb', line 10 def initial_error @initial_error end |
Instance Method Details
#_makara_blacklist! ⇒ Object
blacklist this node for @config seconds
51 52 53 54 55 |
# File 'lib/makara/connection_wrapper.rb', line 51 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?
42 43 44 |
# File 'lib/makara/connection_wrapper.rb', line 42 def _makara_blacklisted? @blacklisted_until.present? && @blacklisted_until.to_i > Time.now.to_i end |
#_makara_connected? ⇒ Boolean
67 68 69 70 71 |
# File 'lib/makara/connection_wrapper.rb', line 67 def _makara_connected? _makara_connection.present? rescue Makara::Errors::BlacklistConnection false end |
#_makara_connection ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/makara/connection_wrapper.rb', line 73 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
63 64 65 |
# File 'lib/makara/connection_wrapper.rb', line 63 def _makara_custom_error_matchers @custom_error_matchers ||= (@config[:connection_error_matchers] || []) end |
#_makara_in_transaction? ⇒ Boolean
46 47 48 |
# File 'lib/makara/connection_wrapper.rb', line 46 def _makara_in_transaction? @connection && @connection.open_transactions > 0 end |
#_makara_name ⇒ Object
the name of this node
33 34 35 |
# File 'lib/makara/connection_wrapper.rb', line 33 def _makara_name @config[:name] end |
#_makara_shard_id ⇒ Object
37 38 39 |
# File 'lib/makara/connection_wrapper.rb', line 37 def _makara_shard_id @config[:shard_id] end |
#_makara_weight ⇒ Object
the weight of the current node
28 29 30 |
# File 'lib/makara/connection_wrapper.rb', line 28 def _makara_weight @config[:weight] || 1 end |
#_makara_whitelist! ⇒ Object
release the blacklist
58 59 60 |
# File 'lib/makara/connection_wrapper.rb', line 58 def _makara_whitelist! @blacklisted_until = nil end |
#execute(*args) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/makara/connection_wrapper.rb', line 93 def execute(*args) SQL_REPLACE.each do |find, replace| if args[0] == find args[0] = replace end end _makara_connection.execute(*args) end |
#respond_to_missing?(m, include_private = false) ⇒ Boolean
110 111 112 |
# File 'lib/makara/connection_wrapper.rb', line 110 def respond_to_missing?(m, include_private = false) _makara_connection.respond_to?(m, true) end |