Class: Mysql2::Aurora::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql2/aurora.rb

Overview

Implement client patch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Client

Note:
Override

with reconnect options

Initialize class

Parameters:

  • opts (Hash)

    Options

Options Hash (opts):

  • aurora_max_retry (Integer)

    Max retry count, when failover. (Default: 5)



16
17
18
19
20
# File 'lib/mysql2/aurora.rb', line 16

def initialize(opts)
  @opts      = Mysql2::Util.key_hash_as_symbols(opts)
  @max_retry = @opts.delete(:aurora_max_retry) || 5
  reconnect!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Delegate method call to client.

Parameters:

  • name (String)

    Method name

  • args (Array)

    Method arguments

  • block (Proc)

    Method block



65
66
67
# File 'lib/mysql2/aurora.rb', line 65

def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissingSuper, Style/MissingRespondToMissing
  client.public_send(name, *args, &block)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/mysql2/aurora.rb', line 10

def client
  @client
end

Class Method Details

.const_missing(name) ⇒ Object

Delegate const reference to class.

Parameters:

  • name (Symbol)

    Const name



79
80
81
# File 'lib/mysql2/aurora.rb', line 79

def self.const_missing(name)
  Mysql2::Aurora::ORIGINAL_CLIENT_CLASS.const_get(name)
end

.method_missing(name, *args, &block) ⇒ Object

Delegate method call to Mysql2::Client.

Parameters:

  • name (String)

    Method name

  • args (Array)

    Method arguments

  • block (Proc)

    Method block



73
74
75
# File 'lib/mysql2/aurora.rb', line 73

def self.method_missing(name, *args, &block) # rubocop:disable Style/MethodMissingSuper, Style/MissingRespondToMissing
  Mysql2::Aurora::ORIGINAL_CLIENT_CLASS.public_send(name, *args, &block)
end

Instance Method Details

#query(*args) ⇒ Object

Note:
Override

with reconnect.

Execute query with reconnect



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mysql2/aurora.rb', line 24

def query(*args)
  try_count = 0

  begin
    client.query(*args)
  rescue Mysql2::Error => e
    try_count += 1

    if e.message&.include?('--read-only') && try_count <= @max_retry
      retry_interval_seconds = [1.5 * (try_count - 1), 10].min

      warn "[mysql2-aurora] Database is readonly. Retry after #{retry_interval_seconds}seconds"
      sleep retry_interval_seconds
      reconnect!

      retry
    else
      raise e
    end
  end
end

#reconnect!Object

Note:

If client is not connected, Connect to database.

Reconnect to database and Set ‘@client`



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mysql2/aurora.rb', line 48

def reconnect!
  query_options = (@client&.query_options&.dup || {})

  begin
    @client&.close
  rescue StandardError
    nil
  end

  @client = Mysql2::Aurora::ORIGINAL_CLIENT_CLASS.new(@opts)
  @client.query_options.merge!(query_options)
end