Class: Mysql2::Aurora::Client

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

Overview

Implement client patch

Constant Summary collapse

RETRY_INTERVAL_SECONDS =
1.5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Client

Note:
Override

with reconnect options

Initialize class

Options Hash (opts):

  • aurora_max_retry (Integer)

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



18
19
20
21
22
# File 'lib/mysql2/aurora.rb', line 18

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.



60
61
62
# File 'lib/mysql2/aurora.rb', line 60

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.



12
13
14
# File 'lib/mysql2/aurora.rb', line 12

def client
  @client
end

Class Method Details

.const_missing(name) ⇒ Object

Delegate const reference to class.



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

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.



68
69
70
# File 'lib/mysql2/aurora.rb', line 68

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



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

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
      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`



47
48
49
50
51
52
53
54
# File 'lib/mysql2/aurora.rb', line 47

def reconnect!
  begin
    @client&.close
  rescue StandardError
    nil
  end
  @client = Mysql2::Aurora::ORIGINAL_CLIENT_CLASS.new(@opts)
end