Skip to content

Commit 216c7c9

Browse files
author
Jon Wayne Parrott
committed
Add configurable timeout for metadata server.
1 parent bb2386e commit 216c7c9

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

oauth2client/client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@
102102
# If set to True _get_environment avoid GCE check (_detect_gce_environment)
103103
NO_GCE_CHECK = os.environ.setdefault('NO_GCE_CHECK', 'False')
104104

105+
# Timeout in seconds to wait for the GCE metadata server when detecting the
106+
# GCE environment.
107+
try:
108+
GCE_METADATA_TIMEOUT = int(
109+
os.environ.setdefault('GCE_METADATA_TIMEOUT', '3'))
110+
except ValueError: # pragma: NO COVER
111+
GCE_METADATA_TIMEOUT = 3
112+
105113
_SERVER_SOFTWARE = 'SERVER_SOFTWARE'
106114
_GCE_METADATA_HOST = '169.254.169.254'
107115
_METADATA_FLAVOR_HEADER = 'Metadata-Flavor'
@@ -998,7 +1006,7 @@ def _detect_gce_environment():
9981006
# the metadata resolution was particularly slow. The latter case is
9991007
# "unlikely".
10001008
connection = six.moves.http_client.HTTPConnection(
1001-
_GCE_METADATA_HOST, timeout=1)
1009+
_GCE_METADATA_HOST, timeout=GCE_METADATA_TIMEOUT)
10021010

10031011
try:
10041012
headers = {_METADATA_FLAVOR_HEADER: _DESIRED_METADATA_FLAVOR}

tests/test_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ def _environment_check_gce_helper(self, status_ok=True, socket_error=False,
399399

400400
if server_software == '':
401401
http_client_module.HTTPConnection.assert_called_once_with(
402-
client._GCE_METADATA_HOST, timeout=1)
402+
client._GCE_METADATA_HOST,
403+
timeout=client.GCE_METADATA_TIMEOUT)
403404
connection.getresponse.assert_called_once_with()
404405
# Remaining calls are not "getresponse"
405406
headers = {

0 commit comments

Comments
 (0)