Skip to content

Commit

Permalink
Set connect/read timeout when calling the metadata service
Browse files Browse the repository at this point in the history
Otherwise callers of these methods can be waiting indefinitely.

googleapis#4398
  • Loading branch information
dmichel1 committed Jan 26, 2019
1 parent f4bac2f commit 88e049b
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 88e049b

Please sign in to comment.