Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Add configurable timeout for metadata server. #571

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion oauth2client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@
# If set to True _get_environment avoid GCE check (_detect_gce_environment)
NO_GCE_CHECK = os.environ.setdefault('NO_GCE_CHECK', 'False')

# Timeout in seconds to wait for the GCE metadata server when detecting the
# GCE environment.
try:
GCE_METADATA_TIMEOUT = int(
os.environ.setdefault('GCE_METADATA_TIMEOUT', '3'))
except ValueError: # pragma: NO COVER
GCE_METADATA_TIMEOUT = 3

_SERVER_SOFTWARE = 'SERVER_SOFTWARE'
_GCE_METADATA_HOST = '169.254.169.254'
_METADATA_FLAVOR_HEADER = 'Metadata-Flavor'
Expand Down Expand Up @@ -998,7 +1006,7 @@ def _detect_gce_environment():
# the metadata resolution was particularly slow. The latter case is
# "unlikely".
connection = six.moves.http_client.HTTPConnection(
_GCE_METADATA_HOST, timeout=1)
_GCE_METADATA_HOST, timeout=GCE_METADATA_TIMEOUT)

try:
headers = {_METADATA_FLAVOR_HEADER: _DESIRED_METADATA_FLAVOR}
Expand Down
3 changes: 2 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ def _environment_check_gce_helper(self, status_ok=True, socket_error=False,

if server_software == '':
http_client_module.HTTPConnection.assert_called_once_with(
client._GCE_METADATA_HOST, timeout=1)
client._GCE_METADATA_HOST,
timeout=client.GCE_METADATA_TIMEOUT)
connection.getresponse.assert_called_once_with()
# Remaining calls are not "getresponse"
headers = {
Expand Down