diff --git a/src/Polly/Bulkhead/BulkheadRejectedException.cs b/src/Polly/Bulkhead/BulkheadRejectedException.cs index c77f51afb2e..39b7d3a15e8 100644 --- a/src/Polly/Bulkhead/BulkheadRejectedException.cs +++ b/src/Polly/Bulkhead/BulkheadRejectedException.cs @@ -16,7 +16,8 @@ public class BulkheadRejectedException : ExecutionRejectedException /// /// Initializes a new instance of the class. /// - public BulkheadRejectedException() : this("The bulkhead semaphore and queue are full and execution was rejected.") + public BulkheadRejectedException() + : this("The bulkhead semaphore and queue are full and execution was rejected.") { } @@ -24,7 +25,8 @@ public BulkheadRejectedException() : this("The bulkhead semaphore and queue are /// Initializes a new instance of the class. /// /// The message. - public BulkheadRejectedException(string message) : base(message) + public BulkheadRejectedException(string message) + : base(message) { } @@ -33,7 +35,8 @@ public BulkheadRejectedException(string message) : base(message) /// /// The message. /// The inner exception. - public BulkheadRejectedException(string message, Exception innerException) : base(message, innerException) + public BulkheadRejectedException(string message, Exception innerException) + : base(message, innerException) { } @@ -44,7 +47,8 @@ public BulkheadRejectedException(string message, Exception innerException) : bas /// /// The information. /// The context. - protected BulkheadRejectedException(SerializationInfo info, StreamingContext context) : base(info, context) + protected BulkheadRejectedException(SerializationInfo info, StreamingContext context) + : base(info, context) { } #endif diff --git a/src/Polly/Caching/AbsoluteTtl.cs b/src/Polly/Caching/AbsoluteTtl.cs index cf1d6f93b33..1f0da8c6408 100644 --- a/src/Polly/Caching/AbsoluteTtl.cs +++ b/src/Polly/Caching/AbsoluteTtl.cs @@ -10,7 +10,8 @@ public class AbsoluteTtl : NonSlidingTtl /// Initializes a new instance of the class. /// /// The UTC point in time until which to consider the cache item valid. - public AbsoluteTtl(DateTimeOffset absoluteExpirationTime) : base(absoluteExpirationTime) + public AbsoluteTtl(DateTimeOffset absoluteExpirationTime) + : base(absoluteExpirationTime) { } } diff --git a/src/Polly/Caching/Ttl.cs b/src/Polly/Caching/Ttl.cs index bdaf3b909c3..1259620710e 100644 --- a/src/Polly/Caching/Ttl.cs +++ b/src/Polly/Caching/Ttl.cs @@ -21,7 +21,8 @@ public struct Ttl /// /// The timespan for which this cache-item remains valid. /// Will be considered as not denoting sliding expiration. - public Ttl(TimeSpan timeSpan) : this(timeSpan, false) + public Ttl(TimeSpan timeSpan) + : this(timeSpan, false) { } diff --git a/src/Polly/CircuitBreaker/AdvancedCircuitController.cs b/src/Polly/CircuitBreaker/AdvancedCircuitController.cs index 6c959aac478..8980cc4698e 100644 --- a/src/Polly/CircuitBreaker/AdvancedCircuitController.cs +++ b/src/Polly/CircuitBreaker/AdvancedCircuitController.cs @@ -17,7 +17,8 @@ public AdvancedCircuitController( Action, CircuitState, TimeSpan, Context> onBreak, Action onReset, Action onHalfOpen - ) : base(durationOfBreak, onBreak, onReset, onHalfOpen) + ) + : base(durationOfBreak, onBreak, onReset, onHalfOpen) { _metrics = samplingDuration.Ticks < ResolutionOfCircuitTimer * NumberOfWindows ? new SingleHealthMetrics(samplingDuration) diff --git a/src/Polly/CircuitBreaker/AsyncCircuitBreakerPolicy.cs b/src/Polly/CircuitBreaker/AsyncCircuitBreakerPolicy.cs index 99f93a860a9..4988a3819f9 100644 --- a/src/Polly/CircuitBreaker/AsyncCircuitBreakerPolicy.cs +++ b/src/Polly/CircuitBreaker/AsyncCircuitBreakerPolicy.cs @@ -10,7 +10,8 @@ public class AsyncCircuitBreakerPolicy : AsyncPolicy, ICircuitBreakerPolicy internal AsyncCircuitBreakerPolicy( PolicyBuilder policyBuilder, ICircuitController breakerController - ) : base(policyBuilder) => + ) + : base(policyBuilder) => _breakerController = breakerController; /// @@ -64,7 +65,8 @@ public class AsyncCircuitBreakerPolicy : AsyncPolicy, ICircuit internal AsyncCircuitBreakerPolicy( PolicyBuilder policyBuilder, ICircuitController breakerController - ) : base(policyBuilder) => + ) + : base(policyBuilder) => _breakerController = breakerController; /// diff --git a/src/Polly/CircuitBreaker/CircuitBreakerPolicy.cs b/src/Polly/CircuitBreaker/CircuitBreakerPolicy.cs index 015e80d9975..d83e260a9a0 100644 --- a/src/Polly/CircuitBreaker/CircuitBreakerPolicy.cs +++ b/src/Polly/CircuitBreaker/CircuitBreakerPolicy.cs @@ -10,7 +10,8 @@ public class CircuitBreakerPolicy : Policy, ICircuitBreakerPolicy internal CircuitBreakerPolicy( PolicyBuilder policyBuilder, ICircuitController breakerController - ) : base(policyBuilder) => + ) + : base(policyBuilder) => _breakerController = breakerController; /// @@ -62,7 +63,8 @@ public class CircuitBreakerPolicy : Policy, ICircuitBreakerPol internal CircuitBreakerPolicy( PolicyBuilder policyBuilder, ICircuitController breakerController - ) : base(policyBuilder) => + ) + : base(policyBuilder) => _breakerController = breakerController; /// diff --git a/src/Polly/CircuitBreaker/ConsecutiveCountCircuitController.cs b/src/Polly/CircuitBreaker/ConsecutiveCountCircuitController.cs index d4214391997..331dd9f011e 100644 --- a/src/Polly/CircuitBreaker/ConsecutiveCountCircuitController.cs +++ b/src/Polly/CircuitBreaker/ConsecutiveCountCircuitController.cs @@ -11,7 +11,8 @@ public ConsecutiveCountCircuitController( Action, CircuitState, TimeSpan, Context> onBreak, Action onReset, Action onHalfOpen - ) : base(durationOfBreak, onBreak, onReset, onHalfOpen) => + ) + : base(durationOfBreak, onBreak, onReset, onHalfOpen) => _exceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking; public override void OnCircuitReset(Context context) diff --git a/src/Polly/Context.Dictionary.cs b/src/Polly/Context.Dictionary.cs index d0090ceba72..c8a42c0b679 100644 --- a/src/Polly/Context.Dictionary.cs +++ b/src/Polly/Context.Dictionary.cs @@ -18,10 +18,12 @@ public partial class Context : IDictionary, IDictionary, IReadOn /// /// The operation key. /// The context data. - public Context(string operationKey, IDictionary contextData) : this(contextData) => + public Context(string operationKey, IDictionary contextData) + : this(contextData) => OperationKey = operationKey; - internal Context(IDictionary contextData) : this() + internal Context(IDictionary contextData) + : this() { if (contextData == null) throw new ArgumentNullException(nameof(contextData)); wrappedDictionary = new Dictionary(contextData); diff --git a/src/Polly/Fallback/AsyncFallbackPolicy.cs b/src/Polly/Fallback/AsyncFallbackPolicy.cs index 284d8a6cef8..9765dd03b4e 100644 --- a/src/Polly/Fallback/AsyncFallbackPolicy.cs +++ b/src/Polly/Fallback/AsyncFallbackPolicy.cs @@ -61,7 +61,8 @@ internal AsyncFallbackPolicy( PolicyBuilder policyBuilder, Func, Context, Task> onFallbackAsync, Func, Context, CancellationToken, Task> fallbackAction - ) : base(policyBuilder) + ) + : base(policyBuilder) { _onFallbackAsync = onFallbackAsync ?? throw new ArgumentNullException(nameof(onFallbackAsync)); _fallbackAction = fallbackAction ?? throw new ArgumentNullException(nameof(fallbackAction)); diff --git a/src/Polly/Fallback/FallbackPolicy.cs b/src/Polly/Fallback/FallbackPolicy.cs index c4cb6c58819..7581427079d 100644 --- a/src/Polly/Fallback/FallbackPolicy.cs +++ b/src/Polly/Fallback/FallbackPolicy.cs @@ -57,7 +57,8 @@ internal FallbackPolicy( PolicyBuilder policyBuilder, Action, Context> onFallback, Func, Context, CancellationToken, TResult> fallbackAction - ) : base(policyBuilder) + ) + : base(policyBuilder) { _onFallback = onFallback ?? throw new ArgumentNullException(nameof(onFallback)); _fallbackAction = fallbackAction ?? throw new ArgumentNullException(nameof(fallbackAction)); diff --git a/src/Polly/Policy.HandleSyntax.cs b/src/Polly/Policy.HandleSyntax.cs index af18cccc647..99dc69af7db 100644 --- a/src/Polly/Policy.HandleSyntax.cs +++ b/src/Polly/Policy.HandleSyntax.cs @@ -7,7 +7,9 @@ public partial class Policy /// /// The type of the exception to handle. /// The PolicyBuilder instance, for fluent chaining. - public static PolicyBuilder Handle() where TException : Exception => + public static PolicyBuilder Handle() + where TException : Exception + => new(exception => exception is TException ? exception : null); /// @@ -16,7 +18,9 @@ public static PolicyBuilder Handle() where TException : Exception => /// The type of the exception. /// The exception predicate to filter the type of exception this policy can handle. /// The PolicyBuilder instance, for fluent chaining. - public static PolicyBuilder Handle(Func exceptionPredicate) where TException : Exception => + public static PolicyBuilder Handle(Func exceptionPredicate) + where TException : Exception + => new(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null); /// @@ -24,7 +28,9 @@ public static PolicyBuilder Handle(Func exceptionP /// /// The type of the exception to handle. /// The PolicyBuilder instance, for fluent chaining. - public static PolicyBuilder HandleInner() where TException : Exception => + public static PolicyBuilder HandleInner() + where TException : Exception + => new(PolicyBuilder.HandleInner(ex => ex is TException)); /// @@ -32,7 +38,9 @@ public static PolicyBuilder HandleInner() where TException : Excepti /// /// The type of the exception to handle. /// The PolicyBuilder instance, for fluent chaining. - public static PolicyBuilder HandleInner(Func exceptionPredicate) where TException : Exception => + public static PolicyBuilder HandleInner(Func exceptionPredicate) + where TException : Exception + => new(PolicyBuilder.HandleInner(ex => ex is TException texception && exceptionPredicate(texception))); /// @@ -62,7 +70,9 @@ public partial class Policy /// /// The type of the exception to handle. /// The PolicyBuilder instance. - public static PolicyBuilder Handle() where TException : Exception => + public static PolicyBuilder Handle() + where TException : Exception + => new(exception => exception is TException ? exception : null); /// @@ -71,7 +81,9 @@ public static PolicyBuilder Handle() where TException : Exc /// The type of the exception. /// The exception predicate to filter the type of exception this policy can handle. /// The PolicyBuilder instance. - public static PolicyBuilder Handle(Func exceptionPredicate) where TException : Exception => + public static PolicyBuilder Handle(Func exceptionPredicate) + where TException : Exception + => new(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null); /// @@ -79,7 +91,9 @@ public static PolicyBuilder Handle(Func e /// /// The type of the exception to handle. /// The PolicyBuilder instance, for fluent chaining. - public static PolicyBuilder HandleInner() where TException : Exception => + public static PolicyBuilder HandleInner() + where TException : Exception + => new(PolicyBuilder.HandleInner(ex => ex is TException)); /// @@ -87,7 +101,9 @@ public static PolicyBuilder HandleInner() where TException /// /// The type of the exception to handle. /// The PolicyBuilder instance, for fluent chaining. - public static PolicyBuilder HandleInner(Func exceptionPredicate) where TException : Exception => + public static PolicyBuilder HandleInner(Func exceptionPredicate) + where TException : Exception + => new(PolicyBuilder.HandleInner(ex => ex is TException texception && exceptionPredicate(texception))); /// diff --git a/src/Polly/PolicyBuilder.OrSyntax.cs b/src/Polly/PolicyBuilder.OrSyntax.cs index 21d6c754754..9edbf05b7f8 100644 --- a/src/Polly/PolicyBuilder.OrSyntax.cs +++ b/src/Polly/PolicyBuilder.OrSyntax.cs @@ -9,7 +9,8 @@ public partial class PolicyBuilder /// /// The type of the exception to handle. /// The PolicyBuilder instance. - public PolicyBuilder Or() where TException : Exception + public PolicyBuilder Or() + where TException : Exception { ExceptionPredicates.Add(exception => exception is TException ? exception : null); return this; @@ -21,7 +22,8 @@ public PolicyBuilder Or() where TException : Exception /// The type of the exception. /// The exception predicate to filter the type of exception this policy can handle. /// The PolicyBuilder instance. - public PolicyBuilder Or(Func exceptionPredicate) where TException : Exception + public PolicyBuilder Or(Func exceptionPredicate) + where TException : Exception { ExceptionPredicates.Add(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null); return this; @@ -32,7 +34,8 @@ public PolicyBuilder Or(Func exceptionPredicate) w /// /// The type of the exception to handle. /// The PolicyBuilder instance, for fluent chaining. - public PolicyBuilder OrInner() where TException : Exception + public PolicyBuilder OrInner() + where TException : Exception { ExceptionPredicates.Add(HandleInner(ex => ex is TException)); return this; @@ -44,7 +47,8 @@ public PolicyBuilder OrInner() where TException : Exception /// The type of the exception to handle. /// The exception predicate to filter the type of exception this policy can handle. /// The PolicyBuilder instance, for fluent chaining. - public PolicyBuilder OrInner(Func exceptionPredicate) where TException : Exception + public PolicyBuilder OrInner(Func exceptionPredicate) + where TException : Exception { ExceptionPredicates.Add(HandleInner(exception => exception is TException innerEx && exceptionPredicate(innerEx))); return this; @@ -134,7 +138,8 @@ public PolicyBuilder OrResult(TResult result) => /// /// The type of the exception to handle. /// The PolicyBuilder instance. - public PolicyBuilder Or() where TException : Exception + public PolicyBuilder Or() + where TException : Exception { ExceptionPredicates.Add(exception => exception is TException ? exception : null); return this; @@ -146,9 +151,10 @@ public PolicyBuilder Or() where TException : Exception /// The type of the exception. /// The exception predicate to filter the type of exception this policy can handle. /// The PolicyBuilder instance. - public PolicyBuilder Or(Func exceptionPredicate) where TException : Exception + public PolicyBuilder Or(Func exceptionPredicate) + where TException : Exception { - ExceptionPredicates.Add(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null); + ExceptionPredicates.Add(exception => exception is TException texception && exceptionPredicate(texception) ? exception : null); return this; } @@ -157,7 +163,8 @@ public PolicyBuilder Or(Func exceptionPre /// /// The type of the exception to handle. /// The PolicyBuilder instance, for fluent chaining. - public PolicyBuilder OrInner() where TException : Exception + public PolicyBuilder OrInner() + where TException : Exception { ExceptionPredicates.Add(PolicyBuilder.HandleInner(ex => ex is TException)); return this; @@ -169,7 +176,8 @@ public PolicyBuilder OrInner() where TException : Exception /// The type of the exception to handle. /// The exception predicate to filter the type of exception this policy can handle. /// The PolicyBuilder instance, for fluent chaining. - public PolicyBuilder OrInner(Func exceptionPredicate) where TException : Exception + public PolicyBuilder OrInner(Func exceptionPredicate) + where TException : Exception { ExceptionPredicates.Add(PolicyBuilder.HandleInner(ex => ex is TException texception && exceptionPredicate(texception))); return this; diff --git a/src/Polly/PolicyBuilder.cs b/src/Polly/PolicyBuilder.cs index 1304d1da739..875da928104 100644 --- a/src/Polly/PolicyBuilder.cs +++ b/src/Polly/PolicyBuilder.cs @@ -75,11 +75,11 @@ private PolicyBuilder() ResultPredicates = new ResultPredicates(); } - internal PolicyBuilder(Func resultPredicate) : this() => - OrResult(resultPredicate); + internal PolicyBuilder(Func resultPredicate) + : this() => OrResult(resultPredicate); - internal PolicyBuilder(ExceptionPredicate predicate) : this() => - ExceptionPredicates.Add(predicate); + internal PolicyBuilder(ExceptionPredicate predicate) + : this() => ExceptionPredicates.Add(predicate); internal PolicyBuilder(ExceptionPredicates exceptionPredicates) : this() => diff --git a/src/Polly/Polly.csproj b/src/Polly/Polly.csproj index e4a0c4388cf..fc36ee1af45 100644 --- a/src/Polly/Polly.csproj +++ b/src/Polly/Polly.csproj @@ -6,12 +6,12 @@ Library 70 true - $(NoWarn);IDE0011;SA1501;S103;IDE0055;SA1111;S3872;SA1127;SA1128;SA1402;SA1513;SA1414;S3215;S2955 - $(NoWarn);IDE1006;CA1062;S107;CA1068;S4039;SA1121;CA1000;CA1063;SA1113;CA1031;CA1051;CA1200 - $(NoWarn);SA1629;SA1612;CA2211;S2223;CA1032;CA1815;CA1816;S4457;SA1615;S109;SA1618;SA1407;CA1033 + $(NoWarn);IDE0011;SA1501;S103;IDE0055;SA1111;S3872;SA1402;SA1513;SA1414;S3215;S2955 + $(NoWarn);IDE1006;CA1062;S107;CA1068;S4039;SA1121;CA1000;CA1063;CA1031;CA1051 + $(NoWarn);SA1612;CA2211;S2223;CA1032;CA1815;CA1816;S4457;SA1615;S109;SA1618;CA1033 $(NoWarn);SA1515;S4023;CA1010;S2681;S3442;S3880;CA1064;SA1110;SA1203;SA1649;SA1625;SA1623;SA1118 $(NoWarn);S3253;S3971;S6605;CA1724;CA1716;SA1108;CA1710;S4049;S3246 - $(NoWarn);SA1805;SA1805;CA1805;CA1821 + $(NoWarn);CA1805;CA1821 $(NoWarn);RS0037; diff --git a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs index 7a5ef950006..dc33e21459a 100644 --- a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs +++ b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs @@ -71,13 +71,13 @@ public LockFreeTokenBucketRateLimiter(TimeSpan onePer, long bucketCapacity) // Passing addNextTokenAtTicks merits one token 1 + // And any whole token tick intervals further each merit another. - -ticksTillAddNextToken / addTokenTickInterval; + (-ticksTillAddNextToken / addTokenTickInterval); // We mustn't exceed bucket capacity though. long tokensToAdd = Math.Min(bucketCapacity, tokensMissedAdding); // Work out when tokens would next be due to be added, if we add these tokens. - long newAddNextTokenAtTicks = currentAddNextTokenAtTicks + tokensToAdd * addTokenTickInterval; + long newAddNextTokenAtTicks = currentAddNextTokenAtTicks + (tokensToAdd * addTokenTickInterval); // But if we were way overdue refilling the bucket (there was inactivity for a while), that value would be out-of-date: the next time we add tokens must be at least addTokenTickInterval from now. newAddNextTokenAtTicks = Math.Max(newAddNextTokenAtTicks, now + addTokenTickInterval); diff --git a/src/Polly/RateLimit/RateLimitRejectedException.cs b/src/Polly/RateLimit/RateLimitRejectedException.cs index 6820473e7fc..85d38a2f78a 100644 --- a/src/Polly/RateLimit/RateLimitRejectedException.cs +++ b/src/Polly/RateLimit/RateLimitRejectedException.cs @@ -25,7 +25,8 @@ public class RateLimitRejectedException : ExecutionRejectedException /// Initializes a new instance of the class. /// /// The timespan after which the operation may be retried. - public RateLimitRejectedException(TimeSpan retryAfter) : this(retryAfter, DefaultMessage(retryAfter)) + public RateLimitRejectedException(TimeSpan retryAfter) + : this(retryAfter, DefaultMessage(retryAfter)) { } @@ -34,16 +35,16 @@ public RateLimitRejectedException(TimeSpan retryAfter) : this(retryAfter, Defaul /// /// The timespan after which the operation may be retried. /// The inner exception. - public RateLimitRejectedException(TimeSpan retryAfter, Exception innerException) : base(DefaultMessage(retryAfter), innerException) => - SetRetryAfter(retryAfter); + public RateLimitRejectedException(TimeSpan retryAfter, Exception innerException) + : base(DefaultMessage(retryAfter), innerException) => SetRetryAfter(retryAfter); /// /// Initializes a new instance of the class. /// /// The timespan after which the operation may be retried. /// The message. - public RateLimitRejectedException(TimeSpan retryAfter, string message) : base(message) => - SetRetryAfter(retryAfter); + public RateLimitRejectedException(TimeSpan retryAfter, string message) + : base(message) => SetRetryAfter(retryAfter); /// /// Initializes a new instance of the class. @@ -51,8 +52,8 @@ public RateLimitRejectedException(TimeSpan retryAfter, string message) : base(me /// The message. /// The timespan after which the operation may be retried. /// The inner exception. - public RateLimitRejectedException(TimeSpan retryAfter, string message, Exception innerException) : base(message, innerException) => - SetRetryAfter(retryAfter); + public RateLimitRejectedException(TimeSpan retryAfter, string message, Exception innerException) + : base(message, innerException) => SetRetryAfter(retryAfter); private static string DefaultMessage(TimeSpan retryAfter) => $"The operation has been rate-limited and should be retried after {retryAfter}"; @@ -69,7 +70,8 @@ private void SetRetryAfter(TimeSpan retryAfter) /// /// The information. /// The context. - protected RateLimitRejectedException(SerializationInfo info, StreamingContext context) : base(info, context) + protected RateLimitRejectedException(SerializationInfo info, StreamingContext context) + : base(info, context) { } #endif diff --git a/src/Polly/Registry/IConcurrentPolicyRegistry.cs b/src/Polly/Registry/IConcurrentPolicyRegistry.cs index 9ed179e31f1..0d43ba3993c 100644 --- a/src/Polly/Registry/IConcurrentPolicyRegistry.cs +++ b/src/Polly/Registry/IConcurrentPolicyRegistry.cs @@ -13,7 +13,8 @@ public interface IConcurrentPolicyRegistry : IPolicyRegistry /// The policy to store in the registry. /// The type of Policy. /// True if Policy was added. False otherwise. - bool TryAdd(TKey key, TPolicy policy) where TPolicy : IsPolicy; + bool TryAdd(TKey key, TPolicy policy) + where TPolicy : IsPolicy; /// /// Removes the policy stored under the specified from the registry. @@ -26,7 +27,8 @@ public interface IConcurrentPolicyRegistry : IPolicyRegistry /// /// The type of Policy. /// True if the policy is successfully removed. Otherwise false. - bool TryRemove(TKey key, out TPolicy policy) where TPolicy : IsPolicy; + bool TryRemove(TKey key, out TPolicy policy) + where TPolicy : IsPolicy; /// /// Compares the existing policy for the specified key with a specified policy, and if they are equal, updates the policy with a third value. @@ -36,7 +38,8 @@ public interface IConcurrentPolicyRegistry : IPolicyRegistry /// The policy that replaces the value for the specified , if the comparison results in equality. /// The policy that is compared to the existing policy at the specified key. /// - bool TryUpdate(TKey key, TPolicy newPolicy, TPolicy comparisonPolicy) where TPolicy : IsPolicy; + bool TryUpdate(TKey key, TPolicy newPolicy, TPolicy comparisonPolicy) + where TPolicy : IsPolicy; /// /// Adds a policy with the provided key and policy to the registry @@ -47,7 +50,8 @@ public interface IConcurrentPolicyRegistry : IPolicyRegistry /// The policy for the key. This will be either the existing policy for the key if the /// key is already in the registry, or the new policy for the key as returned by policyFactory /// if the key was not in the registry. - TPolicy GetOrAdd(TKey key, Func policyFactory) where TPolicy : IsPolicy; + TPolicy GetOrAdd(TKey key, Func policyFactory) + where TPolicy : IsPolicy; /// /// Adds a key/policy pair to the registry @@ -57,7 +61,8 @@ public interface IConcurrentPolicyRegistry : IPolicyRegistry /// the value to be added, if the key does not already exist /// The policy for the key. This will be either the existing policy for the key if the /// key is already in the registry, or the new policy if the key was not in the registry. - TPolicy GetOrAdd(TKey key, TPolicy policy) where TPolicy : IsPolicy; + TPolicy GetOrAdd(TKey key, TPolicy policy) + where TPolicy : IsPolicy; /// /// Adds a key/policy pair to the registry if the key does not already @@ -70,7 +75,8 @@ public interface IConcurrentPolicyRegistry : IPolicyRegistry /// based on the key's existing value /// The new policy for the key. This will be either be the result of addPolicyFactory (if the key was /// absent) or the result of updatePolicyFactory (if the key was present). - TPolicy AddOrUpdate(TKey key, Func addPolicyFactory, Func updatePolicyFactory) where TPolicy : IsPolicy; + TPolicy AddOrUpdate(TKey key, Func addPolicyFactory, Func updatePolicyFactory) + where TPolicy : IsPolicy; /// /// Adds a key/policy pair to the registry if the key does not already @@ -83,5 +89,6 @@ public interface IConcurrentPolicyRegistry : IPolicyRegistry /// the key's existing value /// The new policy for the key. This will be either be addPolicy (if the key was /// absent) or the result of updatePolicyFactory (if the key was present). - TPolicy AddOrUpdate(TKey key, TPolicy addPolicy, Func updatePolicyFactory) where TPolicy : IsPolicy; + TPolicy AddOrUpdate(TKey key, TPolicy addPolicy, Func updatePolicyFactory) + where TPolicy : IsPolicy; } diff --git a/src/Polly/Registry/IPolicyRegistry.cs b/src/Polly/Registry/IPolicyRegistry.cs index 5ba13b4d559..e9fd8360328 100644 --- a/src/Polly/Registry/IPolicyRegistry.cs +++ b/src/Polly/Registry/IPolicyRegistry.cs @@ -14,7 +14,8 @@ public interface IPolicyRegistry : IReadOnlyPolicyRegistry /// The type of Policy. /// is null. /// A Policy with same already exists. - void Add(TKey key, TPolicy policy) where TPolicy : IsPolicy; + void Add(TKey key, TPolicy policy) + where TPolicy : IsPolicy; /// /// Gets or sets the with the specified key. diff --git a/src/Polly/Registry/IReadOnlyPolicyRegistry.cs b/src/Polly/Registry/IReadOnlyPolicyRegistry.cs index 56f68fadd9e..477a2fa388b 100644 --- a/src/Polly/Registry/IReadOnlyPolicyRegistry.cs +++ b/src/Polly/Registry/IReadOnlyPolicyRegistry.cs @@ -22,7 +22,8 @@ public interface IReadOnlyPolicyRegistry : IEnumerableThe type of Policy. /// The policy stored in the registry under the given key. /// is null. - TPolicy Get(TKey key) where TPolicy : IsPolicy; + TPolicy Get(TKey key) + where TPolicy : IsPolicy; /// /// Gets the policy stored under the provided key, casting to . @@ -35,7 +36,8 @@ public interface IReadOnlyPolicyRegistry : IEnumerable /// The type of Policy. /// True if Policy exists for the provided Key. False otherwise. - bool TryGet(TKey key, out TPolicy policy) where TPolicy : IsPolicy; + bool TryGet(TKey key, out TPolicy policy) + where TPolicy : IsPolicy; /// /// Total number of policies in the registry. diff --git a/src/Polly/Registry/PolicyRegistry.cs b/src/Polly/Registry/PolicyRegistry.cs index e9a5106c403..fb57d7da094 100644 --- a/src/Polly/Registry/PolicyRegistry.cs +++ b/src/Polly/Registry/PolicyRegistry.cs @@ -51,8 +51,8 @@ private ConcurrentDictionary ThrowIfNotConcurrentImplementatio /// The type of Policy. /// is null. /// A Policy with same already exists. - public void Add(string key, TPolicy policy) where TPolicy : IsPolicy => - _registry.Add(key, policy); + public void Add(string key, TPolicy policy) + where TPolicy : IsPolicy => _registry.Add(key, policy); /// /// Adds a policy with the provided key and policy to the registry. @@ -61,7 +61,8 @@ public void Add(string key, TPolicy policy) where TPolicy : IsPolicy => /// The policy to store in the registry. /// The type of Policy. /// True if Policy was added. False otherwise. - public bool TryAdd(string key, TPolicy policy) where TPolicy : IsPolicy + public bool TryAdd(string key, TPolicy policy) + where TPolicy : IsPolicy { var registry = ThrowIfNotConcurrentImplementation(); @@ -90,8 +91,8 @@ public IsPolicy this[string key] /// The policy stored in the registry under the given key. /// is null. /// The given key was not present in the registry. - public TPolicy Get(string key) where TPolicy : IsPolicy => - (TPolicy) _registry[key]; + public TPolicy Get(string key) + where TPolicy : IsPolicy => (TPolicy) _registry[key]; /// /// Gets the policy stored under the provided key, casting to . @@ -104,7 +105,8 @@ public TPolicy Get(string key) where TPolicy : IsPolicy => /// /// The type of Policy. /// True if Policy exists for the provided Key. False otherwise. - public bool TryGet(string key, out TPolicy policy) where TPolicy : IsPolicy + public bool TryGet(string key, out TPolicy policy) + where TPolicy : IsPolicy { bool got = _registry.TryGetValue(key, out IsPolicy value); policy = got ? (TPolicy)value : default; @@ -146,7 +148,8 @@ public bool Remove(string key) => /// /// The type of Policy. /// True if the policy is successfully removed. Otherwise false. - public bool TryRemove(string key, out TPolicy policy) where TPolicy : IsPolicy + public bool TryRemove(string key, out TPolicy policy) + where TPolicy : IsPolicy { var registry = ThrowIfNotConcurrentImplementation(); @@ -166,7 +169,8 @@ public bool TryRemove(string key, out TPolicy policy) where TPolicy : I /// if the value with was equal to and /// replaced with ; otherwise, . /// - public bool TryUpdate(string key, TPolicy newPolicy, TPolicy comparisonPolicy) where TPolicy : IsPolicy + public bool TryUpdate(string key, TPolicy newPolicy, TPolicy comparisonPolicy) + where TPolicy : IsPolicy { var registry = ThrowIfNotConcurrentImplementation(); @@ -182,7 +186,8 @@ public bool TryUpdate(string key, TPolicy newPolicy, TPolicy comparison /// The policy for the key. This will be either the existing policy for the key if the /// key is already in the registry, or the new policy for the key as returned by policyFactory /// if the key was not in the registry. - public TPolicy GetOrAdd(string key, Func policyFactory) where TPolicy : IsPolicy + public TPolicy GetOrAdd(string key, Func policyFactory) + where TPolicy : IsPolicy { var registry = ThrowIfNotConcurrentImplementation(); @@ -197,7 +202,8 @@ public TPolicy GetOrAdd(string key, Func policyFactory /// the policy to be added, if the key does not already exist /// The policy for the key. This will be either the existing policy for the key if the /// key is already in the registry, or the new policy if the key was not in the registry. - public TPolicy GetOrAdd(string key, TPolicy policy) where TPolicy : IsPolicy + public TPolicy GetOrAdd(string key, TPolicy policy) + where TPolicy : IsPolicy { var registry = ThrowIfNotConcurrentImplementation(); @@ -215,7 +221,8 @@ public TPolicy GetOrAdd(string key, TPolicy policy) where TPolicy : IsP /// based on the key's existing value /// The new policy for the key. This will be either be the result of addPolicyFactory (if the key was /// absent) or the result of updatePolicyFactory (if the key was present). - public TPolicy AddOrUpdate(string key, Func addPolicyFactory, Func updatePolicyFactory) where TPolicy : IsPolicy + public TPolicy AddOrUpdate(string key, Func addPolicyFactory, Func updatePolicyFactory) + where TPolicy : IsPolicy { var registry = ThrowIfNotConcurrentImplementation(); @@ -233,7 +240,8 @@ public TPolicy AddOrUpdate(string key, Func addPolicyF /// the key's existing value /// The new policy for the key. This will be either be addPolicy (if the key was /// absent) or the result of updatePolicyFactory (if the key was present). - public TPolicy AddOrUpdate(string key, TPolicy addPolicy, Func updatePolicyFactory) where TPolicy : IsPolicy + public TPolicy AddOrUpdate(string key, TPolicy addPolicy, Func updatePolicyFactory) + where TPolicy : IsPolicy { var registry = ThrowIfNotConcurrentImplementation(); diff --git a/src/Polly/Timeout/TimeoutEngine.cs b/src/Polly/Timeout/TimeoutEngine.cs index b49f6ee1f7a..150adf32c22 100644 --- a/src/Polly/Timeout/TimeoutEngine.cs +++ b/src/Polly/Timeout/TimeoutEngine.cs @@ -34,8 +34,8 @@ internal static TResult Implementation( SystemClock.CancelTokenAfter(timeoutCancellationTokenSource, timeout); actionTask = Task.Run(() => - action(context, combinedToken) // cancellation token here allows the user delegate to react to cancellation: possibly clear up; then throw an OperationCanceledException. - , combinedToken); // cancellation token here only allows Task.Run() to not begin the passed delegate at all, if cancellation occurs prior to invoking the delegate. + action(context, combinedToken), // cancellation token here allows the user delegate to react to cancellation: possibly clear up; then throw an OperationCanceledException. + combinedToken); // cancellation token here only allows Task.Run() to not begin the passed delegate at all, if cancellation occurs prior to invoking the delegate. try { actionTask.Wait(timeoutCancellationTokenSource.Token); // cancellation token here cancels the Wait() and causes it to throw, but does not cancel actionTask. We use only timeoutCancellationTokenSource.Token here, not combinedToken. If we allowed the user's cancellation token to cancel the Wait(), in this pessimistic scenario where the user delegate may not observe that cancellation, that would create a no-longer-observed task. That task could in turn later fault before completing, risking an UnobservedTaskException. diff --git a/src/Polly/Utilities/TimedLock.cs b/src/Polly/Utilities/TimedLock.cs index d42f5aa5113..9eb0ecd940b 100644 --- a/src/Polly/Utilities/TimedLock.cs +++ b/src/Polly/Utilities/TimedLock.cs @@ -84,7 +84,8 @@ private class Sentinel internal class LockTimeoutException : Exception { - public LockTimeoutException() : base("Timeout waiting for lock") + public LockTimeoutException() + : base("Timeout waiting for lock") { } } diff --git a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs index 42911a81b9f..fb73ee5b2ba 100644 --- a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerAsyncSpecs.cs @@ -313,7 +313,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) // Placing the rest of the invocations ('samplingDuration' / 2) + 1 seconds later // ensures that even if there are only two windows, then the invocations are placed in the second. // They are still placed within same timeslice. - SystemClock.UtcNow = () => time.AddSeconds(samplingDuration.Seconds / 2d + 1); + SystemClock.UtcNow = () => time.AddSeconds((samplingDuration.Seconds / 2d) + 1); await breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().ThrowAsync(); @@ -404,7 +404,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) // Placing the rest of the invocations ('samplingDuration' / 2) + 1 seconds later // ensures that even if there are only two windows, then the invocations are placed in the second. // They are still placed within same timeslice - SystemClock.UtcNow = () => time.AddSeconds(samplingDuration.Seconds / 2d + 1); + SystemClock.UtcNow = () => time.AddSeconds((samplingDuration.Seconds / 2d) + 1); var ex = await breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().ThrowAsync() @@ -491,7 +491,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) // Placing the rest of the invocations ('samplingDuration' / 2) + 1 seconds later // ensures that even if there are only two windows, then the invocations are placed in the second. // They are still placed within same timeslice - SystemClock.UtcNow = () => time.AddSeconds(samplingDuration.Seconds / 2d + 1); + SystemClock.UtcNow = () => time.AddSeconds((samplingDuration.Seconds / 2d) + 1); var ex = await breaker.Awaiting(x => x.RaiseExceptionAsync()) .Should().ThrowAsync() @@ -1318,7 +1318,7 @@ await breaker.Awaiting(x => x.RaiseExceptionAsync()) // Placing the rest of the invocations ('samplingDuration' / 2) + 1 seconds later // ensures that even if there are only two windows, then the invocations are placed in the second. // They are still placed within same timeslice - var anotherWindowDuration = samplingDuration.Seconds / 2d + 1; + var anotherWindowDuration = (samplingDuration.Seconds / 2d) + 1; SystemClock.UtcNow = () => time.AddSeconds(anotherWindowDuration); await breaker.Awaiting(x => x.RaiseExceptionAsync()) diff --git a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs index d2140938f8b..5b6fe1621e0 100644 --- a/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs +++ b/test/Polly.Specs/CircuitBreaker/AdvancedCircuitBreakerSpecs.cs @@ -313,7 +313,7 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh // Placing the rest of the invocations ('samplingDuration' / 2) + 1 seconds later // ensures that even if there are only two windows, then the invocations are placed in the second. // They are still placed within same timeslice. - SystemClock.UtcNow = () => time.AddSeconds(samplingDuration.Seconds / 2d + 1); + SystemClock.UtcNow = () => time.AddSeconds((samplingDuration.Seconds / 2d) + 1); breaker.Invoking(x => x.RaiseException()) .Should().Throw(); @@ -404,7 +404,7 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh // Placing the rest of the invocations ('samplingDuration' / 2) + 1 seconds later // ensures that even if there are only two windows, then the invocations are placed in the second. // They are still placed within same timeslice - SystemClock.UtcNow = () => time.AddSeconds(samplingDuration.Seconds / 2d + 1); + SystemClock.UtcNow = () => time.AddSeconds((samplingDuration.Seconds / 2d) + 1); breaker.Invoking(x => x.RaiseException()) .Should().Throw() @@ -491,7 +491,7 @@ public void Should_open_circuit_with_the_last_raised_exception_if_failure_thresh // Placing the rest of the invocations ('samplingDuration' / 2) + 1 seconds later // ensures that even if there are only two windows, then the invocations are placed in the second. // They are still placed within same timeslice - SystemClock.UtcNow = () => time.AddSeconds(samplingDuration.Seconds / 2d + 1); + SystemClock.UtcNow = () => time.AddSeconds((samplingDuration.Seconds / 2d) + 1); breaker.Invoking(x => x.RaiseException()) .Should().Throw() @@ -1318,8 +1318,8 @@ public void Should_halfopen_circuit_after_the_specified_duration_has_passed_with // Placing the rest of the invocations ('samplingDuration' / 2) + 1 seconds later // ensures that even if there are only two windows, then the invocations are placed in the second. // They are still placed within same timeslice - var anotherwindowDuration = samplingDuration.Seconds / 2d + 1; - SystemClock.UtcNow = () => time.AddSeconds(anotherwindowDuration); + var anotherWindowDuration = (samplingDuration.Seconds / 2d) + 1; + SystemClock.UtcNow = () => time.AddSeconds(anotherWindowDuration); breaker.Invoking(x => x.RaiseException()) .Should().Throw(); @@ -1331,7 +1331,7 @@ public void Should_halfopen_circuit_after_the_specified_duration_has_passed_with // Since the call that opened the circuit occurred in a later window, then the // break duration must be simulated as from that call. - SystemClock.UtcNow = () => time.Add(durationOfBreak).AddSeconds(anotherwindowDuration); + SystemClock.UtcNow = () => time.Add(durationOfBreak).AddSeconds(anotherWindowDuration); // duration has passed, circuit now half open breaker.CircuitState.Should().Be(CircuitState.HalfOpen); diff --git a/test/Polly.Specs/Polly.Specs.csproj b/test/Polly.Specs/Polly.Specs.csproj index 737cab0336b..b10f1158c4b 100644 --- a/test/Polly.Specs/Polly.Specs.csproj +++ b/test/Polly.Specs/Polly.Specs.csproj @@ -1,4 +1,4 @@ - + net6.0;net7.0;net8.0 @@ -9,7 +9,7 @@ [Polly]* true $(NoWarn);S103;S104;CA2000;IDE0011;SA1600;SA1204;CA2008;CA1806;CA2201; - $(NoWarn);S3878;CA1030;S3717;SA1129;SA1407;S1402;SA1649;SA1402;S4056;CA1031 + $(NoWarn);S3878;CA1030;S3717;SA1129;S1402;SA1649;SA1402;S4056;CA1031 $(NoWarn);S2184;