-
Notifications
You must be signed in to change notification settings - Fork 378
Reworked retry policy functionality & Created IMDS retry policy #5231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
79 commits
Select commit
Hold shift + click to select a range
12e89cc
Retry policies are now pre request instead of per HttpManager.
Robbie-Microsoft 2d3b435
Merge branch 'main' into rginsburg_retry_policy_per_request
Robbie-Microsoft 132ec53
Improved unit test
Robbie-Microsoft fc7eafa
Addressed some GitHub feedback
Robbie-Microsoft dc831fe
Implemented GitHub feedback
Robbie-Microsoft 6576722
Fixed broken unit tests, update public API
Robbie-Microsoft 6d89f1c
Undid public API changes
Robbie-Microsoft a8cdac3
Merge branch 'main' into rginsburg_retry_policy_per_request
Robbie-Microsoft b68df2c
Fixed broken unit test and clarified public api
Robbie-Microsoft aab8940
removed comment, adjusted unit test
Robbie-Microsoft 6e9cc07
Merge branch 'main' into rginsburg_retry_policy_per_request
Robbie-Microsoft 4aedf33
Changed constants to PascalCase
Robbie-Microsoft 4266a14
Deleted file
Robbie-Microsoft 2153fc3
Undid file change from main
Robbie-Microsoft b82f872
Reworked retry policy functionality. Created IMDS retry policy.
Robbie-Microsoft 8b37d08
Implemented GitHub Feedback
Robbie-Microsoft abe860f
Created first retry unit test. The rest of the tests will be based of…
Robbie-Microsoft 0eef7c6
Added more ImdsRetryPolicy unit tests. Fixed bug discovered by unit t…
Robbie-Microsoft 9faf287
Finished all ImdsRetryPolicy unit tests
Robbie-Microsoft 128f628
Discovered bugs through unit tests, and fixed them. Improved ImdsRetr…
Robbie-Microsoft 69a157a
Minor improvements to ImdsRetryPolicy tests
Robbie-Microsoft fe3ac7c
Added option retryAfter header to mockHttpManager. Added more Defauly…
Robbie-Microsoft e73a7bb
Finished DefaultRetryPolicy unit tests. Edited some ImdsRetryPolicy u…
Robbie-Microsoft d5f5978
rebased branch from main to rginsburg_retry_policy_per_request
Robbie-Microsoft c61c3e6
Implemented final feedback
Robbie-Microsoft be1d9e4
Merged in latest
Robbie-Microsoft 42ea075
Fixed some broken unit tests
Robbie-Microsoft 6fefdf2
Fixed all broken unit tests and made STS tests 100 times faster
Robbie-Microsoft b7e70d6
Merge branch 'main' into rginsburg/rginsburg_retry_policy_per_request
Robbie-Microsoft 717a44a
Merge branch 'rginsburg/rginsburg_retry_policy_per_request' into rgin…
Robbie-Microsoft 6671271
whitespace
Robbie-Microsoft 990847e
Retry policies are now pre request instead of per HttpManager.
Robbie-Microsoft fc7f97c
Addressed some GitHub feedback
Robbie-Microsoft b3cdc4b
Implemented GitHub feedback
Robbie-Microsoft 7ccb8c8
Fixed broken unit tests, update public API
Robbie-Microsoft 848987f
Undid public API changes
Robbie-Microsoft 383d318
Fixed broken unit test and clarified public api
Robbie-Microsoft ae69c1d
removed comment, adjusted unit test
Robbie-Microsoft f0d98ed
Deleted file
Robbie-Microsoft e13e0d8
Undid file change from main
Robbie-Microsoft 39164fb
Reworked retry policy functionality. Created IMDS retry policy.
Robbie-Microsoft 4cab624
Implemented GitHub Feedback
Robbie-Microsoft 39180d9
Created first retry unit test. The rest of the tests will be based of…
Robbie-Microsoft d2a01a1
Added more ImdsRetryPolicy unit tests. Fixed bug discovered by unit t…
Robbie-Microsoft 57afaf8
Finished all ImdsRetryPolicy unit tests
Robbie-Microsoft f629dfc
Discovered bugs through unit tests, and fixed them. Improved ImdsRetr…
Robbie-Microsoft 62c0b09
Minor improvements to ImdsRetryPolicy tests
Robbie-Microsoft c79f23d
Added option retryAfter header to mockHttpManager. Added more Defauly…
Robbie-Microsoft 25ce41b
Finished DefaultRetryPolicy unit tests. Edited some ImdsRetryPolicy u…
Robbie-Microsoft b33059c
rebased branch from main to rginsburg_retry_policy_per_request
Robbie-Microsoft 2487799
Fixed some broken unit tests
Robbie-Microsoft eb785d6
Fixed all broken unit tests and made STS tests 100 times faster
Robbie-Microsoft f5668c7
whitespace
Robbie-Microsoft ec2090d
Merge branch 'rginsburg/imds_retry_policy' of https://github.com/Azur…
Robbie-Microsoft 4cd60ed
Final adjustment of unit tests
Robbie-Microsoft 1854cd3
Simplified params for DefaultRetryPolicy
Robbie-Microsoft 001b32d
Implemented some GitHub feedback
Robbie-Microsoft a64391c
Merge branch 'main' into rginsburg/imds_retry_policy
Robbie-Microsoft bf41d92
Fixed build messages
Robbie-Microsoft d9a2dd3
adjusted unit test delay
Robbie-Microsoft f4433c7
Fixed build messages
Robbie-Microsoft fb9653a
removed unused import
Robbie-Microsoft 96a65cc
added temporary logs for debugging CI issues
Robbie-Microsoft 1be5bc1
edited temporary logs
Robbie-Microsoft be8d712
Merge branch 'main' into rginsburg/imds_retry_policy
Robbie-Microsoft 7c87c30
Created "retry" folder inside of "http" and updated all file paths (#…
Robbie-Microsoft 01bcb73
Created Production and Test Retry Policy Factories, and adjusted unit…
Robbie-Microsoft 735dc22
Merge branch 'main' into rginsburg/imds_retry_policy
Robbie-Microsoft 5e3ad09
Moved RequestType enum from Constants to IRetryPolicyFactory (#5316)
Robbie-Microsoft 960c9d9
Moved RetryPolicyFactory configuration from Managed Identity Builder …
Robbie-Microsoft 94dfbdd
Removed another piece that was left out in previous committ
Robbie-Microsoft 7b69caa
undid changes in ManagedIdentityBuilder
Robbie-Microsoft 126ccc4
Added RetryPolicyFactory to test mock
Robbie-Microsoft d92059a
Implemented Neha's feedback
Robbie-Microsoft 1f046dd
Merge branch 'main' into rginsburg/imds_retry_policy
Robbie-Microsoft 05d3d86
Implemented Gladwin's feedback
Robbie-Microsoft ce6349b
undid changes to .csproj
Robbie-Microsoft dbb5315
Implemented Travis's feedback
Robbie-Microsoft fdfdfb7
Moved RequestType enum to it's own file and updated imports
Robbie-Microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
src/client/Microsoft.Identity.Client/Http/LinearRetryPolicy.cs
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
src/client/Microsoft.Identity.Client/Http/Retry/DefaultRetryPolicy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Identity.Client.Core; | ||
|
|
||
| namespace Microsoft.Identity.Client.Http.Retry | ||
| { | ||
| class DefaultRetryPolicy : IRetryPolicy | ||
| { | ||
| // referenced in unit tests | ||
| public const int DefaultStsMaxRetries = 1; | ||
| public const int DefaultManagedIdentityMaxRetries = 3; | ||
|
|
||
| private const int DefaultStsRetryDelayMs = 1000; | ||
| private const int DefaultManagedIdentityRetryDelayMs = 1000; | ||
|
|
||
| public readonly int _defaultRetryDelayMs; | ||
| private readonly int _maxRetries; | ||
| private readonly Func<HttpResponse, Exception, bool> _retryCondition; | ||
| private readonly LinearRetryStrategy _linearRetryStrategy = new LinearRetryStrategy(); | ||
|
|
||
| public DefaultRetryPolicy(RequestType requestType) | ||
| { | ||
| switch (requestType) | ||
| { | ||
| case RequestType.ManagedIdentityDefault: | ||
| _defaultRetryDelayMs = DefaultManagedIdentityRetryDelayMs; | ||
| _maxRetries = DefaultManagedIdentityMaxRetries; | ||
| _retryCondition = HttpRetryConditions.DefaultManagedIdentity; | ||
| break; | ||
| case RequestType.STS: | ||
| _defaultRetryDelayMs = DefaultStsRetryDelayMs; | ||
| _maxRetries = DefaultStsMaxRetries; | ||
| _retryCondition = HttpRetryConditions.Sts; | ||
| break; | ||
| default: | ||
| throw new ArgumentOutOfRangeException(nameof(requestType), requestType, "Unknown request type"); | ||
| } | ||
| } | ||
|
|
||
| internal virtual Task DelayAsync(int milliseconds) | ||
| { | ||
| return Task.Delay(milliseconds); | ||
| } | ||
|
|
||
| public async Task<bool> PauseForRetryAsync(HttpResponse response, Exception exception, int retryCount, ILoggerAdapter logger) | ||
| { | ||
| // Check if the status code is retriable and if the current retry count is less than max retries | ||
| if (_retryCondition(response, exception) && | ||
| retryCount < _maxRetries) | ||
| { | ||
| // Use HeadersAsDictionary to check for "Retry-After" header | ||
| string retryAfter = string.Empty; | ||
| if (response?.HeadersAsDictionary != null) | ||
| { | ||
| response.HeadersAsDictionary.TryGetValue("Retry-After", out retryAfter); | ||
| } | ||
|
|
||
| int retryAfterDelay = _linearRetryStrategy.CalculateDelay(retryAfter, _defaultRetryDelayMs); | ||
|
|
||
| logger.Warning($"Retrying request in {retryAfterDelay}ms (retry attempt: {retryCount + 1})"); | ||
|
|
||
| // Pause execution for the calculated delay | ||
| await DelayAsync(retryAfterDelay).ConfigureAwait(false); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // If the status code is not retriable or max retries have been reached, do not retry | ||
| return false; | ||
| } | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
src/client/Microsoft.Identity.Client/Http/Retry/ExponentialRetryStrategy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Identity.Client.Http.Retry | ||
| { | ||
| internal class ExponentialRetryStrategy | ||
| { | ||
| // Minimum backoff time in milliseconds | ||
| private int _minExponentialBackoff; | ||
| // Maximum backoff time in milliseconds | ||
| private int _maxExponentialBackoff; | ||
| // Maximum backoff time in milliseconds | ||
| private int _exponentialDeltaBackoff; | ||
|
|
||
| public ExponentialRetryStrategy(int minExponentialBackoff, int maxExponentialBackoff, int exponentialDeltaBackoff) | ||
| { | ||
| _minExponentialBackoff = minExponentialBackoff; | ||
| _maxExponentialBackoff = maxExponentialBackoff; | ||
| _exponentialDeltaBackoff = exponentialDeltaBackoff; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Calculates the exponential delay based on the current retry attempt. | ||
| /// </summary> | ||
| /// <param name="currentRetry">The current retry attempt number.</param> | ||
| /// <returns>The calculated exponential delay in milliseconds.</returns> | ||
| /// <remarks> | ||
| /// The delay is calculated using the formula: | ||
| /// - If <paramref name="currentRetry"/> is 0, it returns the minimum backoff time. | ||
| /// - Otherwise, it calculates the delay as the minimum of: | ||
| /// - (2^(currentRetry - 1)) * deltaBackoff | ||
| /// - maxBackoff | ||
| /// This ensures that the delay increases exponentially with each retry attempt, | ||
| /// but does not exceed the maximum backoff time. | ||
| /// </remarks> | ||
| public int CalculateDelay(int currentRetry) | ||
| { | ||
| // Attempt 1 | ||
| if (currentRetry == 0) | ||
| { | ||
| return _minExponentialBackoff; | ||
| } | ||
|
|
||
| // Attempt 2+ | ||
| int exponentialDelay = Math.Min( | ||
| (int)(Math.Pow(2, currentRetry - 1) * _exponentialDeltaBackoff), | ||
| _maxExponentialBackoff | ||
| ); | ||
|
|
||
| return exponentialDelay; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/client/Microsoft.Identity.Client/Http/Retry/IRetryPolicy.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Identity.Client.Core; | ||
|
|
||
| namespace Microsoft.Identity.Client.Http.Retry | ||
| { | ||
| // Interface for implementing retry logic for HTTP requests. | ||
| // Determines if a retry should occur and handles pause logic between retries. | ||
| internal interface IRetryPolicy | ||
Robbie-Microsoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// Determines whether a retry should be attempted for a given HTTP response or exception, | ||
| /// and performs any necessary pause or delay logic before the next retry attempt. | ||
| /// </summary> | ||
| /// <param name="response">The HTTP response received from the request.</param> | ||
| /// <param name="exception">The exception encountered during the request.</param> | ||
| /// <param name="retryCount">The current retry attempt count.</param> | ||
| /// <param name="logger">The logger used for diagnostic and informational messages.</param> | ||
| /// <returns>A task that returns true if a retry should be performed; otherwise, false.</returns> | ||
| Task<bool> PauseForRetryAsync(HttpResponse response, Exception exception, int retryCount, ILoggerAdapter logger); | ||
Robbie-Microsoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
src/client/Microsoft.Identity.Client/Http/Retry/IRetryPolicyFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Identity.Client.Http.Retry | ||
| { | ||
| internal interface IRetryPolicyFactory | ||
| { | ||
| IRetryPolicy GetRetryPolicy(RequestType requestType); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.