Class: Mysql2::Aurora::Client
- Inherits:
-
Object
- Object
- Mysql2::Aurora::Client
- Defined in:
- lib/mysql2/aurora.rb
Overview
Implement client patch
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Class Method Summary collapse
-
.const_missing(name) ⇒ Object
Delegate const reference to class.
-
.method_missing(name, *args, &block) ⇒ Object
Delegate method call to Mysql2::Client.
Instance Method Summary collapse
-
#initialize(opts) ⇒ Client
constructor
Initialize class.
-
#method_missing(name, *args, &block) ⇒ Object
Delegate method call to client.
-
#query(*args) ⇒ Object
Execute query with reconnect.
-
#reconnect! ⇒ Object
Reconnect to database and Set ‘@client`.
Constructor Details
#initialize(opts) ⇒ Client
Note:
- Override
-
with reconnect options
Initialize class
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.
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
#client ⇒ Object (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.
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.
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.&.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! = (@client&.&.dup || {}) begin @client&.close rescue StandardError nil end @client = Mysql2::Aurora::ORIGINAL_CLIENT_CLASS.new(@opts) @client..merge!() end |