Skip to content

Commit

Permalink
Merge pull request #97 from serihiro/retry-request-with-unexpected-error
Browse files Browse the repository at this point in the history
Retry http request when unexpected error occurs
  • Loading branch information
Heng Xiong committed Jul 13, 2017
2 parents 0f7fe1e + 227ba09 commit 8665196
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 19 deletions.
24 changes: 14 additions & 10 deletions lib/googleauth/compute_engine.rb
Expand Up @@ -88,16 +88,20 @@ def on_gce?(options = {})
def fetch_access_token(options = {})
c = options[:connection] || Faraday.default_connection
c.headers = { 'Metadata-Flavor' => 'Google' }
resp = c.get(COMPUTE_AUTH_TOKEN_URI)
case resp.status
when 200
Signet::OAuth2.parse_credentials(resp.body,
resp.headers['content-type'])
when 404
raise(Signet::AuthorizationError, NO_METADATA_SERVER_ERROR)
else
msg = "Unexpected error code #{resp.status}" + UNEXPECTED_ERROR_SUFFIX
raise(Signet::AuthorizationError, msg)

retry_with_error do
resp = c.get(COMPUTE_AUTH_TOKEN_URI)
case resp.status
when 200
Signet::OAuth2.parse_credentials(resp.body,
resp.headers['content-type'])
when 404
raise(Signet::AuthorizationError, NO_METADATA_SERVER_ERROR)
else
msg = "Unexpected error code #{resp.status}" \
"#{UNEXPECTED_ERROR_SUFFIX}"
raise(Signet::AuthorizationError, msg)
end
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions lib/googleauth/signet.rb
Expand Up @@ -77,6 +77,27 @@ def notify_refresh_listeners
block.call(self)
end
end

def retry_with_error(max_retry_count = 5)
retry_count = 0

begin
yield
rescue => e
if e.is_a?(Signet::AuthorizationError) || e.is_a?(Signet::ParseError)
raise e
end

if retry_count < max_retry_count
retry_count += 1
sleep retry_count * 0.3
retry
else
msg = "Unexpected error: #{e.inspect}"
raise(Signet::AuthorizationError, msg)
end
end
end
end
end
end
21 changes: 12 additions & 9 deletions lib/googleauth/user_refresh.rb
Expand Up @@ -92,15 +92,18 @@ def initialize(options = {})
# Revokes the credential
def revoke!(options = {})
c = options[:connection] || Faraday.default_connection
resp = c.get(REVOKE_TOKEN_URI, token: refresh_token || access_token)
case resp.status
when 200
self.access_token = nil
self.refresh_token = nil
self.expires_at = 0
else
raise(Signet::AuthorizationError,
"Unexpected error code #{resp.status}")

retry_with_error do
resp = c.get(REVOKE_TOKEN_URI, token: refresh_token || access_token)
case resp.status
when 200
self.access_token = nil
self.refresh_token = nil
self.expires_at = 0
else
raise(Signet::AuthorizationError,
"Unexpected error code #{resp.status}")
end
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/googleauth/compute_engine_spec.rb
Expand Up @@ -77,6 +77,20 @@ def make_auth_stubs(opts = {})
.to raise_error Signet::AuthorizationError
expect(stub).to have_been_requested
end

it 'should fail with Signet::AuthorizationError if request times out' do
allow_any_instance_of(Faraday::Connection).to receive(:get)
.and_raise(Faraday::TimeoutError)
expect { @client.fetch_access_token! }
.to raise_error Signet::AuthorizationError
end

it 'should fail with Signet::AuthorizationError if request fails' do
allow_any_instance_of(Faraday::Connection).to receive(:get)
.and_raise(Faraday::ConnectionFailed, nil)
expect { @client.fetch_access_token! }
.to raise_error Signet::AuthorizationError
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/googleauth/user_refresh_spec.rb
Expand Up @@ -293,4 +293,20 @@ def cred_json_text(missing = nil)
)
end
end

describe 'when erros occured with request' do
it 'should fail with Signet::AuthorizationError if request times out' do
allow_any_instance_of(Faraday::Connection).to receive(:get)
.and_raise(Faraday::TimeoutError)
expect { @client.revoke! }
.to raise_error Signet::AuthorizationError
end

it 'should fail with Signet::AuthorizationError if request fails' do
allow_any_instance_of(Faraday::Connection).to receive(:get)
.and_raise(Faraday::ConnectionFailed, nil)
expect { @client.revoke! }
.to raise_error Signet::AuthorizationError
end
end
end

0 comments on commit 8665196

Please sign in to comment.