Skip to content
10 changes: 5 additions & 5 deletions google/auth/compute_engine/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@

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

# Detect GCE Residency
_GOOGLE = "Google"
Expand Down Expand Up @@ -100,7 +97,7 @@ def detect_gce_residency_linux():
return content.startswith(_GOOGLE)


def ping(request, timeout=_METADATA_DEFAULT_TIMEOUT, retry_count=3):
def ping(request, timeout=None, retry_count=3):
"""Checks to see if the metadata server is available.

Args:
Expand All @@ -119,6 +116,9 @@ def ping(request, timeout=_METADATA_DEFAULT_TIMEOUT, retry_count=3):
# 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".
if timeout is None:
timeout = float(os.getenv("GCE_METADATA_TIMEOUT", _METADATA_DEFAULT_TIMEOUT))

retries = 0
headers = _METADATA_HEADERS.copy()
headers[metrics.API_CLIENT_HEADER] = metrics.mds_ping()
Expand Down