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

Cleanup the Polly and Polly.Specs codebase #2467

Merged
merged 10 commits into from
Jan 28, 2025
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
4 changes: 4 additions & 0 deletions src/Polly/AsyncPolicy.ExecuteOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ private async Task<PolicyResult> ExecuteAndCaptureInternalAsync(
await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
return PolicyResult.Successful(context);
}
#pragma warning disable CA1031
catch (Exception exception)
#pragma warning restore CA1031
{
return PolicyResult.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context);
}
Expand All @@ -529,7 +531,9 @@ private async Task<PolicyResult<TResult>> ExecuteAndCaptureInternalAsync<TResult
await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext)
.ConfigureAwait(continueOnCapturedContext), context);
}
#pragma warning disable CA1031
catch (Exception exception)
#pragma warning restore CA1031
{
return PolicyResult<TResult>.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ private async Task<PolicyResult<TResult>> ExecuteAndCaptureInternalAsync(

return PolicyResult<TResult>.Successful(result, context);
}
#pragma warning disable CA1031
catch (Exception exception)
#pragma warning restore CA1031
{
return PolicyResult<TResult>.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Polly/Bulkhead/AsyncBulkheadPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ namespace Polly.Bulkhead;
/// <summary>
/// A bulkhead-isolation policy which can be applied to delegates.
/// </summary>
#pragma warning disable CA1063
public class AsyncBulkheadPolicy : AsyncPolicy, IBulkheadPolicy
#pragma warning restore CA1063
{
private readonly SemaphoreSlim _maxParallelizationSemaphore;
private readonly SemaphoreSlim _maxQueuedActionsSemaphore;
Expand Down Expand Up @@ -55,8 +57,10 @@ protected override Task<TResult> ImplementationAsync<TResult>(
cancellationToken);
}

#pragma warning disable CA1063
/// <inheritdoc/>
public void Dispose()
#pragma warning restore CA1063
{
_maxParallelizationSemaphore.Dispose();
_maxQueuedActionsSemaphore.Dispose();
Expand All @@ -68,7 +72,9 @@ public void Dispose()
/// A bulkhead-isolation policy which can be applied to delegates.
/// </summary>
/// <typeparam name="TResult">The return type of delegates which may be executed through the policy.</typeparam>
#pragma warning disable CA1063
public class AsyncBulkheadPolicy<TResult> : AsyncPolicy<TResult>, IBulkheadPolicy<TResult>
#pragma warning restore CA1063
{
private readonly SemaphoreSlim _maxParallelizationSemaphore;
private readonly SemaphoreSlim _maxQueuedActionsSemaphore;
Expand Down Expand Up @@ -119,8 +125,10 @@ protected override Task<TResult> ImplementationAsync(
/// </summary>
public int QueueAvailableCount => Math.Min(_maxQueuedActionsSemaphore.CurrentCount, _maxQueueingActions);

#pragma warning disable CA1063
/// <inheritdoc/>
public void Dispose()
#pragma warning restore CA1063
{
_maxParallelizationSemaphore.Dispose();
_maxQueuedActionsSemaphore.Dispose();
Expand Down
2 changes: 2 additions & 0 deletions src/Polly/Bulkhead/AsyncBulkheadSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#nullable enable
namespace Polly;

#pragma warning disable CA1724
public partial class Policy
#pragma warning restore CA1724
{
/// <summary>
/// <para>Builds a bulkhead isolation <see cref="Policy"/>, which limits the maximum concurrency of actions executed through the policy. Imposing a maximum concurrency limits the potential of governed actions, when faulting, to bring down the system.</para>
Expand Down
8 changes: 8 additions & 0 deletions src/Polly/Bulkhead/BulkheadPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ namespace Polly.Bulkhead;
/// <summary>
/// A bulkhead-isolation policy which can be applied to delegates.
/// </summary>
#pragma warning disable CA1063
public class BulkheadPolicy : Policy, IBulkheadPolicy
#pragma warning restore CA1063
{
private readonly SemaphoreSlim _maxParallelizationSemaphore;
private readonly SemaphoreSlim _maxQueuedActionsSemaphore;
Expand Down Expand Up @@ -50,11 +52,13 @@ protected override TResult Implementation<TResult>(Func<Context, CancellationTok
/// </summary>
public int QueueAvailableCount => Math.Min(_maxQueuedActionsSemaphore.CurrentCount, _maxQueueingActions);

#pragma warning disable CA1063
/// <summary>
/// Disposes of the <see cref="BulkheadPolicy"/>, allowing it to dispose its internal resources.
/// <remarks>Only call <see cref="Dispose()"/> on a <see cref="BulkheadPolicy"/> after all actions executed through the policy have completed. If actions are still executing through the policy when <see cref="Dispose()"/> is called, an <see cref="ObjectDisposedException"/> may be thrown on the actions' threads when those actions complete.</remarks>
/// </summary>
public void Dispose()
#pragma warning restore CA1063
{
_maxParallelizationSemaphore.Dispose();
_maxQueuedActionsSemaphore.Dispose();
Expand All @@ -66,7 +70,9 @@ public void Dispose()
/// A bulkhead-isolation policy which can be applied to delegates returning a value of type <typeparamref name="TResult"/>.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
#pragma warning disable CA1063
public class BulkheadPolicy<TResult> : Policy<TResult>, IBulkheadPolicy<TResult>
#pragma warning restore CA1063
{
private readonly SemaphoreSlim _maxParallelizationSemaphore;
private readonly SemaphoreSlim _maxQueuedActionsSemaphore;
Expand Down Expand Up @@ -111,11 +117,13 @@ protected override TResult Implementation(Func<Context, CancellationToken, TResu
/// </summary>
public int QueueAvailableCount => Math.Min(_maxQueuedActionsSemaphore.CurrentCount, _maxQueueingActions);

#pragma warning disable CA1063
/// <summary>
/// Disposes of the <see cref="BulkheadPolicy"/>, allowing it to dispose its internal resources.
/// <remarks>Only call <see cref="Dispose()"/> on a <see cref="BulkheadPolicy"/> after all actions executed through the policy have completed. If actions are still executing through the policy when <see cref="Dispose()"/> is called, an <see cref="ObjectDisposedException"/> may be thrown on the actions' threads when those actions complete.</remarks>
/// </summary>
public void Dispose()
#pragma warning restore CA1063
{
_maxParallelizationSemaphore.Dispose();
_maxQueuedActionsSemaphore.Dispose();
Expand Down
4 changes: 4 additions & 0 deletions src/Polly/Caching/AsyncCacheEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ internal static async Task<TResult> ImplementationAsync<TResult>(
{
(cacheHit, valueFromCache) = await cacheProvider.TryGetAsync(cacheKey, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
cacheHit = false;
valueFromCache = default;
Expand All @@ -59,7 +61,9 @@ internal static async Task<TResult> ImplementationAsync<TResult>(
await cacheProvider.PutAsync(cacheKey, result, ttl, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
onCachePut(context, cacheKey);
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
onCachePutError?.Invoke(context, cacheKey, ex);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Polly/Caching/CacheEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ internal static TResult Implementation<TResult>(
{
(cacheHit, valueFromCache) = cacheProvider.TryGet(cacheKey);
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
cacheHit = false;
valueFromCache = default;
Expand All @@ -58,7 +60,9 @@ internal static TResult Implementation<TResult>(
cacheProvider.Put(cacheKey, result, ttl);
onCachePut(context, cacheKey);
}
#pragma warning disable CA1031
catch (Exception ex)
#pragma warning restore CA1031
{
onCachePutError?.Invoke(context, cacheKey, ex);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Polly/Caching/NonSlidingTtl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ namespace Polly.Caching;
/// </summary>
public abstract class NonSlidingTtl : ITtlStrategy
{
#pragma warning disable CA1051 // Do not declare visible instance fields
#pragma warning disable IDE1006
/// <summary>
/// The absolute expiration time for cache items, represented by this strategy.
/// </summary>
protected readonly DateTimeOffset absoluteExpirationTime;
#pragma warning restore CA1051 // Do not declare visible instance fields
#pragma warning restore IDE1006

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Polly/Caching/Ttl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ namespace Polly.Caching;
public struct Ttl
#pragma warning restore CA1815
{
#pragma warning disable CA1051 // Do not declare visible instance fields
/// <summary>
/// The timespan for which this cache-item remains valid.
/// </summary>
public TimeSpan Timespan;
#pragma warning restore CA1051 // Do not declare visible instance fields

#pragma warning disable CA1051 // Do not declare visible instance fields
/// <summary>
/// Whether this <see cref="Ttl"/> should be considered as sliding expiration: that is, the cache item should be considered valid for a further period of duration <see cref="Timespan"/> each time the cache item is retrieved.
/// </summary>
public bool SlidingExpiration;
#pragma warning restore CA1051 // Do not declare visible instance fields

/// <summary>
/// Initializes a new instance of the <see cref="Ttl"/> struct.
Expand Down
2 changes: 2 additions & 0 deletions src/Polly/Policy.ContextAndKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ ISyncPolicy ISyncPolicy.WithPolicyKey(string policyKey)
}
}

#pragma warning disable CA1724
public abstract partial class Policy<TResult>
#pragma warning restore CA1724
{
/// <summary>
/// Sets the PolicyKey for this <see cref="Policy{TResult}"/> instance.
Expand Down
4 changes: 4 additions & 0 deletions src/Polly/Policy.ExecuteOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ public PolicyResult ExecuteAndCapture(Action<Context, CancellationToken> action,
Execute(action, context, cancellationToken);
return PolicyResult.Successful(context);
}
#pragma warning disable CA1031
catch (Exception exception)
#pragma warning restore CA1031
{
return PolicyResult.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context);
}
Expand Down Expand Up @@ -327,7 +329,9 @@ public PolicyResult<TResult> ExecuteAndCapture<TResult>(Func<Context, Cancellati
{
return PolicyResult<TResult>.Successful(Execute(action, context, cancellationToken), context);
}
#pragma warning disable CA1031
catch (Exception exception)
#pragma warning restore CA1031
{
return PolicyResult<TResult>.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Polly/Policy.TResult.ExecuteOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ public PolicyResult<TResult> ExecuteAndCapture(Func<Context, CancellationToken,

return PolicyResult<TResult>.Successful(result, context);
}
#pragma warning disable CA1031
catch (Exception exception)
#pragma warning restore CA1031
{
return PolicyResult<TResult>.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Polly/PolicyBase.ContextAndKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

public abstract partial class PolicyBase
{
#pragma warning disable CA1051 // Do not declare visible instance fields
#pragma warning disable IDE1006
/// <summary>
/// A key intended to be unique to each <see cref="IsPolicy"/> instance.
/// </summary>
protected string policyKeyInternal;
#pragma warning restore CA1051 // Do not declare visible instance fields
#pragma warning restore IDE1006

/// <summary>
/// Gets a key intended to be unique to each <see cref="IsPolicy"/> instance, which is passed with executions as the <see cref="Context.PolicyKey"/> property.
/// </summary>
public string PolicyKey => policyKeyInternal ?? (policyKeyInternal = GetType().Name + "-" + KeyHelper.GuidPart());
public string PolicyKey => policyKeyInternal ??= GetType().Name + "-" + KeyHelper.GuidPart();

internal static ArgumentException PolicyKeyMustBeImmutableException(string policyKeyParamName) => new("PolicyKey cannot be changed once set; or (when using the default value after the PolicyKey property has been accessed.", policyKeyParamName);

Expand Down
6 changes: 2 additions & 4 deletions src/Polly/Polly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
<ProjectType>Library</ProjectType>
<MutationScore>70</MutationScore>
<IncludePollyUsings>true</IncludePollyUsings>
<NoWarn>$(NoWarn);CA1031;CA1051;CA1063;CA1064;CA1724;</NoWarn>
<NoWarn>$(NoWarn);S2223;S3215;</NoWarn>
<!--Public API Analyzers: We do not need to fix these as it would break compatibility with released Polly versions-->
<NoWarn>$(NoWarn);RS0037;</NoWarn>
<!-- We do not plan on enabling nullable annotations for Polly -->
<NoWarn>$(NoWarn);RS0037</NoWarn>
</PropertyGroup>

<PropertyGroup>
Expand Down
11 changes: 10 additions & 1 deletion src/Polly/Utilities/SystemClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,51 @@ namespace Polly.Utilities;
/// </summary>
public static class SystemClock
{
#pragma warning disable S2223 // Non-constant static fields should not be visible
/// <summary>
/// Allows the setting of a custom Thread.Sleep implementation for testing.
/// By default this will use the <see cref="CancellationToken"/>'s <see cref="WaitHandle"/>.
/// </summary>
public static Action<TimeSpan, CancellationToken> Sleep = (timeSpan, cancellationToken) =>
#pragma warning restore S2223 // Non-constant static fields should not be visible
{
if (cancellationToken.WaitHandle.WaitOne(timeSpan))
{
cancellationToken.ThrowIfCancellationRequested();
}
};

#pragma warning disable S2223 // Non-constant static fields should not be visible
/// <summary>
/// Allows the setting of a custom async Sleep implementation for testing.
/// By default this will be a call to <see cref="Task.Delay(TimeSpan)"/> taking a <see cref="CancellationToken"/>.
/// </summary>
public static Func<TimeSpan, CancellationToken, Task> SleepAsync = Task.Delay;
#pragma warning restore S2223 // Non-constant static fields should not be visible

#pragma warning disable S2223 // Non-constant static fields should not be visible
/// <summary>
/// Allows the setting of a custom DateTime.UtcNow implementation for testing.
/// By default this will be a call to <see cref="DateTime.UtcNow"/>.
/// </summary>
public static Func<DateTime> UtcNow = () => DateTime.UtcNow;
#pragma warning restore S2223 // Non-constant static fields should not be visible

#pragma warning disable S2223 // Non-constant static fields should not be visible
/// <summary>
/// Allows the setting of a custom DateTimeOffset.UtcNow implementation for testing.
/// By default this will be a call to <see cref="DateTime.UtcNow"/>.
/// </summary>
public static Func<DateTimeOffset> DateTimeOffsetUtcNow = () => DateTimeOffset.UtcNow;
#pragma warning restore S2223 // Non-constant static fields should not be visible

#pragma warning disable S2223 // Non-constant static fields should not be visible
/// <summary>
/// Allows the setting of a custom method for cancelling tokens after a timespan, for use in testing.
/// By default this will be a call to CancellationTokenSource.CancelAfter(timespan).
/// </summary>
public static Action<CancellationTokenSource, TimeSpan> CancelTokenAfter = (tokenSource, timespan) => tokenSource.CancelAfter(timespan);
#pragma warning restore S2223 // Non-constant static fields should not be visible

/// <summary>
/// Resets the custom implementations to their defaults.
Expand All @@ -65,6 +75,5 @@ public static void Reset()
DateTimeOffsetUtcNow = () => DateTimeOffset.UtcNow;

CancelTokenAfter = (tokenSource, timespan) => tokenSource.CancelAfter(timespan);

}
}
1 change: 1 addition & 0 deletions src/Polly/Utilities/TaskHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Polly.Utilities;
public static class TaskHelper
{
#pragma warning disable CA2211 // Non-constant fields should not be visible
#pragma warning disable S2223 // Non-constant static fields should not be visible
/// <summary>
/// Defines a completed Task for use as a completed, empty asynchronous delegate.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Polly/Utilities/TimedLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ private sealed class Sentinel
#endif
}

#pragma warning disable CA1064 // Exceptions should be public
internal sealed class LockTimeoutException : Exception
#pragma warning restore CA1064 // Exceptions should be public
{
/// <summary>
/// Initializes a new instance of the <see cref="LockTimeoutException"/> class.
Expand Down
Loading
Loading