-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
6 changed files
with
175 additions
and
19 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
25 changes: 25 additions & 0 deletions
25
WowsKarma.Api/Infrastructure/Resilience/PollyContextExtensions.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,25 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Polly; | ||
|
||
namespace WowsKarma.Api.Infrastructure.Resilience; | ||
|
||
public static class PollyContextExtensions | ||
{ | ||
/// <summary> | ||
/// Defines the item names for context items. | ||
/// </summary> | ||
/// <seealso cref="ResilienceContext"/> | ||
public static class ContextItems | ||
{ | ||
public static readonly ResiliencePropertyKey<ILogger> Logger = new("logger"); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the logger from the resilience context. | ||
/// </summary> | ||
/// <param name="context">The resilience context.</param> | ||
/// <param name="logger">The logger, if initially provided to the context and found.</param> | ||
/// <returns></returns> | ||
public static bool TryGetLogger(this ResilienceContext context, [NotNullWhen(true)] out ILogger? logger) | ||
=> context.Properties.TryGetValue(ContextItems.Logger, out logger); | ||
} |
91 changes: 91 additions & 0 deletions
91
WowsKarma.Api/Infrastructure/Resilience/ResiliencePipelineDependencyInjectionExtensions.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,91 @@ | ||
using System.Text.Json; | ||
using Polly; | ||
using Polly.CircuitBreaker; | ||
using Polly.Retry; | ||
using static WowsKarma.Api.Infrastructure.Resilience.PollyContextExtensions; | ||
|
||
namespace WowsKarma.Api.Infrastructure.Resilience; | ||
|
||
/// <summary> | ||
/// Keys/Names of the different resilience pipelines. | ||
/// </summary> | ||
public static class ResiliencePipelines | ||
{ | ||
public const string PlayerClansUpdatePolicyName = "player-clans-update"; | ||
} | ||
|
||
/// <summary> | ||
/// Extension methods for adding resilience policies to the dependency injection container, using Polly. | ||
/// </summary> | ||
public static class ResiliencePipelineDependencyInjectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds resilience policies to the dependency injection container, using Polly. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection"/> to add the policies to.</param> | ||
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> | ||
public static IServiceCollection AddResiliencePolicies(this IServiceCollection services) | ||
{ | ||
services.AddResiliencePipeline<string, bool>(ResiliencePipelines.PlayerClansUpdatePolicyName, builder => builder | ||
.AddFallback(new() | ||
{ | ||
ShouldHandle = static args => ValueTask.FromResult(args.Outcome.Exception is not null), | ||
FallbackAction = static args => | ||
{ | ||
if (args.Outcome.Exception is not BrokenCircuitException or IsolatedCircuitException) | ||
{ | ||
(args.Context.TryGetLogger(out ILogger? logger) ? logger : null)? | ||
.LogWarning(args.Outcome.Exception, "Fallback triggered for {Policy} policy", ResiliencePipelines.PlayerClansUpdatePolicyName); | ||
} | ||
return Outcome.FromResultAsValueTask(false); | ||
} | ||
}) | ||
.AddCircuitBreaker(new() | ||
{ | ||
ShouldHandle = static args => ValueTask.FromResult(args.Outcome switch | ||
{ | ||
{ Exception: JsonException } => true, | ||
_ => false | ||
}), | ||
OnClosed = static args => | ||
{ | ||
(args.Context.TryGetLogger(out ILogger? logger) ? logger : null)? | ||
.LogInformation("Circuit closed for {Policy} policy", ResiliencePipelines.PlayerClansUpdatePolicyName); | ||
return ValueTask.CompletedTask; | ||
}, | ||
OnHalfOpened = static args => | ||
{ | ||
(args.Context.TryGetLogger(out ILogger? logger) ? logger : null)? | ||
.LogInformation("Circuit half-opened for {Policy} policy", ResiliencePipelines.PlayerClansUpdatePolicyName); | ||
return ValueTask.CompletedTask; | ||
}, | ||
OnOpened = static args => | ||
{ | ||
(args.Context.TryGetLogger(out ILogger? logger) ? logger : null)? | ||
.LogWarning("Circuit opened for {Policy} policy", ResiliencePipelines.PlayerClansUpdatePolicyName); | ||
return ValueTask.CompletedTask; | ||
} | ||
}) | ||
.AddRetry(new() | ||
{ | ||
BackoffType = DelayBackoffType.Exponential, | ||
MaxRetryAttempts = 4, | ||
ShouldHandle = static args => ValueTask.FromResult(args.Outcome switch | ||
{ | ||
{ Exception: HttpRequestException } => true, | ||
_ => false | ||
}) | ||
}) | ||
); | ||
|
||
return services; | ||
} | ||
} |
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