Class: Mysql2::ReconnectWithReadonly
- Inherits:
-
Object
- Object
- Mysql2::ReconnectWithReadonly
- Defined in:
- lib/mysql2/reconnect_with_readonly.rb,
lib/mysql2/reconnect_with_readonly/version.rb
Constant Summary collapse
Class Attribute Summary collapse
-
.initial_retry_wait ⇒ Object
Returns the value of attribute initial_retry_wait.
-
.logger ⇒ Object
Returns the value of attribute logger.
-
.max_retry_wait ⇒ Object
Returns the value of attribute max_retry_wait.
-
.reconnect_attempts ⇒ Object
Returns the value of attribute reconnect_attempts.
Class Method Summary collapse
Class Attribute Details
.initial_retry_wait ⇒ Object
Returns the value of attribute initial_retry_wait.
13 14 15 |
# File 'lib/mysql2/reconnect_with_readonly.rb', line 13 def initial_retry_wait @initial_retry_wait end |
.logger ⇒ Object
Returns the value of attribute logger.
13 14 15 |
# File 'lib/mysql2/reconnect_with_readonly.rb', line 13 def logger @logger end |
.max_retry_wait ⇒ Object
Returns the value of attribute max_retry_wait.
13 14 15 |
# File 'lib/mysql2/reconnect_with_readonly.rb', line 13 def max_retry_wait @max_retry_wait end |
.reconnect_attempts ⇒ Object
Returns the value of attribute reconnect_attempts.
13 14 15 |
# File 'lib/mysql2/reconnect_with_readonly.rb', line 13 def reconnect_attempts @reconnect_attempts end |
Class Method Details
.reconnect_with_readonly(client, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mysql2/reconnect_with_readonly.rb', line 16 def self.reconnect_with_readonly(client, &block) retries = 0 begin yield block rescue Mysql2::Error => e if e. =~ /read-only/ if retries < reconnect_attempts wait = initial_retry_wait * retries wait = [wait, max_retry_wait].min if max_retry_wait logger.info { "Reconnect with readonly: #{e.} " \ "(retries: #{retries}/#{reconnect_attempts}) (wait: #{wait}sec)" } if logger sleep wait retries += 1 client.reconnect logger.debug { "Reconnect with readonly: disconnected and retry" } if logger retry else logger.info { "Reconnect with readonly: Give up " \ "(retries: #{retries}/#{reconnect_attempts})" } if logger raise e end else raise e end end end |