Skip to content

Commit

Permalink
Set connect/read timeout when calling the metadata service (#4413)
Browse files Browse the repository at this point in the history
Otherwise callers of these methods can be waiting indefinitely.
  • Loading branch information
dmichel1 authored and ajaaym committed Feb 8, 2019
1 parent d6b2c21 commit 44db769
Showing 1 changed file with 3 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public class MetadataConfig {

private static final String METADATA_URL = "http://metadata.google.internal/computeMetadata/v1/";
private static final int TIMEOUT_MS = 5000;

private MetadataConfig() {}

Expand Down Expand Up @@ -69,6 +70,8 @@ public static String getAttribute(String attributeName) {
try {
URL url = new URL(METADATA_URL + attributeName);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(TIMEOUT_MS);
connection.setReadTimeout(TIMEOUT_MS);
connection.setRequestProperty("Metadata-Flavor", "Google");
InputStream input = connection.getInputStream();
if (connection.getResponseCode() == 200) {
Expand Down

0 comments on commit 44db769

Please sign in to comment.