From b4c397bc18b9ca0252712f55f8235f05b77986c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baranyai=20M=C3=A1t=C3=A9?= Date: Sun, 4 Feb 2024 08:14:49 +0100 Subject: [PATCH] Cleanup SA1203/S109 --- src/Polly/PolicyBase.cs | 10 +++++----- src/Polly/PolicyResult.cs | 19 ++++++++++++++----- src/Polly/Polly.csproj | 4 ++-- src/Polly/Retry/AsyncRetryPolicy.cs | 20 ++++++++++++++------ src/Polly/Retry/RetryPolicy.cs | 15 +++++++++------ src/Polly/Utilities/KeyHelper.cs | 4 +++- src/Polly/Wrap/AsyncPolicyWrapSyntax.cs | 10 ++++++---- src/Polly/Wrap/PolicyWrapSyntax.cs | 8 ++++---- 8 files changed, 57 insertions(+), 33 deletions(-) diff --git a/src/Polly/PolicyBase.cs b/src/Polly/PolicyBase.cs index 3be8e81e9b9..7ad06e02981 100644 --- a/src/Polly/PolicyBase.cs +++ b/src/Polly/PolicyBase.cs @@ -5,6 +5,11 @@ /// public abstract partial class PolicyBase { + /// + /// Defines a value to use for continueOnCaptureContext, when none is supplied. + /// + internal const bool DefaultContinueOnCapturedContext = false; + /// /// Predicates indicating which exceptions the policy handles. /// @@ -15,11 +20,6 @@ public abstract partial class PolicyBase /// internal readonly CancellationToken DefaultCancellationToken = CancellationToken.None; - /// - /// Defines a value to use for continueOnCaptureContext, when none is supplied. - /// - internal const bool DefaultContinueOnCapturedContext = false; - internal static ExceptionType GetExceptionType(ExceptionPredicates exceptionPredicates, Exception exception) { bool isExceptionTypeHandledByThisPolicy = exceptionPredicates.FirstMatchOrDefault(exception) != null; diff --git a/src/Polly/PolicyResult.cs b/src/Polly/PolicyResult.cs index 5c3774dc6a1..0de58d7e0d9 100644 --- a/src/Polly/PolicyResult.cs +++ b/src/Polly/PolicyResult.cs @@ -132,12 +132,21 @@ public static PolicyResult Successful(TResult result, Context context) /// /// A representing a failed execution through the policy. /// - public static PolicyResult Failure(Exception exception, ExceptionType exceptionType, Context context) => - new(default, OutcomeType.Failure, exception, exceptionType, default, - exceptionType == Polly.ExceptionType.HandledByThisPolicy - ? Polly.FaultType.ExceptionHandledByThisPolicy - : Polly.FaultType.UnhandledException, + public static PolicyResult Failure(Exception exception, ExceptionType exceptionType, Context context) + { + var faultType = exceptionType == Polly.ExceptionType.HandledByThisPolicy + ? Polly.FaultType.ExceptionHandledByThisPolicy + : Polly.FaultType.UnhandledException; + + return new PolicyResult( + default, + OutcomeType.Failure, + exception, + exceptionType, + default, + faultType, context); + } /// /// Builds a representing a failed execution through the policy. diff --git a/src/Polly/Polly.csproj b/src/Polly/Polly.csproj index 03095db30a2..a1685156cc6 100644 --- a/src/Polly/Polly.csproj +++ b/src/Polly/Polly.csproj @@ -8,8 +8,8 @@ true $(NoWarn);IDE0011;SA1501;S103;IDE0055;S3872;SA1402;SA1414;S3215;S2955 $(NoWarn);IDE1006;CA1062;S107;CA1068;S4039;CA1000;CA1063;CA1031;CA1051 - $(NoWarn);SA1612;CA2211;S2223;CA1032;CA1815;CA1816;S4457;SA1615;S109;SA1618;CA1033 - $(NoWarn);SA1515;S4023;CA1010;S2681;S3442;S3880;CA1064;SA1203;SA1649;SA1625;SA1623;SA1118 + $(NoWarn);SA1612;CA2211;S2223;CA1032;CA1815;CA1816;S4457;SA1615;SA1618;CA1033 + $(NoWarn);SA1515;S4023;CA1010;S2681;S3442;S3880;CA1064;SA1649;SA1625;SA1623;SA1118 $(NoWarn);S3253;S3971;S6605;CA1724;CA1716;SA1108;CA1710;S4049;S3246 $(NoWarn);CA1805;CA1821 diff --git a/src/Polly/Retry/AsyncRetryPolicy.cs b/src/Polly/Retry/AsyncRetryPolicy.cs index 1baa7ea197c..f7c2081f621 100644 --- a/src/Polly/Retry/AsyncRetryPolicy.cs +++ b/src/Polly/Retry/AsyncRetryPolicy.cs @@ -26,9 +26,18 @@ internal AsyncRetryPolicy( /// [DebuggerStepThrough] - protected override Task ImplementationAsync(Func> action, Context context, CancellationToken cancellationToken, - bool continueOnCapturedContext) => - AsyncRetryEngine.ImplementationAsync( + + protected override Task ImplementationAsync( + Func> action, + Context context, + CancellationToken cancellationToken, + bool continueOnCapturedContext) + { + var sleepDurationProvider = _sleepDurationProvider != null + ? (retryCount, outcome, ctx) => _sleepDurationProvider(retryCount, outcome.Exception, ctx) + : (Func, Context, TimeSpan>)null; + + return AsyncRetryEngine.ImplementationAsync( action, context, cancellationToken, @@ -37,10 +46,9 @@ protected override Task ImplementationAsync(Func _onRetryAsync(outcome.Exception, timespan, retryCount, ctx), _permittedRetryCount, _sleepDurationsEnumerable, - _sleepDurationProvider != null - ? (retryCount, outcome, ctx) => _sleepDurationProvider(retryCount, outcome.Exception, ctx) - : (Func, Context, TimeSpan>) null, + sleepDurationProvider, continueOnCapturedContext); + } } /// diff --git a/src/Polly/Retry/RetryPolicy.cs b/src/Polly/Retry/RetryPolicy.cs index ecf2078c840..06042672875 100644 --- a/src/Polly/Retry/RetryPolicy.cs +++ b/src/Polly/Retry/RetryPolicy.cs @@ -26,9 +26,13 @@ internal RetryPolicy( } /// - protected override TResult Implementation(Func action, Context context, CancellationToken cancellationToken) => - RetryEngine.Implementation( - action, + protected override TResult Implementation(Func action, Context context, CancellationToken cancellationToken) + { + var sleepDurationProvider = _sleepDurationProvider != null + ? (retryCount, outcome, ctx) => _sleepDurationProvider(retryCount, outcome.Exception, ctx) + : (Func, Context, TimeSpan>?)null; + + return RetryEngine.Implementation(action, context, cancellationToken, ExceptionPredicates, @@ -36,9 +40,8 @@ protected override TResult Implementation(Func _onRetry(outcome.Exception, timespan, retryCount, ctx), _permittedRetryCount, _sleepDurationsEnumerable, - _sleepDurationProvider != null - ? (retryCount, outcome, ctx) => _sleepDurationProvider(retryCount, outcome.Exception, ctx) - : (Func, Context, TimeSpan>?) null); + sleepDurationProvider); + } } /// diff --git a/src/Polly/Utilities/KeyHelper.cs b/src/Polly/Utilities/KeyHelper.cs index f7f7c47ca84..fc92a4027f8 100644 --- a/src/Polly/Utilities/KeyHelper.cs +++ b/src/Polly/Utilities/KeyHelper.cs @@ -3,6 +3,8 @@ namespace Polly.Utilities; internal static class KeyHelper { + private const int GuidPartLength = 8; + public static string GuidPart() => - Guid.NewGuid().ToString().Substring(0, 8); + Guid.NewGuid().ToString().Substring(0, GuidPartLength); } diff --git a/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs b/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs index 279ce4e696f..d78f1522be7 100644 --- a/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs +++ b/src/Polly/Wrap/AsyncPolicyWrapSyntax.cs @@ -119,6 +119,8 @@ public AsyncPolicyWrap WrapAsync(IAsyncPolicy innerPolicy) public partial class Policy { + private const int MinimumPoliciesRequiredForWrap = 2; + /// /// Creates a of the given policies. /// @@ -128,8 +130,8 @@ public partial class Policy public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) => policies.Length switch { - 0 or 1 => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), - 2 => new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]), + < MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), + MinimumPoliciesRequiredForWrap => new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]), _ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())), }; @@ -143,8 +145,8 @@ public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) => public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) => policies.Length switch { - 0 or 1 => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), - 2 => new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]), + < MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), + MinimumPoliciesRequiredForWrap => new AsyncPolicyWrap((AsyncPolicy)policies[0], policies[1]), _ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())), }; } diff --git a/src/Polly/Wrap/PolicyWrapSyntax.cs b/src/Polly/Wrap/PolicyWrapSyntax.cs index a549f2531a3..a163b8081d7 100644 --- a/src/Polly/Wrap/PolicyWrapSyntax.cs +++ b/src/Polly/Wrap/PolicyWrapSyntax.cs @@ -128,8 +128,8 @@ public partial class Policy public static PolicyWrap Wrap(params ISyncPolicy[] policies) => policies.Length switch { - 0 or 1 => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), - 2 => new PolicyWrap((Policy)policies[0], policies[1]), + < MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), + MinimumPoliciesRequiredForWrap => new PolicyWrap((Policy)policies[0], policies[1]), _ => Wrap(policies[0], Wrap(policies.Skip(1).ToArray())), }; @@ -143,8 +143,8 @@ public static PolicyWrap Wrap(params ISyncPolicy[] policies) => public static PolicyWrap Wrap(params ISyncPolicy[] policies) => policies.Length switch { - 0 or 1 => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), - 2 => new PolicyWrap((Policy)policies[0], policies[1]), + < MinimumPoliciesRequiredForWrap => throw new ArgumentException("The enumerable of policies to form the wrap must contain at least two policies.", nameof(policies)), + MinimumPoliciesRequiredForWrap => new PolicyWrap((Policy)policies[0], policies[1]), _ => Wrap(policies[0], Wrap(policies.Skip(1).ToArray())), }; }