Skip to content

Commit b8865df

Browse files
committed
timeout digital ocean
1 parent d6d93bb commit b8865df

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

inject/src/main/java/io/micronaut/context/env/DefaultEnvironment.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public class DefaultEnvironment extends PropertySourcePropertyResolver implement
7373
private static final String K8S_ENV = "KUBERNETES_SERVICE_HOST";
7474
private static final String PCF_ENV = "VCAP_SERVICES";
7575
private static final String HEROKU_DYNO = "DYNO";
76+
private static final int DEFAULT_READ_TIMEOUT = 500;
77+
private static final int DEFAULT_CONNECT_TIMEOUT = 500;
78+
public static final String DIGITAL_OCEAN_URL = "http://169.254.169.254/metadata/v1.json";
79+
public static final String GOOGLE_COMPUTE_METADATA = "http://metadata.google.internal";
7680

7781
protected final ClassPathResourceLoader resourceLoader;
7882

@@ -732,10 +736,7 @@ private static ComputePlatform determineCloudProvider() {
732736
@SuppressWarnings("MagicNumber")
733737
private static boolean isGoogleCompute() {
734738
try {
735-
URL url = new URL("http://metadata.google.internal");
736-
HttpURLConnection con = (HttpURLConnection) url.openConnection();
737-
con.setReadTimeout(500);
738-
con.setConnectTimeout(500);
739+
final HttpURLConnection con = instantiateHttpURLConnectionForEnvironmentCheck(GOOGLE_COMPUTE_METADATA);
739740
con.setRequestMethod("GET");
740741
con.setDoOutput(true);
741742
int responseCode = con.getResponseCode();
@@ -805,10 +806,17 @@ private static boolean isEC2Windows() {
805806
return false;
806807
}
807808

809+
private static HttpURLConnection instantiateHttpURLConnectionForEnvironmentCheck(String spec) throws IOException {
810+
final URL url = new URL(spec);
811+
final HttpURLConnection con = (HttpURLConnection) url.openConnection();
812+
con.setReadTimeout(DEFAULT_READ_TIMEOUT);
813+
con.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
814+
return con;
815+
}
816+
808817
private static boolean isDigitalOcean() {
809818
try {
810-
final URL url = new URL("http://169.254.169.254/metadata/v1.json");
811-
final HttpURLConnection con = (HttpURLConnection) url.openConnection();
819+
final HttpURLConnection con = instantiateHttpURLConnectionForEnvironmentCheck(DIGITAL_OCEAN_URL);
812820
con.setRequestMethod("HEAD");
813821
int responseCode = con.getResponseCode();
814822
return responseCode == 200;

0 commit comments

Comments
 (0)