Skip to content

Commit

Permalink
Merge pull request #3 from alfa-jpn/fix/retry-interval
Browse files Browse the repository at this point in the history
Fix retry interval
  • Loading branch information
alfa-jpn committed Jan 16, 2019
2 parents d79d1b8 + f694251 commit 1b1b77c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
11 changes: 6 additions & 5 deletions lib/mysql2/aurora.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module Mysql2
module Aurora
# Implement client patch
class Client
RETRY_INTERVAL_SECONDS = 1.5

attr_reader :client

# Initialize class
Expand All @@ -32,9 +30,12 @@ def query(*args)
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
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
Expand Down Expand Up @@ -62,7 +63,7 @@ def reconnect!
# @param [Array] args Method arguments
# @param [Proc] block Method block
def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissingSuper, Style/MissingRespondToMissing
@client.public_send(name, *args, &block)
client.public_send(name, *args, &block)
end

# Delegate method call to Mysql2::Client.
Expand Down
2 changes: 1 addition & 1 deletion lib/mysql2/aurora/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module Aurora
# Major Version: Support `mysql2` major version.
# Minor Version: Support `mysql2` minor version.
# Tiny Version: Mysql2::Aurora version.
VERSION = '0.5.1'.freeze
VERSION = '0.5.2'.freeze
end
end
18 changes: 13 additions & 5 deletions spec/mysql2/aurora_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
host: ENV['TEST_DB_HOST'],
username: ENV['TEST_DB_USER'],
password: ENV['TEST_DB_PASS'],
aurora_max_retry: 2
aurora_max_retry: 10
)
end

Expand Down Expand Up @@ -65,14 +65,22 @@

context 'When raise Mysql2::Error' do
before :each do
allow_any_instance_of(Kernel).to receive(:warn)
allow_any_instance_of(Kernel).to receive(:sleep)
allow(client).to receive(:warn)
allow(client).to receive(:sleep)
allow(client).to receive(:reconnect!)
allow(client.client).to receive(:query).and_raise(Mysql2::Error, 'ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement')
end

it 'Retry query' do
expect(client).to receive(:reconnect!).exactly(2).times
expect(client.client).to receive(:query).exactly(3).times
expect(client).to receive(:reconnect!).exactly(10).times
expect(client.client).to receive(:query).exactly(11).times
expect { subject }.to raise_error(Mysql2::Error)
end

it 'Retry interval is valid' do
[0, 1.5, 3, 4.5, 6, 7.5, 9, 10, 10, 10].each do |seconds|
expect(client).to receive(:sleep).with(seconds).ordered
end
expect { subject }.to raise_error(Mysql2::Error)
end

Expand Down

0 comments on commit 1b1b77c

Please sign in to comment.