@@ -80,7 +80,7 @@ private DefaultEc2MetadataClientWithFallback(Ec2MetadataBuilder builder) {
80
80
81
81
this .imdsV1FallbackEnabled = !resolveImdsV1Disabled ();
82
82
83
- this .tokenCache = CachedSupplier .builder (() -> RefreshResult .builder (this .getToken ())
83
+ this .tokenCache = CachedSupplier .builder (() -> RefreshResult .builder (this .getTokenWithFallback ())
84
84
.staleTime (Instant .now ().plus (tokenTtl ))
85
85
.build ())
86
86
.cachedValueName (toString ())
@@ -122,7 +122,7 @@ public Ec2MetadataResponse get(String path) {
122
122
RetryPolicyContext retryPolicyContext = RetryPolicyContext .builder ().retriesAttempted (attempt ).build ();
123
123
try {
124
124
if (token == null || token .isExpired ()) {
125
- token = getTokenWithFallback ();
125
+ token = tokenCache . get ();
126
126
}
127
127
return sendRequest (path , token != null ? token .value () : null );
128
128
} catch (UncheckedIOException | RetryableException e ) {
@@ -154,13 +154,13 @@ public Ec2MetadataResponse get(String path) {
154
154
}
155
155
156
156
/**
157
- * Gets token with fallback logic that matches EC2MetadataUtils behavior .
157
+ * Gets token with fallback logic that can be cached .
158
158
* If token retrieval fails with 400 error, throws exception.
159
159
* Otherwise, returns null to indicate fallback to IMDSv1.
160
160
*/
161
161
private Token getTokenWithFallback () {
162
162
try {
163
- return tokenCache . get ();
163
+ return getToken ();
164
164
} catch (Exception e ) {
165
165
boolean is400ServiceException = e instanceof Ec2MetadataClientException
166
166
&& ((Ec2MetadataClientException ) e ).statusCode () == 400 ;
0 commit comments