Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use StrategyHelper for safe executions and drop redundant methods #1463

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Polly.Utils;

namespace Polly.CircuitBreaker;

internal sealed class CircuitBreakerResilienceStrategy<T> : ReactiveResilienceStrategy<T>
Expand Down Expand Up @@ -28,7 +30,7 @@ protected internal override async ValueTask<Outcome<T>> ExecuteCore<TState>(Func
return outcome;
}

outcome = await ExecuteCallbackSafeAsync(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext);
outcome = await StrategyHelper.ExecuteCallbackSafeAsync(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext);

var args = new OutcomeArguments<T, CircuitBreakerPredicateArguments>(context, outcome, default);
if (await _handler(args).ConfigureAwait(context.ContinueOnCapturedContext))
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Core/Fallback/FallbackResilienceStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public FallbackResilienceStrategy(FallbackHandler<T> handler, Func<OutcomeArgume

protected internal override async ValueTask<Outcome<T>> ExecuteCore<TState>(Func<ResilienceContext, TState, ValueTask<Outcome<T>>> callback, ResilienceContext context, TState state)
{
var outcome = await ExecuteCallbackSafeAsync(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext);
var outcome = await StrategyHelper.ExecuteCallbackSafeAsync(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext);
var handleFallbackArgs = new OutcomeArguments<T, FallbackPredicateArguments>(context, outcome, default);
if (!await _handler.ShouldHandle(handleFallbackArgs).ConfigureAwait(context.ContinueOnCapturedContext))
{
Expand Down
5 changes: 0 additions & 5 deletions src/Polly.Core/ReactiveResilienceStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,4 @@ protected internal abstract ValueTask<Outcome<TResult>> ExecuteCore<TState>(
Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback,
ResilienceContext context,
TState state);

internal static ValueTask<Outcome<T>> ExecuteCallbackSafeAsync<T, TState>(
Func<ResilienceContext, TState, ValueTask<Outcome<T>>> callback,
ResilienceContext context,
TState state) => StrategyHelper.ExecuteCallbackSafeAsync(callback, context, state);
}
5 changes: 0 additions & 5 deletions src/Polly.Core/ResilienceStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,4 @@ private Outcome<TResult> ExecuteCoreSync<TResult, TState>(
context,
(callback, state)).GetResult();
}

internal static ValueTask<Outcome<TResult>> ExecuteCallbackSafeAsync<TResult, TState>(
Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback,
ResilienceContext context,
TState state) => StrategyHelper.ExecuteCallbackSafeAsync(callback, context, state);
}
2 changes: 1 addition & 1 deletion src/Polly.Core/Retry/RetryResilienceStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected internal override async ValueTask<Outcome<T>> ExecuteCore<TState>(Func
while (true)
{
var startTimestamp = _timeProvider.GetTimestamp();
var outcome = await ExecuteCallbackSafeAsync(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext);
var outcome = await StrategyHelper.ExecuteCallbackSafeAsync(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext);
var shouldRetryArgs = new OutcomeArguments<T, RetryPredicateArguments>(context, outcome, new RetryPredicateArguments(attempt));
var handle = await ShouldHandle(shouldRetryArgs).ConfigureAwait(context.ContinueOnCapturedContext);
var executionTime = _timeProvider.GetElapsedTime(startTimestamp);
Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Core/Timeout/TimeoutResilienceStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected internal override async ValueTask<Outcome<TResult>> ExecuteCore<TResul

var registration = CreateRegistration(cancellationSource, previousToken);

var outcome = await ExecuteCallbackSafeAsync(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext);
var outcome = await StrategyHelper.ExecuteCallbackSafeAsync(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext);
var isCancellationRequested = cancellationSource.IsCancellationRequested;

// execution is finished, clean up
Expand Down
24 changes: 0 additions & 24 deletions test/Polly.Core.Tests/ResilienceStrategyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,6 @@ public partial class ResilienceStrategyTests
{
public static readonly CancellationToken CancellationToken = new CancellationTokenSource().Token;

[InlineData(true)]
[InlineData(false)]
[Theory]
public async Task ExecuteCallbackSafeAsync_CallbackThrows_EnsureExceptionWrapped(bool isAsync)
{
await TestUtilities.AssertWithTimeoutAsync(async () =>
{
var outcome = await ResilienceStrategy.ExecuteCallbackSafeAsync<string, string>(
async (_, _) =>
{
if (isAsync)
{
await Task.Delay(15);
}

throw new InvalidOperationException();
},
ResilienceContextPool.Shared.Get(),
"dummy");

outcome.Exception.Should().BeOfType<InvalidOperationException>();
});
}

[Fact]
public void DebuggerProxy_Ok()
{
Expand Down
30 changes: 30 additions & 0 deletions test/Polly.Core.Tests/Utils/StrategyHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Polly.Utils;

namespace Polly.Core.Tests.Utils;

public class StrategyHelperTests
{
[InlineData(true)]
[InlineData(false)]
[Theory]
public async Task ExecuteCallbackSafeAsync_CallbackThrows_EnsureExceptionWrapped(bool isAsync)
{
await TestUtilities.AssertWithTimeoutAsync(async () =>
{
var outcome = await StrategyHelper.ExecuteCallbackSafeAsync<string, string>(
async (_, _) =>
{
if (isAsync)
{
await Task.Delay(15);
}

throw new InvalidOperationException();
},
ResilienceContextPool.Shared.Get(),
"dummy");

outcome.Exception.Should().BeOfType<InvalidOperationException>();
});
}
}