-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8cbb68
commit 2c75906
Showing
3 changed files
with
70 additions
and
0 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
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,41 @@ | ||
require 'gh' | ||
|
||
module GH | ||
# Public: In some cases, the GitHub API can take some number of | ||
# seconds to reflect a change in the system. This class catches 404 | ||
# responses for any GET request and retries the request. | ||
class Retry < Wrapper | ||
DEFAULTS = { | ||
retries: 5, | ||
wait: 1 | ||
} | ||
|
||
attr_accessor :retries, :wait | ||
|
||
def initialize(backend = nil, options = {}) | ||
options = DEFAULTS.merge options | ||
super backend, options | ||
end | ||
|
||
def fetch_resource(key) | ||
begin | ||
decrement_retries! | ||
super key | ||
rescue GH::Error(response_status: 404) => e | ||
retries_remaining? or raise e | ||
sleep wait | ||
fetch_resource key | ||
end | ||
end | ||
|
||
private | ||
|
||
def decrement_retries! | ||
self.retries = self.retries - 1 | ||
end | ||
|
||
def retries_remaining? | ||
retries > 0 | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'spec_helper' | ||
|
||
describe GH::Retry do | ||
let(:not_finder) do | ||
Class.new(GH::MockBackend) do | ||
def fetch_resource(key) | ||
if key =~ %r{users/not-found} | ||
@requests << key | ||
error = Struct.new(:info).new(response_status: 404) | ||
raise GH::Error.new(error) | ||
end | ||
super(key) | ||
end | ||
end | ||
end | ||
|
||
subject { described_class.new(not_finder.new, retries: 3, wait: 0.1) } | ||
|
||
it 'retries request specified number of times' do | ||
expect { subject['users/not-found'] }.to raise_error(GH::Error) | ||
expect(subject.backend.requests.count).to eq 3 | ||
end | ||
|
||
it 'does not retry when response is successful' do | ||
subject['users/rkh'] | ||
expect(subject.backend.requests.count).to eq 1 | ||
end | ||
end |