-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3b88e9
commit ad52da7
Showing
13 changed files
with
213 additions
and
24 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
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
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
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
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
35 changes: 35 additions & 0 deletions
35
src/Pandatech.CleanArchitecture.Infrastructure/Extensions/ResilienceExtensions.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,35 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Http.Resilience; | ||
using Pandatech.CleanArchitecture.Infrastructure.Helpers; | ||
using Polly; | ||
|
||
namespace Pandatech.CleanArchitecture.Infrastructure.Extensions; | ||
|
||
public static class ResilienceExtensions | ||
{ | ||
public static WebApplicationBuilder AddResilienceDefaultPipeline(this WebApplicationBuilder builder) | ||
{ | ||
builder.Services.AddResiliencePipeline(ResilienceDefaultPipelineProvider.DefaultPipelineName, | ||
pipelineBuilder => | ||
{ | ||
pipelineBuilder.AddRetry(ResilienceDefaultPipelineProvider.DefaultNetworkRetryOptions) | ||
.AddRetry(ResilienceDefaultPipelineProvider.TooManyRequestsRetryOptions) | ||
.AddCircuitBreaker(ResilienceDefaultPipelineProvider.DefaultCircuitBreakerOptions) | ||
.AddTimeout(TimeSpan.FromSeconds(8)); | ||
}); | ||
return builder; | ||
} | ||
|
||
public static IHttpResiliencePipelineBuilder AddResilienceDefaultPipeline(this IHttpClientBuilder builder) | ||
{ | ||
return builder.AddResilienceHandler("DefaultPipeline", | ||
resilienceBuilder => | ||
{ | ||
resilienceBuilder.AddRetry(ResilienceHttpOptions.DefaultTooManyRequestsRetryOptions) | ||
.AddRetry(ResilienceHttpOptions.DefaultNetworkRetryOptions) | ||
.AddCircuitBreaker(ResilienceHttpOptions.DefaultCircuitBreakerOptions) | ||
.AddTimeout(TimeSpan.FromSeconds(8)); | ||
}); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Pandatech.CleanArchitecture.Infrastructure/Helpers/ResilienceDefaultPipelineProvider.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,51 @@ | ||
using System.Net; | ||
using Polly; | ||
using Polly.CircuitBreaker; | ||
using Polly.Registry; | ||
using Polly.Retry; | ||
|
||
namespace Pandatech.CleanArchitecture.Infrastructure.Helpers; | ||
|
||
public static class ResilienceDefaultPipelineProvider | ||
{ | ||
public static ResiliencePipeline GetDefaultPipeline( | ||
this ResiliencePipelineProvider<string> resiliencePipelineProvider) | ||
{ | ||
return resiliencePipelineProvider.GetPipeline(DefaultPipelineName); | ||
} | ||
|
||
internal const string DefaultPipelineName = "DefaultPipeline"; | ||
|
||
internal static RetryStrategyOptions TooManyRequestsRetryOptions => | ||
new() | ||
{ | ||
MaxRetryAttempts = 5, | ||
BackoffType = DelayBackoffType.Exponential, | ||
UseJitter = true, | ||
Delay = TimeSpan.FromMilliseconds(3000), | ||
ShouldHandle = new PredicateBuilder() | ||
.Handle<HttpRequestException>(exception => exception.StatusCode == HttpStatusCode.TooManyRequests) | ||
}; | ||
|
||
internal static RetryStrategyOptions DefaultNetworkRetryOptions => | ||
new() | ||
{ | ||
MaxRetryAttempts = 7, | ||
BackoffType = DelayBackoffType.Exponential, | ||
UseJitter = true, | ||
Delay = TimeSpan.FromMilliseconds(800), | ||
ShouldHandle = new PredicateBuilder() | ||
.Handle<HttpRequestException>(exception => exception.StatusCode == HttpStatusCode.RequestTimeout || | ||
(int)exception.StatusCode! >= 500) | ||
}; | ||
|
||
internal static CircuitBreakerStrategyOptions DefaultCircuitBreakerOptions => | ||
new() | ||
{ | ||
FailureRatio = 0.5, | ||
SamplingDuration = TimeSpan.FromSeconds(30), | ||
MinimumThroughput = 200, | ||
BreakDuration = TimeSpan.FromSeconds(45), | ||
ShouldHandle = new PredicateBuilder().Handle<Exception>() | ||
}; | ||
} |
103 changes: 103 additions & 0 deletions
103
src/Pandatech.CleanArchitecture.Infrastructure/Helpers/ResilienceHttpOptions.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,103 @@ | ||
using System.Net; | ||
using Microsoft.Extensions.Http.Resilience; | ||
using Polly; | ||
|
||
namespace Pandatech.CleanArchitecture.Infrastructure.Helpers; | ||
|
||
internal static class ResilienceHttpOptions | ||
{ | ||
public static HttpRetryStrategyOptions DefaultTooManyRequestsRetryOptions => | ||
new() | ||
{ | ||
MaxRetryAttempts = 5, | ||
BackoffType = DelayBackoffType.Exponential, | ||
UseJitter = true, | ||
Delay = TimeSpan.FromMilliseconds(3000), | ||
ShouldHandle = args => | ||
{ | ||
if (args.Outcome.Exception is HttpRequestException httpException) | ||
{ | ||
return ValueTask.FromResult((int)httpException.StatusCode! == 429); | ||
} | ||
if (args.Outcome.Result is not null && args.Outcome.Result.StatusCode == HttpStatusCode.TooManyRequests) | ||
{ | ||
return ValueTask.FromResult(true); | ||
} | ||
return ValueTask.FromResult(false); | ||
}, | ||
DelayGenerator = args => | ||
{ | ||
if (args.Outcome.Result is null) | ||
{ | ||
return ValueTask.FromResult<TimeSpan?>(null); | ||
} | ||
if (!args.Outcome.Result.Headers.TryGetValues("Retry-After", out var values)) | ||
{ | ||
return ValueTask.FromResult<TimeSpan?>(null); | ||
} | ||
var retryAfterValue = values.FirstOrDefault(); | ||
if (int.TryParse(retryAfterValue, out var retryAfterSeconds)) | ||
{ | ||
return ValueTask.FromResult<TimeSpan?>(TimeSpan.FromSeconds(retryAfterSeconds)); | ||
} | ||
if (!DateTimeOffset.TryParseExact(retryAfterValue, | ||
"R", // RFC1123 pattern | ||
System.Globalization.CultureInfo.InvariantCulture, | ||
System.Globalization.DateTimeStyles.None, | ||
out var retryAfterDate)) | ||
{ | ||
return ValueTask.FromResult<TimeSpan?>(null); | ||
} | ||
var retryDelay = retryAfterDate - DateTimeOffset.UtcNow; | ||
return ValueTask.FromResult<TimeSpan?>(retryDelay > TimeSpan.Zero ? retryDelay : TimeSpan.MinValue); | ||
} | ||
}; | ||
|
||
public static HttpRetryStrategyOptions DefaultNetworkRetryOptions => | ||
new() | ||
{ | ||
MaxRetryAttempts = 7, | ||
BackoffType = DelayBackoffType.Exponential, | ||
UseJitter = true, | ||
Delay = TimeSpan.FromMilliseconds(800), | ||
ShouldHandle = args => | ||
{ | ||
if (args.Outcome.Exception is HttpRequestException httpException) | ||
{ | ||
return ValueTask.FromResult((int)httpException.StatusCode! >= 500 || | ||
(int)httpException.StatusCode! == 408); | ||
} | ||
return ValueTask.FromResult(args.Outcome.Result is not null && | ||
(args.Outcome.Result.StatusCode == HttpStatusCode.RequestTimeout || | ||
(int)args.Outcome.Result.StatusCode >= 500)); | ||
} | ||
}; | ||
|
||
public static HttpCircuitBreakerStrategyOptions DefaultCircuitBreakerOptions => | ||
new() | ||
{ | ||
FailureRatio = 0.5, | ||
SamplingDuration = TimeSpan.FromSeconds(30), | ||
MinimumThroughput = 200, | ||
BreakDuration = TimeSpan.FromSeconds(45), | ||
ShouldHandle = args => | ||
{ | ||
if (args.Outcome.Exception is not null) | ||
{ | ||
return ValueTask.FromResult(true); | ||
} | ||
return args.Outcome.Result is null | ||
? ValueTask.FromResult(false) | ||
: ValueTask.FromResult(!args.Outcome.Result.IsSuccessStatusCode); | ||
} | ||
}; | ||
} |
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