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

Finalize the API review #1528

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion bench/Polly.Core.Benchmarks/CreationBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void Fallback_V8()
new ResiliencePipelineBuilder<string>()
.AddFallback(new()
{
FallbackAction = _ => Outcome.FromResultAsTask("fallback")
FallbackAction = _ => Outcome.FromResultAsValueTask("fallback")
})
.Build();
}
Expand Down
10 changes: 5 additions & 5 deletions bench/Polly.Core.Benchmarks/PredicateBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class PredicateBenchmark
{
ShouldHandle = args => args.Outcome switch
{
{ Result: { StatusCode: HttpStatusCode.InternalServerError } } => PredicateResult.True,
{ Exception: HttpRequestException } => PredicateResult.True,
{ Exception: IOException } => PredicateResult.True,
{ Exception: InvalidOperationException } => PredicateResult.False,
_ => PredicateResult.False,
{ Result: { StatusCode: HttpStatusCode.InternalServerError } } => PredicateResult.True(),
{ Exception: HttpRequestException } => PredicateResult.True(),
{ Exception: IOException } => PredicateResult.True(),
{ Exception: InvalidOperationException } => PredicateResult.False(),
_ => PredicateResult.False(),
}
};

Expand Down
2 changes: 1 addition & 1 deletion bench/Polly.Core.Benchmarks/ResiliencePipelineBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ResiliencePipelineBenchmark
public async ValueTask ExecuteOutcomeAsync()
{
var context = ResilienceContextPool.Shared.Get();
await ResiliencePipeline.Empty.ExecuteOutcomeAsync((_, _) => Outcome.FromResultAsTask("dummy"), context, "state").ConfigureAwait(false);
await ResiliencePipeline.Empty.ExecuteOutcomeAsync((_, _) => Outcome.FromResultAsValueTask("dummy"), context, "state").ConfigureAwait(false);
ResilienceContextPool.Shared.Return(context);
}

Expand Down
2 changes: 1 addition & 1 deletion bench/Polly.Core.Benchmarks/TelemetryBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Prepare()
public async ValueTask Execute()
{
var context = ResilienceContextPool.Shared.Get();
await _pipeline!.ExecuteOutcomeAsync((_, _) => Outcome.FromResultAsTask("dummy"), context, "state").ConfigureAwait(false);
await _pipeline!.ExecuteOutcomeAsync((_, _) => Outcome.FromResultAsValueTask("dummy"), context, "state").ConfigureAwait(false);
ResilienceContextPool.Shared.Return(context);
}

Expand Down
8 changes: 4 additions & 4 deletions bench/Polly.Core.Benchmarks/Utils/Helper.CircuitBreaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static object CreateOpenedCircuitBreaker(PollyVersion version, bool handl
var manualControl = new CircuitBreakerManualControl();
var options = new CircuitBreakerStrategyOptions
{
ShouldHandle = _ => PredicateResult.True,
ShouldHandle = _ => PredicateResult.True(),
ManualControl = manualControl,
};

Expand Down Expand Up @@ -54,9 +54,9 @@ public static object CreateCircuitBreaker(PollyVersion technology)
BreakDuration = TimeSpan.FromSeconds(5),
ShouldHandle = args => args.Outcome switch
{
{ Exception: InvalidOperationException } => PredicateResult.True,
{ Result: string result } when result == Failure => PredicateResult.True,
_ => PredicateResult.False
{ Exception: InvalidOperationException } => PredicateResult.True(),
{ Result: string result } when result == Failure => PredicateResult.True(),
_ => PredicateResult.False()
}
});
}),
Expand Down
2 changes: 1 addition & 1 deletion bench/Polly.Core.Benchmarks/Utils/Helper.Hedging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static ResiliencePipeline<string> CreateHedging()
builder.AddHedging(new HedgingStrategyOptions<string>
{
ShouldHandle = args => new ValueTask<bool>(args.Outcome.Result == Failure),
ActionGenerator = args => () => Outcome.FromResultAsTask("hedged response"),
ActionGenerator = args => () => Outcome.FromResultAsValueTask("hedged response"),
});
});
}
Expand Down
24 changes: 12 additions & 12 deletions bench/Polly.Core.Benchmarks/Utils/Helper.MultipleStrategies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ internal static partial class Helper
Delay = TimeSpan.FromSeconds(1),
ShouldHandle = args => args.Outcome switch
{
{ Exception: InvalidOperationException } => PredicateResult.True,
{ Result: string result } when result == Failure => PredicateResult.True,
_ => PredicateResult.False
{ Exception: InvalidOperationException } => PredicateResult.True(),
{ Result: string result } when result == Failure => PredicateResult.True(),
_ => PredicateResult.False()
}
})
.AddTimeout(TimeSpan.FromSeconds(1))
Expand All @@ -43,9 +43,9 @@ internal static partial class Helper
BreakDuration = TimeSpan.FromSeconds(5),
ShouldHandle = args => args.Outcome switch
{
{ Exception: InvalidOperationException } => PredicateResult.True,
{ Result: string result } when result == Failure => PredicateResult.True,
_ => PredicateResult.False
{ Exception: InvalidOperationException } => PredicateResult.True(),
{ Result: string result } when result == Failure => PredicateResult.True(),
_ => PredicateResult.False()
}
});

