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

Fix SA1203/S109 #1948

Merged
merged 1 commit into from
Feb 4, 2024
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
10 changes: 5 additions & 5 deletions src/Polly/PolicyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
/// </summary>
public abstract partial class PolicyBase
{
/// <summary>
/// Defines a value to use for continueOnCaptureContext, when none is supplied.
/// </summary>
internal const bool DefaultContinueOnCapturedContext = false;

/// <summary>
/// Predicates indicating which exceptions the policy handles.
/// </summary>
Expand All @@ -15,11 +20,6 @@ public abstract partial class PolicyBase
/// </summary>
internal readonly CancellationToken DefaultCancellationToken = CancellationToken.None;

/// <summary>
/// Defines a value to use for continueOnCaptureContext, when none is supplied.
/// </summary>
internal const bool DefaultContinueOnCapturedContext = false;

internal static ExceptionType GetExceptionType(ExceptionPredicates exceptionPredicates, Exception exception)
{
bool isExceptionTypeHandledByThisPolicy = exceptionPredicates.FirstMatchOrDefault(exception) != null;
Expand Down
19 changes: 14 additions & 5 deletions src/Polly/PolicyResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,21 @@ public static PolicyResult<TResult> Successful(TResult result, Context context)
/// <returns>
/// A <see cref="PolicyResult" /> representing a failed execution through the policy.
/// </returns>
public static PolicyResult<TResult> 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<TResult> Failure(Exception exception, ExceptionType exceptionType, Context context)
{
var faultType = exceptionType == Polly.ExceptionType.HandledByThisPolicy
? Polly.FaultType.ExceptionHandledByThisPolicy
: Polly.FaultType.UnhandledException;

return new PolicyResult<TResult>(
default,
OutcomeType.Failure,
exception,
exceptionType,
default,
faultType,
context);
}

/// <summary>
/// Builds a <see cref="PolicyResult" /> representing a failed execution through the policy.
Expand Down
4 changes: 2 additions & 2 deletions src/Polly/Polly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<IncludePollyUsings>true</IncludePollyUsings>
<NoWarn>$(NoWarn);IDE0011;SA1501;S103;IDE0055;S3872;SA1402;SA1414;S3215;S2955</NoWarn>
<NoWarn>$(NoWarn);IDE1006;CA1062;S107;CA1068;S4039;CA1000;CA1063;CA1031;CA1051</NoWarn>
<NoWarn>$(NoWarn);SA1612;CA2211;S2223;CA1032;CA1815;CA1816;S4457;SA1615;S109;SA1618;CA1033</NoWarn>
<NoWarn>$(NoWarn);SA1515;S4023;CA1010;S2681;S3442;S3880;CA1064;SA1203;SA1649;SA1625;SA1623;SA1118</NoWarn>
<NoWarn>$(NoWarn);SA1612;CA2211;S2223;CA1032;CA1815;CA1816;S4457;SA1615;SA1618;CA1033</NoWarn>
<NoWarn>$(NoWarn);SA1515;S4023;CA1010;S2681;S3442;S3880;CA1064;SA1649;SA1625;SA1623;SA1118</NoWarn>
<NoWarn>$(NoWarn);S3253;S3971;S6605;CA1724;CA1716;SA1108;CA1710;S4049;S3246</NoWarn>
<NoWarn>$(NoWarn);CA1805;CA1821</NoWarn>

Expand Down
20 changes: 14 additions & 6 deletions src/Polly/Retry/AsyncRetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ internal AsyncRetryPolicy(

/// <inheritdoc/>
[DebuggerStepThrough]
protected override Task<TResult> ImplementationAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken,
bool continueOnCapturedContext) =>
AsyncRetryEngine.ImplementationAsync(

protected override Task<TResult> ImplementationAsync<TResult>(
Func<Context, CancellationToken, Task<TResult>> action,
Context context,
CancellationToken cancellationToken,
bool continueOnCapturedContext)
{
var sleepDurationProvider = _sleepDurationProvider != null
? (retryCount, outcome, ctx) => _sleepDurationProvider(retryCount, outcome.Exception, ctx)
: (Func<int, DelegateResult<TResult>, Context, TimeSpan>)null;

return AsyncRetryEngine.ImplementationAsync(
action,
context,
cancellationToken,
Expand All @@ -37,10 +46,9 @@ protected override Task<TResult> ImplementationAsync<TResult>(Func<Context, Canc
(outcome, timespan, retryCount, ctx) => _onRetryAsync(outcome.Exception, timespan, retryCount, ctx),
_permittedRetryCount,
_sleepDurationsEnumerable,
_sleepDurationProvider != null
? (retryCount, outcome, ctx) => _sleepDurationProvider(retryCount, outcome.Exception, ctx)
: (Func<int, DelegateResult<TResult>, Context, TimeSpan>) null,
sleepDurationProvider,
continueOnCapturedContext);
}
}

/// <summary>
Expand Down
15 changes: 9 additions & 6 deletions src/Polly/Retry/RetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ internal RetryPolicy(
}

/// <inheritdoc/>
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken) =>
RetryEngine.Implementation(
action,
protected override TResult Implementation<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
var sleepDurationProvider = _sleepDurationProvider != null
? (retryCount, outcome, ctx) => _sleepDurationProvider(retryCount, outcome.Exception, ctx)
: (Func<int, DelegateResult<TResult>, Context, TimeSpan>?)null;

return RetryEngine.Implementation(action,
context,
cancellationToken,
ExceptionPredicates,
ResultPredicates<TResult>.None,
(outcome, timespan, retryCount, ctx) => _onRetry(outcome.Exception, timespan, retryCount, ctx),
_permittedRetryCount,
_sleepDurationsEnumerable,
_sleepDurationProvider != null
? (retryCount, outcome, ctx) => _sleepDurationProvider(retryCount, outcome.Exception, ctx)
: (Func<int, DelegateResult<TResult>, Context, TimeSpan>?) null);
sleepDurationProvider);
}
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/Polly/Utilities/KeyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
10 changes: 6 additions & 4 deletions src/Polly/Wrap/AsyncPolicyWrapSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public AsyncPolicyWrap<TResult> WrapAsync(IAsyncPolicy<TResult> innerPolicy)

public partial class Policy
{
private const int MinimumPoliciesRequiredForWrap = 2;

/// <summary>
/// Creates a <see cref="PolicyWrap" /> of the given policies.
/// </summary>
Expand All @@ -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())),
};

Expand All @@ -143,8 +145,8 @@ public static AsyncPolicyWrap WrapAsync(params IAsyncPolicy[] policies) =>
public static AsyncPolicyWrap<TResult> WrapAsync<TResult>(params IAsyncPolicy<TResult>[] 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<TResult>((AsyncPolicy<TResult>)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<TResult>((AsyncPolicy<TResult>)policies[0], policies[1]),
_ => WrapAsync(policies[0], WrapAsync(policies.Skip(1).ToArray())),
};
}
8 changes: 4 additions & 4 deletions src/Polly/Wrap/PolicyWrapSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
};

Expand All @@ -143,8 +143,8 @@ public static PolicyWrap Wrap(params ISyncPolicy[] policies) =>
public static PolicyWrap<TResult> Wrap<TResult>(params ISyncPolicy<TResult>[] 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<TResult>((Policy<TResult>)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<TResult>((Policy<TResult>)policies[0], policies[1]),
_ => Wrap(policies[0], Wrap(policies.Skip(1).ToArray())),
};
}