From 6b8cfc098168fcb0e56a9b34d2fed5d7db2ba2e9 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 13 Jan 2023 19:56:39 +1100 Subject: [PATCH] use some compound assignments --- src/Polly/Caching/AsyncCacheSyntax.cs | 4 ++-- src/Polly/Caching/AsyncCacheTResultSyntax.cs | 8 ++++---- src/Polly/Caching/CacheSyntax.cs | 4 ++-- src/Polly/Caching/CacheTResultSyntax.cs | 8 ++++---- src/Polly/CircuitBreaker/AdvancedCircuitController.cs | 2 +- src/Polly/CircuitBreaker/RollingHealthMetrics.cs | 2 +- src/Polly/ExceptionPredicates.cs | 2 +- src/Polly/PolicyBuilder.OrSyntax.cs | 4 ++-- src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs | 4 ++-- src/Polly/ResultPredicates.cs | 2 +- 10 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Polly/Caching/AsyncCacheSyntax.cs b/src/Polly/Caching/AsyncCacheSyntax.cs index 2e5d4f24fa0..c8a9583e05b 100644 --- a/src/Polly/Caching/AsyncCacheSyntax.cs +++ b/src/Polly/Caching/AsyncCacheSyntax.cs @@ -68,7 +68,7 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); @@ -110,7 +110,7 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); diff --git a/src/Polly/Caching/AsyncCacheTResultSyntax.cs b/src/Polly/Caching/AsyncCacheTResultSyntax.cs index 1f76748397d..2aed04a7b8a 100644 --- a/src/Polly/Caching/AsyncCacheTResultSyntax.cs +++ b/src/Polly/Caching/AsyncCacheTResultSyntax.cs @@ -417,7 +417,7 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider< /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; @@ -440,7 +440,7 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider< /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; @@ -479,7 +479,7 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider< /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; @@ -502,7 +502,7 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider< /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; diff --git a/src/Polly/Caching/CacheSyntax.cs b/src/Polly/Caching/CacheSyntax.cs index abc7a537b15..dd51e0995d0 100644 --- a/src/Polly/Caching/CacheSyntax.cs +++ b/src/Polly/Caching/CacheSyntax.cs @@ -68,7 +68,7 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy t if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); @@ -110,7 +110,7 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy t if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); diff --git a/src/Polly/Caching/CacheTResultSyntax.cs b/src/Polly/Caching/CacheTResultSyntax.cs index 4b192a8529d..28330b2bdcd 100644 --- a/src/Polly/Caching/CacheTResultSyntax.cs +++ b/src/Polly/Caching/CacheTResultSyntax.cs @@ -417,7 +417,7 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; @@ -441,7 +441,7 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; @@ -481,7 +481,7 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; @@ -505,7 +505,7 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - onCacheError = onCacheError ?? ((_, _, _) => { }); + onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; diff --git a/src/Polly/CircuitBreaker/AdvancedCircuitController.cs b/src/Polly/CircuitBreaker/AdvancedCircuitController.cs index 9a79dda5be5..2e195c40956 100644 --- a/src/Polly/CircuitBreaker/AdvancedCircuitController.cs +++ b/src/Polly/CircuitBreaker/AdvancedCircuitController.cs @@ -82,7 +82,7 @@ public override void OnActionFailure(DelegateResult outcome, Context co var healthCount = _metrics.GetHealthCount_NeedsLock(); int throughput = healthCount.Total; - if (throughput >= _minimumThroughput && ((double)healthCount.Failures) / throughput >= _failureThreshold) + if (throughput >= _minimumThroughput && (double)healthCount.Failures / throughput >= _failureThreshold) { Break_NeedsLock(context); } diff --git a/src/Polly/CircuitBreaker/RollingHealthMetrics.cs b/src/Polly/CircuitBreaker/RollingHealthMetrics.cs index 12f993ad32e..79400a5e914 100644 --- a/src/Polly/CircuitBreaker/RollingHealthMetrics.cs +++ b/src/Polly/CircuitBreaker/RollingHealthMetrics.cs @@ -65,7 +65,7 @@ private void ActualiseCurrentMetric_NeedsLock() _windows.Enqueue(_currentWindow); } - while (_windows.Count > 0 && (now - _windows.Peek().StartedAt >= _samplingDuration)) + while (_windows.Count > 0 && now - _windows.Peek().StartedAt >= _samplingDuration) _windows.Dequeue(); } } diff --git a/src/Polly/ExceptionPredicates.cs b/src/Polly/ExceptionPredicates.cs index dcc2aceb03b..65375e57925 100644 --- a/src/Polly/ExceptionPredicates.cs +++ b/src/Polly/ExceptionPredicates.cs @@ -9,7 +9,7 @@ public class ExceptionPredicates internal void Add(ExceptionPredicate predicate) { - _predicates = _predicates ?? new List(); // The ?? pattern here is sufficient; only a deliberately contrived example would lead to the same PolicyBuilder instance being used in a multi-threaded way to define policies simultaneously on multiple threads. + _predicates ??= new List(); // The ?? pattern here is sufficient; only a deliberately contrived example would lead to the same PolicyBuilder instance being used in a multi-threaded way to define policies simultaneously on multiple threads. _predicates.Add(predicate); } diff --git a/src/Polly/PolicyBuilder.OrSyntax.cs b/src/Polly/PolicyBuilder.OrSyntax.cs index 2e077b24305..655a7658654 100644 --- a/src/Polly/PolicyBuilder.OrSyntax.cs +++ b/src/Polly/PolicyBuilder.OrSyntax.cs @@ -34,7 +34,7 @@ public PolicyBuilder Or(Func exceptionPredicate) w /// The PolicyBuilder instance, for fluent chaining. public PolicyBuilder OrInner() where TException : Exception { - ExceptionPredicates.Add((HandleInner(ex => ex is TException))); + ExceptionPredicates.Add(HandleInner(ex => ex is TException)); return this; } @@ -160,7 +160,7 @@ public PolicyBuilder Or(Func exceptionPre /// The PolicyBuilder instance, for fluent chaining. public PolicyBuilder OrInner() where TException : Exception { - ExceptionPredicates.Add((PolicyBuilder.HandleInner(ex => ex is TException))); + ExceptionPredicates.Add(PolicyBuilder.HandleInner(ex => ex is TException)); return this; } diff --git a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs index b67a3f2aba1..8595af4ec4b 100644 --- a/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs +++ b/src/Polly/RateLimit/LockFreeTokenBucketRateLimiter.cs @@ -69,13 +69,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/ResultPredicates.cs b/src/Polly/ResultPredicates.cs index 52abd86980b..c386c9b7c52 100644 --- a/src/Polly/ResultPredicates.cs +++ b/src/Polly/ResultPredicates.cs @@ -9,7 +9,7 @@ public class ResultPredicates internal void Add(ResultPredicate predicate) { - _predicates = _predicates ?? new List>(); // The ?? pattern here is sufficient; only a deliberately contrived example would lead to the same PolicyBuilder instance being used in a multi-threaded way to define policies simultaneously on multiple threads. + _predicates ??= new List>(); // The ?? pattern here is sufficient; only a deliberately contrived example would lead to the same PolicyBuilder instance being used in a multi-threaded way to define policies simultaneously on multiple threads. _predicates.Add(predicate); }