-
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
d23c6da
commit 484ecf3
Showing
12 changed files
with
213 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Pandatech.VerticalSlices; | ||
|
||
public record AssemblyReference(); |
13 changes: 7 additions & 6 deletions
13
src/Pandatech.VerticalSlices/Pandatech.VerticalSlices.csproj
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
33 changes: 33 additions & 0 deletions
33
src/Pandatech.VerticalSlices/SharedKernel/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,33 @@ | ||
using Microsoft.Extensions.Http.Resilience; | ||
using Pandatech.VerticalSlices.SharedKernel.Helpers; | ||
using Polly; | ||
|
||
namespace Pandatech.VerticalSlices.SharedKernel.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)); | ||
}); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
src/Pandatech.VerticalSlices/SharedKernel/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.VerticalSlices.SharedKernel.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.VerticalSlices/SharedKernel/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.VerticalSlices.SharedKernel.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
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