14
14
import java .util .TreeSet ;
15
15
import java .util .Map ;
16
16
import java .util .HashMap ;
17
- import java .util .concurrent .ConcurrentHashMap ;
17
+ import java .util .concurrent .* ;
18
18
19
19
class AadInstanceDiscoveryProvider {
20
20
@@ -60,23 +60,21 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
60
60
ServiceBundle serviceBundle ) {
61
61
String host = authorityUrl .getHost ();
62
62
63
- if (shouldUseRegionalEndpoint (msalRequest )) {
64
- //Server side telemetry requires the result from region discovery when any part of the region API is used
65
- String detectedRegion = discoverRegion (msalRequest , serviceBundle );
63
+ ExecutorService executor = Executors .newSingleThreadExecutor ();
66
64
67
- if (msalRequest .application ().azureRegion () != null ) {
68
- host = getRegionalizedHost (authorityUrl .getHost (), msalRequest .application ().azureRegion ());
69
- }
65
+ Future <String > future = executor .submit (() -> performRegionalDiscovery (authorityUrl , msalRequest , serviceBundle ));
70
66
71
- //If region autodetection is enabled and a specific region not already set,
72
- // set the application's region to the discovered region so that future requests can skip the IMDS endpoint call
73
- if (null == msalRequest .application ().azureRegion () && msalRequest .application ().autoDetectRegion ()
74
- && null != detectedRegion ) {
75
- msalRequest .application ().azureRegion = detectedRegion ;
76
- }
77
- cacheRegionInstanceMetadata (authorityUrl .getHost (), msalRequest .application ().azureRegion ());
78
- serviceBundle .getServerSideTelemetry ().getCurrentRequest ().regionOutcome (
79
- determineRegionOutcome (detectedRegion , msalRequest .application ().azureRegion (), msalRequest .application ().autoDetectRegion ()));
67
+ try {
68
+ log .info ("Starting call to IMDS endpoint." );
69
+ host = future .get (2 , TimeUnit .SECONDS );
70
+ } catch (TimeoutException ex ) {
71
+ log .info ("Cancelled call to IMDS endpoint after waiting for 2 seconds" );
72
+ future .cancel (true );
73
+ } catch (Exception ex ) {
74
+ // handle other exceptions
75
+ log .info ("Exception while calling IMDS endpoint" + ex .getMessage ());
76
+ } finally {
77
+ executor .shutdownNow ();
80
78
}
81
79
82
80
InstanceDiscoveryMetadataEntry result = cache .get (host );
@@ -97,6 +95,32 @@ static InstanceDiscoveryMetadataEntry getMetadataEntry(URL authorityUrl,
97
95
return cache .get (host );
98
96
}
99
97
98
+ private static String performRegionalDiscovery (URL authorityUrl , MsalRequest msalRequest , ServiceBundle serviceBundle ){
99
+
100
+ String host = authorityUrl .getHost ();
101
+
102
+ if (shouldUseRegionalEndpoint (msalRequest )) {
103
+ //Server side telemetry requires the result from region discovery when any part of the region API is used
104
+ String detectedRegion = discoverRegion (msalRequest , serviceBundle );
105
+
106
+ if (msalRequest .application ().azureRegion () != null ) {
107
+ host = getRegionalizedHost (authorityUrl .getHost (), msalRequest .application ().azureRegion ());
108
+ }
109
+
110
+ //If region autodetection is enabled and a specific region not already set,
111
+ // set the application's region to the discovered region so that future requests can skip the IMDS endpoint call
112
+ if (null == msalRequest .application ().azureRegion () && msalRequest .application ().autoDetectRegion ()
113
+ && null != detectedRegion ) {
114
+ msalRequest .application ().azureRegion = detectedRegion ;
115
+ }
116
+ cacheRegionInstanceMetadata (authorityUrl .getHost (), msalRequest .application ().azureRegion ());
117
+ serviceBundle .getServerSideTelemetry ().getCurrentRequest ().regionOutcome (
118
+ determineRegionOutcome (detectedRegion , msalRequest .application ().azureRegion (), msalRequest .application ().autoDetectRegion ()));
119
+ }
120
+
121
+ return host ;
122
+ }
123
+
100
124
static Set <String > getAliases (String host ) {
101
125
if (cache .containsKey (host )) {
102
126
return cache .get (host ).aliases ();
@@ -299,7 +323,7 @@ private static String discoverRegion(MsalRequest msalRequest, ServiceBundle serv
299
323
300
324
//If call to IMDS endpoint was successful, return region from response body
301
325
if (httpResponse .statusCode () == HttpHelper .HTTP_STATUS_200 && !httpResponse .body ().isEmpty ()) {
302
- log .info ("Region retrieved from IMDS endpoint: " + httpResponse .body ());
326
+ log .info (String . format ( "Region retrieved from IMDS endpoint: %s" , httpResponse .body () ));
303
327
currentRequest .regionSource (RegionTelemetry .REGION_SOURCE_IMDS .telemetryValue );
304
328
305
329
return httpResponse .body ();
0 commit comments