Skip to content

Commit

Permalink
Reduced timeout and log output for IMDS timeout (#192)
Browse files Browse the repository at this point in the history
* reduced timeout and log output for imds

* separated read and connect timeouts
  • Loading branch information
willarmiros authored Aug 10, 2020
1 parent a460106 commit 3d2c733
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
Expand All @@ -46,7 +47,8 @@ enum EC2Metadata {
AMI_ID,
}

private static final int TIMEOUT_MILLIS = 2000;
private static final int CONNECT_TIMEOUT_MILLIS = 100;
private static final int READ_TIMEOUT_MILLIS = 1000;
private static final String DEFAULT_IMDS_ENDPOINT = "169.254.169.254";

private final URL identityDocumentUrl;
Expand Down Expand Up @@ -142,8 +144,8 @@ private static String fetchString(String httpMethod, URL url, String token, bool
return "";
}

connection.setConnectTimeout(TIMEOUT_MILLIS);
connection.setReadTimeout(TIMEOUT_MILLIS);
connection.setConnectTimeout(CONNECT_TIMEOUT_MILLIS);
connection.setReadTimeout(READ_TIMEOUT_MILLIS);

if (includeTtl) {
connection.setRequestProperty("X-aws-ec2-metadata-token-ttl-seconds", "60");
Expand All @@ -156,7 +158,11 @@ private static String fetchString(String httpMethod, URL url, String token, bool
try {
responseCode = connection.getResponseCode();
} catch (Exception e) {
logger.warn("Error connecting to IMDS.", e);
if (e instanceof SocketTimeoutException) {
logger.debug("Timed out trying to connect to IMDS, likely not operating in EC2 environment");
} else {
logger.warn("Error connecting to IMDS.", e);
}
return "";
}

Expand Down

0 comments on commit 3d2c733

Please sign in to comment.