Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .retriable feature to Http #598

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
13 changes: 11 additions & 2 deletions lib/http/retriable/performer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Performer
IOError
].freeze

RETRIABLE_STATUSES = [500].freeze
RETRIABLE_STATUSES = (500...Float::INFINITY).freeze
Bertg marked this conversation as resolved.
Show resolved Hide resolved

# Default retry delay proc
DELAY_PROC = ->(attempt) {
Expand Down Expand Up @@ -168,11 +168,20 @@ def out_of_retries_error(request, response, exception)
end

def build_retry_proc(exception_classes, retry_statuses)
retry_statuses = [retry_statuses].flatten

->(_req, err, res, _i) {
if err
exception_classes.any? { |e| err.is_a?(e) }
else
retry_statuses.include?(res.status.to_i)
response_status = res.status.to_i
retry_statuses.any? do |matcher|
case matcher
when Range then matcher.include?(response_status)
Bertg marked this conversation as resolved.
Show resolved Hide resolved
when Numeric then matcher == response_status
else matcher.call(response_status)
end
end
end
}
end
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/http/retriable/performer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,47 @@ def measure_wait
end

describe "expected status codes" do

def response(options = {})
HTTP::Response.new(
{
:status => 200,
:version => "1.1",
:headers => {},
:body => "Hello world!",
:uri => "http://example.com/",
:request => request
}.merge(options)
)
end

it "retries the request" do
expect do
perform(:retry_statuses => [200], :tries => 2)
end.to raise_error HTTP::OutOfRetriesError

expect(counter_spy).to eq 2
end

describe "status codes can be expressed in many ways" do

[
301,
[200, 301, 485],
250...400,
[250...Float::INFINITY],
->(status_code) { status_code == 301 },
[->(status_code) { status_code == 301 }]
].each do |retry_statuses|
it retry_statuses.to_s do
expect do
perform(:retry_statuses => retry_statuses, :tries => 2) do
response(:status => 301)
end
end.to raise_error HTTP::OutOfRetriesError
end
end
end
end

describe "unexpected status code" do
Expand Down