From 9893b6c20b512a9e698260efbca606733abfdded Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Thu, 22 Aug 2024 10:52:22 +0200 Subject: [PATCH 1/2] Cleaning and package upgrades. --- .../AspNetCoreRateLimit.Redis.csproj | 4 +-- .../AspNetCoreRateLimit.csproj | 12 +++++---- .../Core/ClientRateLimitProcessor.cs | 25 ++++++------------ src/AspNetCoreRateLimit/Core/Extensions.cs | 4 +-- .../Core/IpRateLimitProcessor.cs | 26 ++++++------------- .../AsyncKeyLockProcessingStrategy.cs | 13 +++------- .../ProcessingStrategy.cs | 9 ++----- .../Core/RateLimitProcessor.cs | 12 +++------ .../ClientCounterKeyBuilder.cs | 9 ++----- .../CounterKeyBuilders/IpCounterKeyBuilder.cs | 9 ++----- .../Middleware/ClientRateLimitMiddleware.cs | 20 +++++--------- .../Middleware/IpRateLimitMiddleware.cs | 22 ++++++---------- .../Middleware/RateLimitConfiguration.cs | 20 +++++--------- .../Models/IpRateLimitPolicies.cs | 2 +- .../Models/RateLimitPolicy.cs | 2 +- .../Properties/AssemblyInfo.cs | 1 - .../ClientHeaderResolveContributor.cs | 8 ++---- .../IpConnectionResolveContributor.cs | 6 ----- .../Resolvers/IpHeaderResolveContributor.cs | 11 +++----- .../DistributedCacheClientPolicyStore.cs | 18 +++++-------- .../DistributedCacheIpPolicyStore.cs | 18 +++++-------- .../DistributedCacheRateLimitCounterStore.cs | 5 +--- .../DistributedCacheRateLimitStore.cs | 9 ++----- .../MemoryCacheClientPolicyStore.cs | 18 +++++-------- .../MemoryCache/MemoryCacheIpPolicyStore.cs | 18 +++++-------- .../MemoryCacheRateLimitCounterStore.cs | 5 +--- .../MemoryCache/MemoryCacheRateLimitStore.cs | 9 ++----- .../Controllers/IpRateLimitController.cs | 2 +- .../AspNetCoreRateLimit.Tests.csproj | 6 ++--- 29 files changed, 102 insertions(+), 221 deletions(-) diff --git a/src/AspNetCoreRateLimit.Redis/AspNetCoreRateLimit.Redis.csproj b/src/AspNetCoreRateLimit.Redis/AspNetCoreRateLimit.Redis.csproj index 3bc62cbe..fe106e6a 100644 --- a/src/AspNetCoreRateLimit.Redis/AspNetCoreRateLimit.Redis.csproj +++ b/src/AspNetCoreRateLimit.Redis/AspNetCoreRateLimit.Redis.csproj @@ -21,7 +21,7 @@ - + @@ -31,7 +31,7 @@ - + diff --git a/src/AspNetCoreRateLimit/AspNetCoreRateLimit.csproj b/src/AspNetCoreRateLimit/AspNetCoreRateLimit.csproj index 937335d3..4c4340e5 100644 --- a/src/AspNetCoreRateLimit/AspNetCoreRateLimit.csproj +++ b/src/AspNetCoreRateLimit/AspNetCoreRateLimit.csproj @@ -18,13 +18,15 @@ true true embedded + IDE0130 + latest - - - - + + + + @@ -33,7 +35,7 @@ - + diff --git a/src/AspNetCoreRateLimit/Core/ClientRateLimitProcessor.cs b/src/AspNetCoreRateLimit/Core/ClientRateLimitProcessor.cs index 2c8fef3b..669c2625 100644 --- a/src/AspNetCoreRateLimit/Core/ClientRateLimitProcessor.cs +++ b/src/AspNetCoreRateLimit/Core/ClientRateLimitProcessor.cs @@ -4,24 +4,15 @@ namespace AspNetCoreRateLimit { - public class ClientRateLimitProcessor : RateLimitProcessor, IRateLimitProcessor + public class ClientRateLimitProcessor( + ClientRateLimitOptions options, + IClientPolicyStore policyStore, + IProcessingStrategy processingStrategy) : RateLimitProcessor(options), IRateLimitProcessor { - private readonly ClientRateLimitOptions _options; - private readonly IProcessingStrategy _processingStrategy; - private readonly IRateLimitStore _policyStore; - private readonly ICounterKeyBuilder _counterKeyBuilder; - - public ClientRateLimitProcessor( - ClientRateLimitOptions options, - IClientPolicyStore policyStore, - IProcessingStrategy processingStrategy) - : base(options) - { - _options = options; - _policyStore = policyStore; - _counterKeyBuilder = new ClientCounterKeyBuilder(options); - _processingStrategy = processingStrategy; - } + private readonly ClientRateLimitOptions _options = options; + private readonly IProcessingStrategy _processingStrategy = processingStrategy; + private readonly IRateLimitStore _policyStore = policyStore; + private readonly ICounterKeyBuilder _counterKeyBuilder = new ClientCounterKeyBuilder(options); public async Task> GetMatchingRulesAsync(ClientRequestIdentity identity, CancellationToken cancellationToken = default) { diff --git a/src/AspNetCoreRateLimit/Core/Extensions.cs b/src/AspNetCoreRateLimit/Core/Extensions.cs index 2b2be06b..dff29a17 100644 --- a/src/AspNetCoreRateLimit/Core/Extensions.cs +++ b/src/AspNetCoreRateLimit/Core/Extensions.cs @@ -27,7 +27,7 @@ public static bool IsRegexMatch(this string source, string value) } // if the regex is e.g. /api/values/ the path should be an exact match // if all paths below this should be included the regex should be /api/values/* - if (value[value.Length - 1] != '$') + if (value[^1] != '$') { value += '$'; } @@ -49,7 +49,7 @@ public static string RetryAfterFrom(this DateTime timestamp, RateLimitRule rule) public static TimeSpan ToTimeSpan(this string timeSpan) { var l = timeSpan.Length - 1; - var value = timeSpan.Substring(0, l); + var value = timeSpan[..l]; var type = timeSpan.Substring(l, 1); return type switch diff --git a/src/AspNetCoreRateLimit/Core/IpRateLimitProcessor.cs b/src/AspNetCoreRateLimit/Core/IpRateLimitProcessor.cs index 3cf1a0ec..d327a6c8 100644 --- a/src/AspNetCoreRateLimit/Core/IpRateLimitProcessor.cs +++ b/src/AspNetCoreRateLimit/Core/IpRateLimitProcessor.cs @@ -5,25 +5,15 @@ namespace AspNetCoreRateLimit { - public class IpRateLimitProcessor : RateLimitProcessor, IRateLimitProcessor + public class IpRateLimitProcessor( + IpRateLimitOptions options, + IIpPolicyStore policyStore, + IProcessingStrategy processingStrategy) : RateLimitProcessor(options), IRateLimitProcessor { - private readonly IpRateLimitOptions _options; - private readonly IRateLimitStore _policyStore; - private readonly IProcessingStrategy _processingStrategy; - private readonly ICounterKeyBuilder _counterKeyBuilder; - - public IpRateLimitProcessor( - IpRateLimitOptions options, - IIpPolicyStore policyStore, - IProcessingStrategy processingStrategy) - : base(options) - { - _options = options; - _policyStore = policyStore; - _counterKeyBuilder = new IpCounterKeyBuilder(options); - _processingStrategy = processingStrategy; - } - + private readonly IpRateLimitOptions _options = options; + private readonly IRateLimitStore _policyStore = policyStore; + private readonly IProcessingStrategy _processingStrategy = processingStrategy; + private readonly ICounterKeyBuilder _counterKeyBuilder = new IpCounterKeyBuilder(options); public async Task> GetMatchingRulesAsync(ClientRequestIdentity identity, CancellationToken cancellationToken = default) { diff --git a/src/AspNetCoreRateLimit/Core/ProcessingStrategies/AsyncKeyLockProcessingStrategy.cs b/src/AspNetCoreRateLimit/Core/ProcessingStrategies/AsyncKeyLockProcessingStrategy.cs index 05367a7e..928c9377 100644 --- a/src/AspNetCoreRateLimit/Core/ProcessingStrategies/AsyncKeyLockProcessingStrategy.cs +++ b/src/AspNetCoreRateLimit/Core/ProcessingStrategies/AsyncKeyLockProcessingStrategy.cs @@ -5,17 +5,10 @@ namespace AspNetCoreRateLimit { - public class AsyncKeyLockProcessingStrategy : ProcessingStrategy + public class AsyncKeyLockProcessingStrategy(IRateLimitCounterStore counterStore, IRateLimitConfiguration config) : ProcessingStrategy(config) { - private readonly IRateLimitCounterStore _counterStore; - private readonly IRateLimitConfiguration _config; - - public AsyncKeyLockProcessingStrategy(IRateLimitCounterStore counterStore, IRateLimitConfiguration config) - : base(config) - { - _counterStore = counterStore; - _config = config; - } + private readonly IRateLimitCounterStore _counterStore = counterStore; + private readonly IRateLimitConfiguration _config = config; /// The key-lock used for limiting requests. private static readonly AsyncKeyedLocker AsyncLock = new(o => diff --git a/src/AspNetCoreRateLimit/Core/ProcessingStrategies/ProcessingStrategy.cs b/src/AspNetCoreRateLimit/Core/ProcessingStrategies/ProcessingStrategy.cs index 41578fe1..bfe067d9 100644 --- a/src/AspNetCoreRateLimit/Core/ProcessingStrategies/ProcessingStrategy.cs +++ b/src/AspNetCoreRateLimit/Core/ProcessingStrategies/ProcessingStrategy.cs @@ -6,14 +6,9 @@ namespace AspNetCoreRateLimit { - public abstract class ProcessingStrategy : IProcessingStrategy + public abstract class ProcessingStrategy(IRateLimitConfiguration config) : IProcessingStrategy { - private readonly IRateLimitConfiguration _config; - - protected ProcessingStrategy(IRateLimitConfiguration config) - { - _config = config; - } + private readonly IRateLimitConfiguration _config = config; public abstract Task ProcessRequestAsync(ClientRequestIdentity requestIdentity, RateLimitRule rule, ICounterKeyBuilder counterKeyBuilder, RateLimitOptions rateLimitOptions, CancellationToken cancellationToken = default); diff --git a/src/AspNetCoreRateLimit/Core/RateLimitProcessor.cs b/src/AspNetCoreRateLimit/Core/RateLimitProcessor.cs index bb3945b7..410d6175 100644 --- a/src/AspNetCoreRateLimit/Core/RateLimitProcessor.cs +++ b/src/AspNetCoreRateLimit/Core/RateLimitProcessor.cs @@ -6,15 +6,9 @@ namespace AspNetCoreRateLimit { - public abstract class RateLimitProcessor + public abstract class RateLimitProcessor(RateLimitOptions options) { - private readonly RateLimitOptions _options; - - protected RateLimitProcessor(RateLimitOptions options) - { - _options = options; - } - + private readonly RateLimitOptions _options = options; public virtual bool IsWhitelisted(ClientRequestIdentity requestIdentity) { @@ -143,7 +137,7 @@ protected virtual List GetMatchingRules(ClientRequestIdentity ide } } - limits = limits.OrderBy(l => l.PeriodTimespan).ToList(); + limits = [.. limits.OrderBy(l => l.PeriodTimespan)]; if (_options.StackBlockedRequests) { diff --git a/src/AspNetCoreRateLimit/CounterKeyBuilders/ClientCounterKeyBuilder.cs b/src/AspNetCoreRateLimit/CounterKeyBuilders/ClientCounterKeyBuilder.cs index 32aca93f..78dd78e5 100644 --- a/src/AspNetCoreRateLimit/CounterKeyBuilders/ClientCounterKeyBuilder.cs +++ b/src/AspNetCoreRateLimit/CounterKeyBuilders/ClientCounterKeyBuilder.cs @@ -1,13 +1,8 @@ namespace AspNetCoreRateLimit { - public class ClientCounterKeyBuilder : ICounterKeyBuilder + public class ClientCounterKeyBuilder(ClientRateLimitOptions options) : ICounterKeyBuilder { - private readonly ClientRateLimitOptions _options; - - public ClientCounterKeyBuilder(ClientRateLimitOptions options) - { - _options = options; - } + private readonly ClientRateLimitOptions _options = options; public string Build(ClientRequestIdentity requestIdentity, RateLimitRule rule) { diff --git a/src/AspNetCoreRateLimit/CounterKeyBuilders/IpCounterKeyBuilder.cs b/src/AspNetCoreRateLimit/CounterKeyBuilders/IpCounterKeyBuilder.cs index 6c610d06..f949313d 100644 --- a/src/AspNetCoreRateLimit/CounterKeyBuilders/IpCounterKeyBuilder.cs +++ b/src/AspNetCoreRateLimit/CounterKeyBuilders/IpCounterKeyBuilder.cs @@ -1,13 +1,8 @@ namespace AspNetCoreRateLimit { - public class IpCounterKeyBuilder : ICounterKeyBuilder + public class IpCounterKeyBuilder(IpRateLimitOptions options) : ICounterKeyBuilder { - private readonly IpRateLimitOptions _options; - - public IpCounterKeyBuilder(IpRateLimitOptions options) - { - _options = options; - } + private readonly IpRateLimitOptions _options = options; public string Build(ClientRequestIdentity requestIdentity, RateLimitRule rule) { diff --git a/src/AspNetCoreRateLimit/Middleware/ClientRateLimitMiddleware.cs b/src/AspNetCoreRateLimit/Middleware/ClientRateLimitMiddleware.cs index 5cb06e09..5cccdf62 100644 --- a/src/AspNetCoreRateLimit/Middleware/ClientRateLimitMiddleware.cs +++ b/src/AspNetCoreRateLimit/Middleware/ClientRateLimitMiddleware.cs @@ -4,20 +4,14 @@ namespace AspNetCoreRateLimit { - public class ClientRateLimitMiddleware : RateLimitMiddleware + public class ClientRateLimitMiddleware(RequestDelegate next, + IProcessingStrategy processingStrategy, + IOptions options, + IClientPolicyStore policyStore, + IRateLimitConfiguration config, + ILogger logger) : RateLimitMiddleware(next, options?.Value, new ClientRateLimitProcessor(options?.Value, policyStore, processingStrategy), config) { - private readonly ILogger _logger; - - public ClientRateLimitMiddleware(RequestDelegate next, - IProcessingStrategy processingStrategy, - IOptions options, - IClientPolicyStore policyStore, - IRateLimitConfiguration config, - ILogger logger) - : base(next, options?.Value, new ClientRateLimitProcessor(options?.Value, policyStore, processingStrategy), config) - { - _logger = logger; - } + private readonly ILogger _logger = logger; protected override void LogBlockedRequest(HttpContext httpContext, ClientRequestIdentity identity, RateLimitCounter counter, RateLimitRule rule) { diff --git a/src/AspNetCoreRateLimit/Middleware/IpRateLimitMiddleware.cs b/src/AspNetCoreRateLimit/Middleware/IpRateLimitMiddleware.cs index 71834d6c..4faa34bf 100644 --- a/src/AspNetCoreRateLimit/Middleware/IpRateLimitMiddleware.cs +++ b/src/AspNetCoreRateLimit/Middleware/IpRateLimitMiddleware.cs @@ -4,21 +4,15 @@ namespace AspNetCoreRateLimit { - public class IpRateLimitMiddleware : RateLimitMiddleware + public class IpRateLimitMiddleware(RequestDelegate next, + IProcessingStrategy processingStrategy, + IOptions options, + IIpPolicyStore policyStore, + IRateLimitConfiguration config, + ILogger logger + ) : RateLimitMiddleware(next, options?.Value, new IpRateLimitProcessor(options?.Value, policyStore, processingStrategy), config) { - private readonly ILogger _logger; - - public IpRateLimitMiddleware(RequestDelegate next, - IProcessingStrategy processingStrategy, - IOptions options, - IIpPolicyStore policyStore, - IRateLimitConfiguration config, - ILogger logger - ) - : base(next, options?.Value, new IpRateLimitProcessor(options?.Value, policyStore, processingStrategy), config) - { - _logger = logger; - } + private readonly ILogger _logger = logger; protected override void LogBlockedRequest(HttpContext httpContext, ClientRequestIdentity identity, RateLimitCounter counter, RateLimitRule rule) { diff --git a/src/AspNetCoreRateLimit/Middleware/RateLimitConfiguration.cs b/src/AspNetCoreRateLimit/Middleware/RateLimitConfiguration.cs index 6a7fe241..07487220 100644 --- a/src/AspNetCoreRateLimit/Middleware/RateLimitConfiguration.cs +++ b/src/AspNetCoreRateLimit/Middleware/RateLimitConfiguration.cs @@ -4,25 +4,19 @@ namespace AspNetCoreRateLimit { - public class RateLimitConfiguration : IRateLimitConfiguration + public class RateLimitConfiguration( + IOptions ipOptions, + IOptions clientOptions) : IRateLimitConfiguration { - public IList ClientResolvers { get; } = new List(); - public IList IpResolvers { get; } = new List(); + public IList ClientResolvers { get; } = []; + public IList IpResolvers { get; } = []; public virtual ICounterKeyBuilder EndpointCounterKeyBuilder { get; } = new PathCounterKeyBuilder(); public virtual Func RateIncrementer { get; } = () => 1; - public RateLimitConfiguration( - IOptions ipOptions, - IOptions clientOptions) - { - IpRateLimitOptions = ipOptions?.Value; - ClientRateLimitOptions = clientOptions?.Value; - } - - protected readonly IpRateLimitOptions IpRateLimitOptions; - protected readonly ClientRateLimitOptions ClientRateLimitOptions; + protected readonly IpRateLimitOptions IpRateLimitOptions = ipOptions?.Value; + protected readonly ClientRateLimitOptions ClientRateLimitOptions = clientOptions?.Value; public virtual void RegisterResolvers() { diff --git a/src/AspNetCoreRateLimit/Models/IpRateLimitPolicies.cs b/src/AspNetCoreRateLimit/Models/IpRateLimitPolicies.cs index 20ad6713..3c787137 100644 --- a/src/AspNetCoreRateLimit/Models/IpRateLimitPolicies.cs +++ b/src/AspNetCoreRateLimit/Models/IpRateLimitPolicies.cs @@ -4,6 +4,6 @@ namespace AspNetCoreRateLimit { public class IpRateLimitPolicies { - public List IpRules { get; set; } = new List(); + public List IpRules { get; set; } = []; } } \ No newline at end of file diff --git a/src/AspNetCoreRateLimit/Models/RateLimitPolicy.cs b/src/AspNetCoreRateLimit/Models/RateLimitPolicy.cs index 7403efdc..6621dd3f 100644 --- a/src/AspNetCoreRateLimit/Models/RateLimitPolicy.cs +++ b/src/AspNetCoreRateLimit/Models/RateLimitPolicy.cs @@ -4,6 +4,6 @@ namespace AspNetCoreRateLimit { public class RateLimitPolicy { - public List Rules { get; set; } = new List(); + public List Rules { get; set; } = []; } } diff --git a/src/AspNetCoreRateLimit/Properties/AssemblyInfo.cs b/src/AspNetCoreRateLimit/Properties/AssemblyInfo.cs index fcaeb398..ba3ad450 100644 --- a/src/AspNetCoreRateLimit/Properties/AssemblyInfo.cs +++ b/src/AspNetCoreRateLimit/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following diff --git a/src/AspNetCoreRateLimit/Resolvers/ClientHeaderResolveContributor.cs b/src/AspNetCoreRateLimit/Resolvers/ClientHeaderResolveContributor.cs index 479f5627..0626e0a2 100644 --- a/src/AspNetCoreRateLimit/Resolvers/ClientHeaderResolveContributor.cs +++ b/src/AspNetCoreRateLimit/Resolvers/ClientHeaderResolveContributor.cs @@ -4,14 +4,10 @@ namespace AspNetCoreRateLimit { - public class ClientHeaderResolveContributor : IClientResolveContributor + public class ClientHeaderResolveContributor(string headerName) : IClientResolveContributor { - private readonly string _headerName; + private readonly string _headerName = headerName; - public ClientHeaderResolveContributor(string headerName) - { - _headerName = headerName; - } public Task ResolveClientAsync(HttpContext httpContext) { string clientId = null; diff --git a/src/AspNetCoreRateLimit/Resolvers/IpConnectionResolveContributor.cs b/src/AspNetCoreRateLimit/Resolvers/IpConnectionResolveContributor.cs index 2fa25820..b3249ce9 100644 --- a/src/AspNetCoreRateLimit/Resolvers/IpConnectionResolveContributor.cs +++ b/src/AspNetCoreRateLimit/Resolvers/IpConnectionResolveContributor.cs @@ -4,12 +4,6 @@ namespace AspNetCoreRateLimit { public class IpConnectionResolveContributor : IIpResolveContributor { - - public IpConnectionResolveContributor() - { - - } - public string ResolveIp(HttpContext httpContext) { return httpContext.Connection.RemoteIpAddress?.ToString(); diff --git a/src/AspNetCoreRateLimit/Resolvers/IpHeaderResolveContributor.cs b/src/AspNetCoreRateLimit/Resolvers/IpHeaderResolveContributor.cs index 72842fcb..a599fea7 100644 --- a/src/AspNetCoreRateLimit/Resolvers/IpHeaderResolveContributor.cs +++ b/src/AspNetCoreRateLimit/Resolvers/IpHeaderResolveContributor.cs @@ -4,15 +4,10 @@ namespace AspNetCoreRateLimit { - public class IpHeaderResolveContributor : IIpResolveContributor + public class IpHeaderResolveContributor( + string headerName) : IIpResolveContributor { - private readonly string _headerName; - - public IpHeaderResolveContributor( - string headerName) - { - _headerName = headerName; - } + private readonly string _headerName = headerName; public string ResolveIp(HttpContext httpContext) { diff --git a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheClientPolicyStore.cs b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheClientPolicyStore.cs index f8ff4790..bfede6f2 100644 --- a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheClientPolicyStore.cs +++ b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheClientPolicyStore.cs @@ -4,19 +4,13 @@ namespace AspNetCoreRateLimit { - public class DistributedCacheClientPolicyStore : DistributedCacheRateLimitStore, IClientPolicyStore + public class DistributedCacheClientPolicyStore( + IDistributedCache cache, + IOptions options = null, + IOptions policies = null) : DistributedCacheRateLimitStore(cache), IClientPolicyStore { - private readonly ClientRateLimitOptions _options; - private readonly ClientRateLimitPolicies _policies; - - public DistributedCacheClientPolicyStore( - IDistributedCache cache, - IOptions options = null, - IOptions policies = null) : base(cache) - { - _options = options?.Value; - _policies = policies?.Value; - } + private readonly ClientRateLimitOptions _options = options?.Value; + private readonly ClientRateLimitPolicies _policies = policies?.Value; public async Task SeedAsync() { diff --git a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheIpPolicyStore.cs b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheIpPolicyStore.cs index 565a7d72..32c734f9 100644 --- a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheIpPolicyStore.cs +++ b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheIpPolicyStore.cs @@ -4,19 +4,13 @@ namespace AspNetCoreRateLimit { - public class DistributedCacheIpPolicyStore : DistributedCacheRateLimitStore, IIpPolicyStore + public class DistributedCacheIpPolicyStore( + IDistributedCache cache, + IOptions options = null, + IOptions policies = null) : DistributedCacheRateLimitStore(cache), IIpPolicyStore { - private readonly IpRateLimitOptions _options; - private readonly IpRateLimitPolicies _policies; - - public DistributedCacheIpPolicyStore( - IDistributedCache cache, - IOptions options = null, - IOptions policies = null) : base(cache) - { - _options = options?.Value; - _policies = policies?.Value; - } + private readonly IpRateLimitOptions _options = options?.Value; + private readonly IpRateLimitPolicies _policies = policies?.Value; public async Task SeedAsync() { diff --git a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitCounterStore.cs b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitCounterStore.cs index abe82410..d11368c1 100644 --- a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitCounterStore.cs +++ b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitCounterStore.cs @@ -2,10 +2,7 @@ namespace AspNetCoreRateLimit { - public class DistributedCacheRateLimitCounterStore : DistributedCacheRateLimitStore, IRateLimitCounterStore + public class DistributedCacheRateLimitCounterStore(IDistributedCache cache) : DistributedCacheRateLimitStore(cache), IRateLimitCounterStore { - public DistributedCacheRateLimitCounterStore(IDistributedCache cache) : base(cache) - { - } } } \ No newline at end of file diff --git a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitStore.cs b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitStore.cs index c2f5d005..9d5f3e99 100644 --- a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitStore.cs +++ b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitStore.cs @@ -6,14 +6,9 @@ namespace AspNetCoreRateLimit { - public class DistributedCacheRateLimitStore : IRateLimitStore + public class DistributedCacheRateLimitStore(IDistributedCache cache) : IRateLimitStore { - private readonly IDistributedCache _cache; - - public DistributedCacheRateLimitStore(IDistributedCache cache) - { - _cache = cache; - } + private readonly IDistributedCache _cache = cache; public Task SetAsync(string id, T entry, TimeSpan? expirationTime = null, CancellationToken cancellationToken = default) { diff --git a/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheClientPolicyStore.cs b/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheClientPolicyStore.cs index 37f87284..14d09c4d 100644 --- a/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheClientPolicyStore.cs +++ b/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheClientPolicyStore.cs @@ -4,19 +4,13 @@ namespace AspNetCoreRateLimit { - public class MemoryCacheClientPolicyStore : MemoryCacheRateLimitStore, IClientPolicyStore + public class MemoryCacheClientPolicyStore( + IMemoryCache cache, + IOptions options = null, + IOptions policies = null) : MemoryCacheRateLimitStore(cache), IClientPolicyStore { - private readonly ClientRateLimitOptions _options; - private readonly ClientRateLimitPolicies _policies; - - public MemoryCacheClientPolicyStore( - IMemoryCache cache, - IOptions options = null, - IOptions policies = null) : base(cache) - { - _options = options?.Value; - _policies = policies?.Value; - } + private readonly ClientRateLimitOptions _options = options?.Value; + private readonly ClientRateLimitPolicies _policies = policies?.Value; public async Task SeedAsync() { diff --git a/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheIpPolicyStore.cs b/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheIpPolicyStore.cs index 69972f62..67711fdf 100644 --- a/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheIpPolicyStore.cs +++ b/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheIpPolicyStore.cs @@ -4,19 +4,13 @@ namespace AspNetCoreRateLimit { - public class MemoryCacheIpPolicyStore : MemoryCacheRateLimitStore, IIpPolicyStore + public class MemoryCacheIpPolicyStore( + IMemoryCache cache, + IOptions options = null, + IOptions policies = null) : MemoryCacheRateLimitStore(cache), IIpPolicyStore { - private readonly IpRateLimitOptions _options; - private readonly IpRateLimitPolicies _policies; - - public MemoryCacheIpPolicyStore( - IMemoryCache cache, - IOptions options = null, - IOptions policies = null) : base(cache) - { - _options = options?.Value; - _policies = policies?.Value; - } + private readonly IpRateLimitOptions _options = options?.Value; + private readonly IpRateLimitPolicies _policies = policies?.Value; public async Task SeedAsync() { diff --git a/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheRateLimitCounterStore.cs b/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheRateLimitCounterStore.cs index 8f8faa63..5633b41a 100644 --- a/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheRateLimitCounterStore.cs +++ b/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheRateLimitCounterStore.cs @@ -2,10 +2,7 @@ namespace AspNetCoreRateLimit { - public class MemoryCacheRateLimitCounterStore : MemoryCacheRateLimitStore, IRateLimitCounterStore + public class MemoryCacheRateLimitCounterStore(IMemoryCache cache) : MemoryCacheRateLimitStore(cache), IRateLimitCounterStore { - public MemoryCacheRateLimitCounterStore(IMemoryCache cache) : base(cache) - { - } } } \ No newline at end of file diff --git a/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheRateLimitStore.cs b/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheRateLimitStore.cs index 46e3ed5c..db3b8031 100644 --- a/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheRateLimitStore.cs +++ b/src/AspNetCoreRateLimit/Store/MemoryCache/MemoryCacheRateLimitStore.cs @@ -5,14 +5,9 @@ namespace AspNetCoreRateLimit { - public class MemoryCacheRateLimitStore : IRateLimitStore + public class MemoryCacheRateLimitStore(IMemoryCache cache) : IRateLimitStore { - private readonly IMemoryCache _cache; - - public MemoryCacheRateLimitStore(IMemoryCache cache) - { - _cache = cache; - } + private readonly IMemoryCache _cache = cache; public Task ExistsAsync(string id, CancellationToken cancellationToken = default) { diff --git a/test/AspNetCoreRateLimit.Demo/Controllers/IpRateLimitController.cs b/test/AspNetCoreRateLimit.Demo/Controllers/IpRateLimitController.cs index 36f533c6..02f81844 100644 --- a/test/AspNetCoreRateLimit.Demo/Controllers/IpRateLimitController.cs +++ b/test/AspNetCoreRateLimit.Demo/Controllers/IpRateLimitController.cs @@ -32,7 +32,7 @@ public async Task Post() { Ip = "8.8.4.4", Rules = new List(new RateLimitRule[] { - new RateLimitRule { + new() { Endpoint = "*:/api/testupdate", Limit = 100, Period = "1d" } diff --git a/test/AspNetCoreRateLimit.Tests/AspNetCoreRateLimit.Tests.csproj b/test/AspNetCoreRateLimit.Tests/AspNetCoreRateLimit.Tests.csproj index 2b34ee2d..d3a76e9d 100644 --- a/test/AspNetCoreRateLimit.Tests/AspNetCoreRateLimit.Tests.csproj +++ b/test/AspNetCoreRateLimit.Tests/AspNetCoreRateLimit.Tests.csproj @@ -14,9 +14,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive From 401cbc41a8c31c30ca818ff15c079f96ac4fe031 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 11 Nov 2024 10:45:25 +0100 Subject: [PATCH 2/2] Update AspNetCoreRateLimit.csproj --- src/AspNetCoreRateLimit/AspNetCoreRateLimit.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AspNetCoreRateLimit/AspNetCoreRateLimit.csproj b/src/AspNetCoreRateLimit/AspNetCoreRateLimit.csproj index 4c4340e5..327a7d6f 100644 --- a/src/AspNetCoreRateLimit/AspNetCoreRateLimit.csproj +++ b/src/AspNetCoreRateLimit/AspNetCoreRateLimit.csproj @@ -23,7 +23,7 @@ - +