Class: Makara::ConnectionWrapper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#configObject

Returns the value of attribute config.



11
12
13
# File 'lib/makara/connection_wrapper.rb', line 11

def config
  @config
end

#initial_errorObject

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?

Returns:

  • (Boolean)


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

Returns:

  • (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_connectionObject



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_matchersObject

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_nameObject

the name of this node



34
35
36
# File 'lib/makara/connection_wrapper.rb', line 34

def _makara_name
  @config[:name]
end

#_makara_weightObject

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