Expand Down Expand Up @@ -73,9 +73,9 @@ public static ResiliencePipeline CreateNonGenericStrategyPipeline(bool telemetry
Delay = TimeSpan.FromSeconds(1),
ShouldHandle = args => args.Outcome switch
{
{ Exception: InvalidOperationException } => PredicateResult.True,
{ Result: string result } when result == Failure => PredicateResult.True,
_ => PredicateResult.False
{ Exception: InvalidOperationException } => PredicateResult.True(),
{ Result: string result } when result == Failure => PredicateResult.True(),
_ => PredicateResult.False()
}
})
.AddTimeout(TimeSpan.FromSeconds(1))
Expand All @@ -87,9 +87,9 @@ public static ResiliencePipeline CreateNonGenericStrategyPipeline(bool telemetry
BreakDuration = TimeSpan.FromSeconds(5),
ShouldHandle = args => args.Outcome switch
{
{ Exception: InvalidOperationException } => PredicateResult.True,
{ Result: string result } when result == Failure => PredicateResult.True,
_ => PredicateResult.False
{ Exception: InvalidOperationException } => PredicateResult.True(),
{ Result: string result } when result == Failure => PredicateResult.True(),
_ => PredicateResult.False()
}
});

Expand Down
6 changes: 3 additions & 3 deletions bench/Polly.Core.Benchmarks/Utils/Helper.Retry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static object CreateRetry(PollyVersion technology)
Delay = delay,
ShouldHandle = args => args.Outcome switch
{
{ Exception: InvalidOperationException } => PredicateResult.True,
{ Result: string result } when result == Failure => PredicateResult.True,
_ => PredicateResult.False
{ Exception: InvalidOperationException } => PredicateResult.True(),
{ Result: string result } when result == Failure => PredicateResult.True(),
_ => PredicateResult.False()
},
OnRetry = _ => default
});
Expand Down
2 changes: 1 addition & 1 deletion bench/Polly.Core.Benchmarks/Utils/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static async ValueTask ExecuteAsync(this object obj, PollyVersion version
var context = ResilienceContextPool.Shared.Get();

await ((ResiliencePipeline<string>)obj).ExecuteOutcomeAsync(
static (_, _) => Outcome.FromResultAsTask("dummy"),
static (_, _) => Outcome.FromResultAsValueTask("dummy"),
context,
string.Empty).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ private void SetLastHandledOutcome_NeedsLock(Outcome<T> outcome)
{
_breakingException = new BrokenCircuitException(BrokenCircuitException.DefaultMessage, exception);
}
else if (outcome.TryGetResult(out var result))
else
{
_breakingException = new BrokenCircuitException<T>(BrokenCircuitException.DefaultMessage, result!);
_breakingException = new BrokenCircuitException(BrokenCircuitException.DefaultMessage);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Polly.Core/Outcome.TResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal Outcome(ExceptionDispatchInfo exceptionDispatchInfo)
/// <remarks>
/// If the operation produced a result, this method does nothing. The thrown exception maintains its original stack trace.
/// </remarks>
public void EnsureSuccess() => ExceptionDispatchInfo?.Throw();
public void ThrowIfException() => ExceptionDispatchInfo?.Throw();

/// <summary>
/// Tries to get the result, if available.
Expand Down
4 changes: 2 additions & 2 deletions src/Polly.Core/Outcome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class Outcome
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="value">The result value.</param>
/// <returns>A completed <see cref="ValueTask{TResult}"/> that produces <see cref="Outcome{TResult}"/>.</returns>
public static ValueTask<Outcome<TResult>> FromResultAsTask<TResult>(TResult value) => new(FromResult(value));
public static ValueTask<Outcome<TResult>> FromResultAsValueTask<TResult>(TResult value) => new(FromResult(value));

/// <summary>
/// Returns a <see cref="Outcome{TResult}"/> with the given <paramref name="exception"/>.
Expand All @@ -42,7 +42,7 @@ public static Outcome<TResult> FromException<TResult>(Exception exception)
/// <param name="exception">The exception.</param>
/// <returns>A completed <see cref="ValueTask{TResult}"/> that produces <see cref="Outcome{TResult}"/>.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="exception"/> is <see langword="null"/>.</exception>
public static ValueTask<Outcome<TResult>> FromExceptionAsTask<TResult>(Exception exception)
public static ValueTask<Outcome<TResult>> FromExceptionAsValueTask<TResult>(Exception exception)
{
Guard.NotNull(exception);

Expand Down
10 changes: 6 additions & 4 deletions src/Polly.Core/PredicateResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ namespace Polly;
public static class PredicateResult
{
/// <summary>
/// Gets a finished <see cref="ValueTask{TResult}"/> that returns <see langword="true"/> value.
/// Returns a finished <see cref="ValueTask{TResult}"/> that returns <see langword="true"/> value.
/// </summary>
public static ValueTask<bool> True => new(true);
/// <returns>A new instance of finished <see cref="ValueTask{TResult}"/>.</returns>
public static ValueTask<bool> True() => new(true);

/// <summary>
/// Gets a finished <see cref="ValueTask{TResult}"/> that returns <see langword="false"/> value.
/// Returns a finished <see cref="ValueTask{TResult}"/> that returns <see langword="false"/> value.
/// </summary>
public static ValueTask<bool> False => new(false);
/// <returns>A new instance of finished <see cref="ValueTask{TResult}"/>.</returns>
public static ValueTask<bool> False() => new(false);
}
20 changes: 7 additions & 13 deletions src/Polly.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ Polly.CircuitBreaker.BrokenCircuitException
Polly.CircuitBreaker.BrokenCircuitException.BrokenCircuitException() -> void
Polly.CircuitBreaker.BrokenCircuitException.BrokenCircuitException(string! message) -> void
Polly.CircuitBreaker.BrokenCircuitException.BrokenCircuitException(string! message, System.Exception! inner) -> void
Polly.CircuitBreaker.BrokenCircuitException<TResult>
Polly.CircuitBreaker.BrokenCircuitException<TResult>.BrokenCircuitException(string! message, System.Exception! inner, TResult result) -> void
Polly.CircuitBreaker.BrokenCircuitException<TResult>.BrokenCircuitException(string! message, TResult result) -> void
Polly.CircuitBreaker.BrokenCircuitException<TResult>.BrokenCircuitException(TResult result) -> void
Polly.CircuitBreaker.BrokenCircuitException<TResult>.Result.get -> TResult
Polly.CircuitBreaker.CircuitBreakerManualControl
Polly.CircuitBreaker.CircuitBreakerManualControl.CircuitBreakerManualControl() -> void
Polly.CircuitBreaker.CircuitBreakerManualControl.CircuitBreakerManualControl(bool isIsolated) -> void
Expand Down Expand Up @@ -158,10 +153,10 @@ Polly.HedgingResiliencePipelineBuilderExtensions
Polly.LegacySupport
Polly.Outcome
Polly.Outcome<TResult>
Polly.Outcome<TResult>.EnsureSuccess() -> void
Polly.Outcome<TResult>.Exception.get -> System.Exception?
Polly.Outcome<TResult>.Outcome() -> void
Polly.Outcome<TResult>.Result.get -> TResult?
Polly.Outcome<TResult>.ThrowIfException() -> void
Polly.PredicateBuilder
Polly.PredicateBuilder.PredicateBuilder() -> void
Polly.PredicateBuilder<TResult>
Expand Down Expand Up @@ -296,10 +291,9 @@ Polly.Retry.OnRetryArguments<TResult>.RetryDelay.get -> System.TimeSpan
Polly.Retry.RetryDelayGeneratorArguments<TResult>
Polly.Retry.RetryDelayGeneratorArguments<TResult>.AttemptNumber.get -> int
Polly.Retry.RetryDelayGeneratorArguments<TResult>.Context.get -> Polly.ResilienceContext!
Polly.Retry.RetryDelayGeneratorArguments<TResult>.DelayHint.get -> System.TimeSpan
Polly.Retry.RetryDelayGeneratorArguments<TResult>.Outcome.get -> Polly.Outcome<TResult>
Polly.Retry.RetryDelayGeneratorArguments<TResult>.RetryDelayGeneratorArguments() -> void
Polly.Retry.RetryDelayGeneratorArguments<TResult>.RetryDelayGeneratorArguments(Polly.ResilienceContext! context, Polly.Outcome<TResult> outcome, int attemptNumber, System.TimeSpan delayHint) -> void
Polly.Retry.RetryDelayGeneratorArguments<TResult>.RetryDelayGeneratorArguments(Polly.ResilienceContext! context, Polly.Outcome<TResult> outcome, int attemptNumber) -> void
Polly.Retry.RetryPredicateArguments<TResult>
Polly.Retry.RetryPredicateArguments<TResult>.AttemptNumber.get -> int
Polly.Retry.RetryPredicateArguments<TResult>.Context.get -> Polly.ResilienceContext!
Expand All @@ -313,7 +307,7 @@ Polly.Retry.RetryStrategyOptions<TResult>.BackoffType.get -> Polly.DelayBackoffT
Polly.Retry.RetryStrategyOptions<TResult>.BackoffType.set -> void
Polly.Retry.RetryStrategyOptions<TResult>.Delay.get -> System.TimeSpan
Polly.Retry.RetryStrategyOptions<TResult>.Delay.set -> void
Polly.Retry.RetryStrategyOptions<TResult>.DelayGenerator.get -> System.Func<Polly.Retry.RetryDelayGeneratorArguments<TResult>, System.Threading.Tasks.ValueTask<System.TimeSpan>>?
Polly.Retry.RetryStrategyOptions<TResult>.DelayGenerator.get -> System.Func<Polly.Retry.RetryDelayGeneratorArguments<TResult>, System.Threading.Tasks.ValueTask<System.TimeSpan?>>?
Polly.Retry.RetryStrategyOptions<TResult>.DelayGenerator.set -> void
Polly.Retry.RetryStrategyOptions<TResult>.MaxRetryAttempts.get -> int
Polly.Retry.RetryStrategyOptions<TResult>.MaxRetryAttempts.set -> void
Expand Down Expand Up @@ -403,15 +397,15 @@ static Polly.FallbackResiliencePipelineBuilderExtensions.AddFallback<TResult>(th
static Polly.HedgingResiliencePipelineBuilderExtensions.AddHedging<TResult>(this Polly.ResiliencePipelineBuilder<TResult>! builder, Polly.Hedging.HedgingStrategyOptions<TResult>! options) -> Polly.ResiliencePipelineBuilder<TResult>!
static Polly.LegacySupport.SetProperties(this Polly.ResilienceProperties! resilienceProperties, System.Collections.Generic.IDictionary<string!, object?>! properties, out System.Collections.Generic.IDictionary<string!, object?>! oldProperties) -> void
static Polly.Outcome.FromException<TResult>(System.Exception! exception) -> Polly.Outcome<TResult>
static Polly.Outcome.FromExceptionAsTask<TResult>(System.Exception! exception) -> System.Threading.Tasks.ValueTask<Polly.Outcome<TResult>>
static Polly.Outcome.FromExceptionAsValueTask<TResult>(System.Exception! exception) -> System.Threading.Tasks.ValueTask<Polly.Outcome<TResult>>
static Polly.Outcome.FromResult<TResult>(TResult? value) -> Polly.Outcome<TResult>
static Polly.Outcome.FromResultAsTask<TResult>(TResult value) -> System.Threading.Tasks.ValueTask<Polly.Outcome<TResult>>
static Polly.Outcome.FromResultAsValueTask<TResult>(TResult value) -> System.Threading.Tasks.ValueTask<Polly.Outcome<TResult>>
static Polly.PredicateBuilder<TResult>.implicit operator System.Func<Polly.CircuitBreaker.CircuitBreakerPredicateArguments<TResult>, System.Threading.Tasks.ValueTask<bool>>!(Polly.PredicateBuilder<TResult>! builder) -> System.Func<Polly.CircuitBreaker.CircuitBreakerPredicateArguments<TResult>, System.Threading.Tasks.ValueTask<bool>>!
static Polly.PredicateBuilder<TResult>.implicit operator System.Func<Polly.Fallback.FallbackPredicateArguments<TResult>, System.Threading.Tasks.ValueTask<bool>>!(Polly.PredicateBuilder<TResult>! builder) -> System.Func<Polly.Fallback.FallbackPredicateArguments<TResult>, System.Threading.Tasks.ValueTask<bool>>!
static Polly.PredicateBuilder<TResult>.implicit operator System.Func<Polly.Hedging.HedgingPredicateArguments<TResult>, System.Threading.Tasks.ValueTask<bool>>!(Polly.PredicateBuilder<TResult>! builder) -> System.Func<Polly.Hedging.HedgingPredicateArguments<TResult>, System.Threading.Tasks.ValueTask<bool>>!
static Polly.PredicateBuilder<TResult>.implicit operator System.Func<Polly.Retry.RetryPredicateArguments<TResult>, System.Threading.Tasks.ValueTask<bool>>!(Polly.PredicateBuilder<TResult>! builder) -> System.Func<Polly.Retry.RetryPredicateArguments<TResult>, System.Threading.Tasks.ValueTask<bool>>!
static Polly.PredicateResult.False.get -> System.Threading.Tasks.ValueTask<bool>
static Polly.PredicateResult.True.get -> System.Threading.Tasks.ValueTask<bool>
static Polly.PredicateResult.False() -> System.Threading.Tasks.ValueTask<bool>
static Polly.PredicateResult.True() -> System.Threading.Tasks.ValueTask<bool>
static Polly.ResilienceContextPool.Shared.get -> Polly.ResilienceContextPool!
static Polly.ResiliencePipelineBuilderExtensions.AddPipeline<TBuilder>(this TBuilder! builder, Polly.ResiliencePipeline! pipeline) -> TBuilder!
static Polly.ResiliencePipelineBuilderExtensions.AddPipeline<TResult>(this Polly.ResiliencePipelineBuilder<TResult>! builder, Polly.ResiliencePipeline<TResult>! pipeline) -> Polly.ResiliencePipelineBuilder<TResult>!
Expand Down
14 changes: 7 additions & 7 deletions src/Polly.Core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ new ResiliencePipelineBuilder()
{
ShouldRetry = args => args switch
{
{ Exception: InvalidOperationException } => PredicateResult.True,
{ Result: string result } when result == Failure => PredicateResult.True,
{ Result: int result } when result == -1 => PredicateResult.True,
_ => PredicateResult.False
{ Exception: InvalidOperationException } => PredicateResult.True(),
{ Result: string result } when result == Failure => PredicateResult.True(),
{ Result: int result } when result == -1 => PredicateResult.True(),
_ => PredicateResult.False()
},
})
.Build();
Expand All @@ -243,9 +243,9 @@ new ResiliencePipelineBuilder()
{
ShouldRetry = args => args switch
{
{ Exception: InvalidOperationException } => PredicateResult.True,
{ Result: result } when result == Failure => PredicateResult.True,
_ => PredicateResult.False
{ Exception: InvalidOperationException } => PredicateResult.True(),
{ Result: result } when result == Failure => PredicateResult.True(),
_ => PredicateResult.False()
},
})
.Build();
Expand Down
Loading