-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
25 changed files
with
647 additions
and
678 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,60 @@ | ||
using Polly.Telemetry; | ||
|
||
namespace Polly.Simmy.Fault; | ||
|
||
internal class FaultChaosStrategy : MonkeyStrategy | ||
{ | ||
private readonly ResilienceStrategyTelemetry _telemetry; | ||
|
||
public FaultChaosStrategy(FaultStrategyOptions options, ResilienceStrategyTelemetry telemetry) | ||
: base(options) | ||
{ | ||
if (options.Fault is null && options.FaultGenerator is null) | ||
{ | ||
throw new InvalidOperationException("Either Fault or FaultGenerator is required."); | ||
} | ||
|
||
_telemetry = telemetry; | ||
Fault = options.Fault; | ||
OnFaultInjected = options.OnFaultInjected; | ||
FaultGenerator = options.FaultGenerator is not null ? options.FaultGenerator : (_) => new(options.Fault); | ||
} | ||
|
||
public Func<OnFaultInjectedArguments, ValueTask>? OnFaultInjected { get; } | ||
|
||
public Func<FaultGeneratorArguments, ValueTask<Exception?>> FaultGenerator { get; } | ||
|
||
public Exception? Fault { get; } | ||
|
||
protected internal override async ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState>( | ||
Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback, | ||
ResilienceContext context, | ||
TState state) | ||
{ | ||
try | ||
{ | ||
if (await ShouldInjectAsync(context).ConfigureAwait(context.ContinueOnCapturedContext)) | ||
{ | ||
var fault = await FaultGenerator(new(context)).ConfigureAwait(context.ContinueOnCapturedContext); | ||
if (fault is not null) | ||
{ | ||
var args = new OnFaultInjectedArguments(context, fault); | ||
_telemetry.Report(new(ResilienceEventSeverity.Information, FaultConstants.OnFaultInjectedEvent), context, args); | ||
|
||
if (OnFaultInjected is not null) | ||
{ | ||
await OnFaultInjected(args).ConfigureAwait(context.ContinueOnCapturedContext); | ||
} | ||
|
||
return new Outcome<TResult>(fault); | ||
} | ||
} | ||
|
||
return await StrategyHelper.ExecuteCallbackSafeAsync(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext); | ||
} | ||
catch (OperationCanceledException e) | ||
{ | ||
return new Outcome<TResult>(e); | ||
} | ||
} | ||
} |
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,6 @@ | ||
namespace Polly.Simmy.Fault; | ||
|
||
internal static class FaultConstants | ||
{ | ||
public const string OnFaultInjectedEvent = "OnFaultInjectedEvent"; | ||
} |
2 changes: 1 addition & 1 deletion
2
...Simmy/Outcomes/FaultGeneratorArguments.cs → ...re/Simmy/Fault/FaultGeneratorArguments.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
72 changes: 72 additions & 0 deletions
72
src/Polly.Core/Simmy/Fault/FaultPipelineBuilderExtensions.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,72 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Polly.Simmy.Fault; | ||
|
||
namespace Polly.Simmy; | ||
|
||
/// <summary> | ||
/// Extension methods for adding outcome to a <see cref="ResiliencePipelineBuilder"/>. | ||
/// </summary> | ||
internal static class FaultPipelineBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a fault chaos strategy to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder instance.</param> | ||
/// <param name="injectionRate">The injection rate for a given execution, which the value should be between [0, 1] (inclusive).</param> | ||
/// <param name="fault">The exception to inject.</param> | ||
/// <returns>The builder instance with the retry strategy added.</returns> | ||
public static TBuilder AddChaosFault<TBuilder>(this TBuilder builder, double injectionRate, Exception fault) | ||
where TBuilder : ResiliencePipelineBuilderBase | ||
{ | ||
builder.AddChaosFault(new FaultStrategyOptions | ||
{ | ||
Enabled = true, | ||
InjectionRate = injectionRate, | ||
Fault = fault | ||
}); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Adds a fault chaos strategy to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder instance.</param> | ||
/// <param name="injectionRate">The injection rate for a given execution, which the value should be between [0, 1] (inclusive).</param> | ||
/// <param name="faultGenerator">The exception generator delegate.</param> | ||
/// <returns>The builder instance with the retry strategy added.</returns> | ||
public static TBuilder AddChaosFault<TBuilder>(this TBuilder builder, double injectionRate, Func<Exception?> faultGenerator) | ||
where TBuilder : ResiliencePipelineBuilderBase | ||
{ | ||
builder.AddChaosFault(new FaultStrategyOptions | ||
{ | ||
Enabled = true, | ||
InjectionRate = injectionRate, | ||
FaultGenerator = (_) => new ValueTask<Exception?>(Task.FromResult(faultGenerator())) | ||
}); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Adds a fault chaos strategy to the builder. | ||
/// </summary> | ||
/// <typeparam name="TBuilder">The builder type.</typeparam> | ||
/// <param name="builder">The builder instance.</param> | ||
/// <param name="options">The fault strategy options.</param> | ||
/// <returns>The builder instance with the retry strategy added.</returns> | ||
[UnconditionalSuppressMessage( | ||
"Trimming", | ||
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", | ||
Justification = "All options members preserved.")] | ||
public static TBuilder AddChaosFault<TBuilder>(this TBuilder builder, FaultStrategyOptions options) | ||
where TBuilder : ResiliencePipelineBuilderBase | ||
{ | ||
Guard.NotNull(builder); | ||
Guard.NotNull(options); | ||
|
||
builder.AddStrategy( | ||
context => new FaultChaosStrategy(options, context.Telemetry), | ||
options); | ||
|
||
return builder; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...re/Simmy/Outcomes/FaultStrategyOptions.cs → ....Core/Simmy/Fault/FaultStrategyOptions.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
2 changes: 1 addition & 1 deletion
2
...immy/Outcomes/OnFaultInjectedArguments.cs → ...e/Simmy/Fault/OnFaultInjectedArguments.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
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
Oops, something went wrong.