-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retrying to Azure downloads (#249)
* Add retrying to Azure downloads * Add default timeouts to Faraday requests * Stub rand() and not the whole method to avoid randomness * These are specs, no reason to wait
- Loading branch information
Showing
5 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Retryable | ||
SLEEP_INTERVAL = 0.4 | ||
|
||
def with_retries(retries: 3, rescue_class: ) | ||
tries = 0 | ||
|
||
begin | ||
yield | ||
rescue *rescue_class => e | ||
tries += 1 | ||
if tries <= retries | ||
sleep sleep_interval(tries) | ||
retry | ||
else | ||
raise | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def sleep_interval(tries) | ||
(SLEEP_INTERVAL + rand(0.0..1.0)) * tries | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'faraday' | ||
|
||
# Sets a 5 second default timeout for read & write requests | ||
# Faraday is used under the hood by ActiveStorage, and therefore Azure Blob Storage | ||
Faraday.default_connection_options.request.open_timeout = 5 | ||
Faraday.default_connection_options.request.timeout = 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'rails_helper' | ||
require 'retryable' | ||
|
||
RSpec.describe Retryable do | ||
class DummyClass | ||
def process | ||
with_retries( | ||
rescue_class: [Faraday::TimeoutError, Errno::ECONNREFUSED] | ||
) { self.call_me_maybe } | ||
end | ||
|
||
# Can't raise the exception directly or it won't retry | ||
def call_me_maybe | ||
return "success" | ||
end | ||
end | ||
|
||
let(:dummy_class) { DummyClass.new } | ||
|
||
before(:each) do | ||
# No waiting for the interval, no retry timing randomness | ||
stub_const("Retryable::SLEEP_INTERVAL", 0.0) | ||
allow_any_instance_of(Object).to receive(:rand).and_return(0.0) | ||
dummy_class.extend(Retryable) | ||
end | ||
|
||
it 'tries 4 times, then re-raises the error' do | ||
expect(dummy_class).to receive(:call_me_maybe).exactly(4).times.and_raise(Faraday::TimeoutError) | ||
expect { | ||
expect(dummy_class.process) | ||
}.to raise_error(Faraday::TimeoutError) | ||
end | ||
|
||
it 'retries once then succeeds' do | ||
expect(dummy_class).to receive(:call_me_maybe).once.and_raise(Errno::ECONNREFUSED) | ||
expect(dummy_class).to receive(:call_me_maybe).once.and_return("success") | ||
expect(dummy_class.process).to eq("success") | ||
end | ||
end |