Adds an explicit short timeout to on_gce?

- uses the current timeout in oauth2client/client.py
- includes the comments from that file as well
This commit is contained in:
Tim Emiola 2015-02-11 21:03:37 -08:00
parent e01b14cba8
commit b01737abe2
1 changed files with 12 additions and 2 deletions

View File

@ -50,11 +50,21 @@ module Google
# is available # is available
def on_gce?(options = {}) def on_gce?(options = {})
c = options[:connection] || Faraday.default_connection c = options[:connection] || Faraday.default_connection
resp = c.get(COMPUTE_CHECK_URI) resp = c.get(COMPUTE_CHECK_URI) do |req|
# Comment from: oauth2client/client.py
#
# Note: the explicit `timeout` below is a workaround. The underlying
# issue is that resolving an unknown host on some networks will take
# 20-30 seconds; making this timeout short fixes the issue, but
# could lead to false negatives in the event that we are on GCE, but
# the metadata resolution was particularly slow. The latter case is
# "unlikely".
req.options.timeout = 0.1
end
return false unless resp.status == 200 return false unless resp.status == 200
return false unless resp.headers.key?('Metadata-Flavor') return false unless resp.headers.key?('Metadata-Flavor')
return resp.headers['Metadata-Flavor'] == 'Google' return resp.headers['Metadata-Flavor'] == 'Google'
rescue Faraday::ConnectionFailed rescue [Faraday::TimeoutError, Faraday::ConnectionFailed]
return false return false
end end
memoize :on_gce? memoize :on_gce?