-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dh: Teach Retry handler to honor retry-after header
- Loading branch information
1 parent
7e1d31c
commit d817098
Showing
7 changed files
with
659 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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,41 @@ | ||
using System; | ||
using Polly.Retry; | ||
|
||
namespace Polly; | ||
|
||
public static class AsyncRetryTResultSyntax | ||
{ | ||
/// <summary> | ||
/// Builds an <see cref="AsyncRetryPolicy{TResult}" /> that will wait and retry <paramref name="retryCount" /> times | ||
/// calling <paramref name="onRetry" /> on each retry with the handled exception or result, the current sleep duration, retry count, and context data. | ||
/// On each retry, the duration to wait is calculated by calling <paramref name="sleepDurationProvider" /> with | ||
/// the current retry number (1 for first retry, 2 for second etc), result of previous execution, and execution context. | ||
/// </summary> | ||
/// <param name="policyBuilder">The policy builder.</param> | ||
/// <param name="retryCount">The retry count.</param> | ||
/// <param name="sleepDurationProvider">The function that provides the duration to wait for for a particular retry attempt.</param> | ||
/// <param name="onRetry">The action to call on each retry.</param> | ||
/// <returns>The policy instance.</returns> | ||
/// <exception cref="ArgumentOutOfRangeException">retryCount;Value must be greater than or equal to zero.</exception> | ||
/// <exception cref="ArgumentNullException"> | ||
/// sleepDurationProvider | ||
/// or | ||
/// onRetryAsync | ||
/// </exception> | ||
/// <note> | ||
/// issue: https://github.com/App-vNext/Polly/issues/908 | ||
/// </note> | ||
public static AsyncRetryPolicy<TResult> WaitAndRetryAsync<TResult>(this PolicyBuilder<TResult> policyBuilder, int retryCount, | ||
Func<int, DelegateResult<TResult>, Context, TimeSpan> sleepDurationProvider, Action<DelegateResult<TResult>, TimeSpan, int, Context> onRetry) | ||
{ | ||
if (onRetry == null) throw new ArgumentNullException(nameof(onRetry)); | ||
|
||
return policyBuilder.WaitAndRetryAsync( | ||
retryCount, | ||
sleepDurationProvider, | ||
#pragma warning disable 1998 // async method has no awaits, will run synchronously | ||
onRetryAsync: async (outcome, timespan, i, ctx) => onRetry(outcome, timespan, i, ctx) | ||
#pragma warning restore 1998 | ||
); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/rm.DelegatingHandlers/misc/HttpStatusCodeIntExtensions.cs
This file contains 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 @@ | ||
using System.Net; | ||
|
||
namespace rm.DelegatingHandlers; | ||
|
||
public static class HttpStatusCodeIntExtensions | ||
{ | ||
public static bool Is1xx(this int statusCode) | ||
{ | ||
return ((HttpStatusCode)statusCode).Is1xx(); | ||
} | ||
|
||
public static bool Is2xx(this int statusCode) | ||
{ | ||
return ((HttpStatusCode)statusCode).Is2xx(); | ||
} | ||
|
||
public static bool Is3xx(this int statusCode) | ||
{ | ||
return ((HttpStatusCode)statusCode).Is3xx(); | ||
} | ||
|
||
public static bool Is4xx(this int statusCode) | ||
{ | ||
return ((HttpStatusCode)statusCode).Is4xx(); | ||
} | ||
|
||
public static bool Is5xx(this int statusCode) | ||
{ | ||
return ((HttpStatusCode)statusCode).Is5xx(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns true if status code is a client error status code (4xx). | ||
/// </summary> | ||
public static bool IsClientErrorStatusCode(this int statusCode) | ||
{ | ||
return statusCode.Is4xx(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns true if status code is a server error status code (5xx). | ||
/// </summary> | ||
public static bool IsServerErrorStatusCode(this int statusCode) | ||
{ | ||
return statusCode.Is5xx(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns true if status code is an error status code (4xx, 5xx). | ||
/// </summary> | ||
public static bool IsErrorStatusCode(this int statusCode) | ||
{ | ||
return statusCode.Is4xx() || statusCode.Is5xx(); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.