Skip to content

Commit 1894fed

Browse files
committed
PR feedback
1 parent 072a160 commit 1894fed

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/DefaultRetryPolicy.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
class DefaultRetryPolicy implements IRetryPolicy {
1010
private static final int RETRY_NUM = 1;
11-
private static int RETRY_DELAY_MS = 1000;
11+
private static final int RETRY_DELAY_MS = 1000;
1212

1313
@Override
1414
public boolean isRetryable(IHttpResponse httpResponse) {
@@ -25,13 +25,4 @@ public int getMaxRetryCount(IHttpResponse httpResponse) {
2525
public int getRetryDelayMs(IHttpResponse httpResponse) {
2626
return RETRY_DELAY_MS;
2727
}
28-
29-
//Package-private methods to allow much quicker testing. The delay values should be treated as constants in any non-test scenario.
30-
static void setRetryDelayMs(int retryDelayMs) {
31-
RETRY_DELAY_MS = retryDelayMs;
32-
}
33-
34-
static void resetToDefaults() {
35-
RETRY_DELAY_MS = 1000;
36-
}
3728
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/IMDSRetryPolicy.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
//IMDS uses a different try policy than other MI flows, see https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/main/docs/imds_retry_based_on_errors.md
1212
class IMDSRetryPolicy extends ManagedIdentityRetryPolicy {
1313
private static final int LINEAR_RETRY_NUM = 7;
14-
private static int LINEAR_RETRY_DELAY_MS = 10000; // 10 seconds
14+
private static final int LINEAR_RETRY_DELAY_MS = 10000; // 10 seconds
1515
private static final int EXPONENTIAL_RETRY_NUM = 3;
16-
private static int EXPONENTIAL_RETRY_DELAY_MS = 1000; // 1 second
16+
private static final int EXPONENTIAL_RETRY_DELAY_MS = 1000; // 1 second
17+
18+
private static int currentLinearRetryDelayMs = LINEAR_RETRY_DELAY_MS;
19+
private static int exponentialLinearRetryDelayMs = EXPONENTIAL_RETRY_DELAY_MS;
1720

1821
private int currentRetryCount;
1922
private int lastStatusCode;
@@ -37,27 +40,27 @@ public boolean isRetryable(IHttpResponse httpResponse) {
3740

3841
@Override
3942
public int getMaxRetryCount(IHttpResponse httpResponse) {
40-
return (httpResponse.statusCode() == 410) ? LINEAR_RETRY_NUM : EXPONENTIAL_RETRY_NUM;
43+
return (httpResponse.statusCode() == HttpStatus.GONE.getCode()) ? LINEAR_RETRY_NUM : EXPONENTIAL_RETRY_NUM;
4144
}
4245

4346
@Override
4447
public int getRetryDelayMs(IHttpResponse httpResponse) {
4548
// Use exponential backoff for non-410 status codes
46-
if (lastStatusCode == 410) {
47-
return LINEAR_RETRY_DELAY_MS;
49+
if (lastStatusCode == HttpStatus.GONE.getCode()) {
50+
return currentLinearRetryDelayMs;
4851
} else {
49-
return (int) (Math.pow(2, currentRetryCount) * EXPONENTIAL_RETRY_DELAY_MS);
52+
return (int) (Math.pow(2, currentRetryCount) * exponentialLinearRetryDelayMs);
5053
}
5154
}
5255

5356
//Package-private methods to allow much quicker testing. The delay values should be treated as constants in any non-test scenario.
5457
static void setRetryDelayMs(int retryDelayMs) {
55-
LINEAR_RETRY_DELAY_MS = retryDelayMs;
56-
EXPONENTIAL_RETRY_DELAY_MS = retryDelayMs;
58+
currentLinearRetryDelayMs = retryDelayMs;
59+
exponentialLinearRetryDelayMs = retryDelayMs;
5760
}
5861

5962
static void resetToDefaults() {
60-
LINEAR_RETRY_DELAY_MS = 10000;
61-
EXPONENTIAL_RETRY_DELAY_MS = 1000;
63+
currentLinearRetryDelayMs = LINEAR_RETRY_DELAY_MS;
64+
exponentialLinearRetryDelayMs = EXPONENTIAL_RETRY_DELAY_MS;
6265
}
6366
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/ManagedIdentityRetryPolicy.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
*/
1414
class ManagedIdentityRetryPolicy implements IRetryPolicy {
1515
private static final int RETRY_NUM = 3;
16-
private static int RETRY_DELAY_MS = 1000;
16+
private static final int RETRY_DELAY_MS = 1000;
17+
18+
private static int currentRetryDelayMs = RETRY_DELAY_MS;
1719

1820
private static final Set<Integer> RETRYABLE_STATUS_CODES = Collections.unmodifiableSet(
1921
new HashSet<>(Arrays.asList(
@@ -38,15 +40,15 @@ public int getMaxRetryCount(IHttpResponse httpResponse) {
3840

3941
@Override
4042
public int getRetryDelayMs(IHttpResponse httpResponse) {
41-
return RETRY_DELAY_MS;
43+
return currentRetryDelayMs;
4244
}
4345

4446
//Package-private methods to allow much quicker testing. The delay values should be treated as constants in any non-test scenario.
4547
static void setRetryDelayMs(int retryDelayMs) {
46-
RETRY_DELAY_MS = retryDelayMs;
48+
currentRetryDelayMs = retryDelayMs;
4749
}
4850

4951
static void resetToDefaults() {
50-
RETRY_DELAY_MS = 1000;
52+
currentRetryDelayMs = RETRY_DELAY_MS;
5153
}
5254
}

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/ManagedIdentityTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ void managedIdentityTest_IMDSRetry() throws Exception {
600600
.build();
601601

602602
// IMDS has different retry logic for certain status codes, such as 410
603-
when(httpClientMock.send(expectedRequest(IMDS, ManagedIdentityTestConstants.RESOURCE))).thenReturn(expectedResponse(410, ManagedIdentityTestConstants.MSI_ERROR_RESPONSE_500));
603+
when(httpClientMock.send(expectedRequest(IMDS, ManagedIdentityTestConstants.RESOURCE))).thenReturn(expectedResponse(HttpStatus.GONE.getCode(), ManagedIdentityTestConstants.MSI_ERROR_RESPONSE_500));
604604

605605
try {
606606
acquireTokenCommon(ManagedIdentityTestConstants.RESOURCE).get();
@@ -612,7 +612,7 @@ void managedIdentityTest_IMDSRetry() throws Exception {
612612
}
613613

614614
clearInvocations(httpClientMock);
615-
when(httpClientMock.send(expectedRequest(IMDS, ManagedIdentityTestConstants.RESOURCE))).thenReturn(expectedResponse(500, ManagedIdentityTestConstants.MSI_ERROR_RESPONSE_500));
615+
when(httpClientMock.send(expectedRequest(IMDS, ManagedIdentityTestConstants.RESOURCE))).thenReturn(expectedResponse(HttpStatus.INTERNAL_SERVER_ERROR.getCode(), ManagedIdentityTestConstants.MSI_ERROR_RESPONSE_500));
616616

617617
try {
618618
acquireTokenCommon(ManagedIdentityTestConstants.RESOURCE).get();
@@ -642,8 +642,8 @@ void managedIdentityTest_RetrySucceedsAfterFailure() throws Exception {
642642

643643
// First call returns 500, subsequent calls return 200
644644
when(httpClientMock.send(expectedRequest(IMDS, ManagedIdentityTestConstants.RESOURCE)))
645-
.thenReturn(expectedResponse(500, ManagedIdentityTestConstants.MSI_ERROR_RESPONSE_500))
646-
.thenReturn(expectedResponse(200, getSuccessfulResponse(ManagedIdentityTestConstants.RESOURCE)));
645+
.thenReturn(expectedResponse(HttpStatus.INTERNAL_SERVER_ERROR.getCode(), ManagedIdentityTestConstants.MSI_ERROR_RESPONSE_500))
646+
.thenReturn(expectedResponse(HttpStatus.OK.getCode(), getSuccessfulResponse(ManagedIdentityTestConstants.RESOURCE)));
647647

648648
IAuthenticationResult result = acquireTokenCommon(ManagedIdentityTestConstants.RESOURCE).get();
649649

@@ -697,8 +697,8 @@ void managedIdentityTest_RetriesARePerRequest() throws Exception {
697697

698698
// First call returns 500, subsequent calls return 200
699699
when(httpClientMock.send(expectedRequest(IMDS, ManagedIdentityTestConstants.RESOURCE)))
700-
.thenReturn(expectedResponse(500, ManagedIdentityTestConstants.MSI_ERROR_RESPONSE_500))
701-
.thenReturn(expectedResponse(200, getSuccessfulResponse(ManagedIdentityTestConstants.RESOURCE)));
700+
.thenReturn(expectedResponse(HttpStatus.INTERNAL_SERVER_ERROR.getCode(), ManagedIdentityTestConstants.MSI_ERROR_RESPONSE_500))
701+
.thenReturn(expectedResponse(HttpStatus.OK.getCode(), getSuccessfulResponse(ManagedIdentityTestConstants.RESOURCE)));
702702

703703
IAuthenticationResult result = acquireTokenCommon(ManagedIdentityTestConstants.RESOURCE).get();
704704

@@ -710,7 +710,7 @@ void managedIdentityTest_RetriesARePerRequest() throws Exception {
710710

711711
//All calls return 500
712712
when(httpClientMock.send(expectedRequest(IMDS, "otherResource")))
713-
.thenReturn(expectedResponse(500, ManagedIdentityTestConstants.MSI_ERROR_RESPONSE_500));
713+
.thenReturn(expectedResponse(HttpStatus.INTERNAL_SERVER_ERROR.getCode(), ManagedIdentityTestConstants.MSI_ERROR_RESPONSE_500));
714714

715715
CompletableFuture<IAuthenticationResult> future = acquireTokenCommon("otherResource");
716716

0 commit comments

Comments
 (0)