class Mysql2::Aurora::Client

Implement client patch

Attributes

client[R]

Public Class Methods

const_missing(name) click to toggle source

Delegate const reference to class. @param [Symbol] name Const name

# 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) click to toggle source

Delegate method call to Mysql2::Client. @param [String] name Method name @param [Array] args Method arguments @param [Proc] block Method block

# 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
new(opts) click to toggle source

Initialize class @note [Override] with reconnect options @param [Hash] opts Options @option opts [Integer] aurora_max_retry Max retry count, when failover. (Default: 5)

# 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

Public Instance Methods

method_missing(name, *args, &block) click to toggle source

Delegate method call to client. @param [String] name Method name @param [Array] args Method arguments @param [Proc] block Method block

# 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
query(*args) click to toggle source

Execute query with reconnect @note [Override] with reconnect.

# 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!() click to toggle source

Reconnect to database and Set `@client` @note If client is not connected, Connect to database.

# 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