diff --git a/src/AspNetCoreRateLimit.Redis/RedisProcessingStrategy.cs b/src/AspNetCoreRateLimit.Redis/RedisProcessingStrategy.cs index 8481be42..cc3237bb 100644 --- a/src/AspNetCoreRateLimit.Redis/RedisProcessingStrategy.cs +++ b/src/AspNetCoreRateLimit.Redis/RedisProcessingStrategy.cs @@ -36,12 +36,24 @@ public async Task IncrementAsync(string counterId, TimeSpan in var intervalStart = new DateTime(numberOfIntervals * interval.Ticks, DateTimeKind.Utc); _logger.LogDebug("Calling Lua script. {counterId}, {timeout}, {delta}", counterId, interval.TotalSeconds, 1D); - var count = await _connectionMultiplexer.GetDatabase().ScriptEvaluateAsync(_atomicIncrement, new { key = new RedisKey(counterId), timeout = interval.TotalSeconds, delta = RateIncrementer?.Invoke() ?? 1D }); - return new RateLimitCounter + try { - Count = (double)count, - Timestamp = intervalStart - }; + var count = await _connectionMultiplexer.GetDatabase().ScriptEvaluateAsync(_atomicIncrement, new { key = new RedisKey(counterId), timeout = interval.TotalSeconds, delta = RateIncrementer?.Invoke() ?? 1D }); + return new RateLimitCounter + { + Count = (double)count, + Timestamp = intervalStart + }; + } + catch (Exception ex) when (ex is RedisException || ex is RedisCommandException || ex is RedisTimeoutException) + { + _logger.LogWarning($"Error occurred: {ex.Message}"); + return new RateLimitCounter + { + Count = 0, + Timestamp = intervalStart + }; + } } } } diff --git a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheClientPolicyStore.cs b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheClientPolicyStore.cs index f8ff4790..f43c708e 100644 --- a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheClientPolicyStore.cs +++ b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheClientPolicyStore.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace AspNetCoreRateLimit @@ -8,11 +9,12 @@ public class DistributedCacheClientPolicyStore : DistributedCacheRateLimitStore< { private readonly ClientRateLimitOptions _options; private readonly ClientRateLimitPolicies _policies; - + private readonly ILogger _logger; public DistributedCacheClientPolicyStore( IDistributedCache cache, + ILogger logger, IOptions options = null, - IOptions policies = null) : base(cache) + IOptions policies = null) : base(cache,logger) { _options = options?.Value; _policies = policies?.Value; diff --git a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheIpPolicyStore.cs b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheIpPolicyStore.cs index 565a7d72..86372df5 100644 --- a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheIpPolicyStore.cs +++ b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheIpPolicyStore.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace AspNetCoreRateLimit @@ -8,11 +9,12 @@ public class DistributedCacheIpPolicyStore : DistributedCacheRateLimitStore _logger; public DistributedCacheIpPolicyStore( IDistributedCache cache, + ILogger logger, IOptions options = null, - IOptions policies = null) : base(cache) + IOptions policies = null) : base(cache, logger) { _options = options?.Value; _policies = policies?.Value; diff --git a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitCounterStore.cs b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitCounterStore.cs index abe82410..090ad41a 100644 --- a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitCounterStore.cs +++ b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitCounterStore.cs @@ -1,10 +1,11 @@ using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Logging; namespace AspNetCoreRateLimit { public class DistributedCacheRateLimitCounterStore : DistributedCacheRateLimitStore, IRateLimitCounterStore { - public DistributedCacheRateLimitCounterStore(IDistributedCache cache) : base(cache) + public DistributedCacheRateLimitCounterStore(IDistributedCache cache,ILogger logger) : base(cache,logger) { } } diff --git a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitStore.cs b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitStore.cs index c2f5d005..b654c259 100644 --- a/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitStore.cs +++ b/src/AspNetCoreRateLimit/Store/DistributedCache/DistributedCacheRateLimitStore.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Threading; @@ -9,10 +10,12 @@ namespace AspNetCoreRateLimit public class DistributedCacheRateLimitStore : IRateLimitStore { private readonly IDistributedCache _cache; + private readonly ILogger> _logger; - public DistributedCacheRateLimitStore(IDistributedCache cache) + public DistributedCacheRateLimitStore(IDistributedCache cache, ILogger> logger) { _cache = cache; + _logger = logger; } public Task SetAsync(string id, T entry, TimeSpan? expirationTime = null, CancellationToken cancellationToken = default) @@ -23,32 +26,60 @@ public Task SetAsync(string id, T entry, TimeSpan? expirationTime = null, Cancel { options.SetAbsoluteExpiration(expirationTime.Value); } - - return _cache.SetStringAsync(id, JsonConvert.SerializeObject(entry), options, cancellationToken); + try + { + return _cache.SetStringAsync(id, JsonConvert.SerializeObject(entry), options, cancellationToken); + } + catch (Exception ex) + { + _logger.LogWarning($"Error occurred: {ex.Message}"); + return Task.CompletedTask; + } } public async Task ExistsAsync(string id, CancellationToken cancellationToken = default) { - var stored = await _cache.GetStringAsync(id, cancellationToken); - - return !string.IsNullOrEmpty(stored); + try + { + var stored = await _cache.GetStringAsync(id, cancellationToken); + return !string.IsNullOrEmpty(stored); + } + catch (Exception ex) + { + _logger.LogWarning($"Error occurred: {ex.Message}"); + return true; + } } public async Task GetAsync(string id, CancellationToken cancellationToken = default) { - var stored = await _cache.GetStringAsync(id, cancellationToken); + try + { + var stored = await _cache.GetStringAsync(id, cancellationToken); - if (!string.IsNullOrEmpty(stored)) + if (!string.IsNullOrEmpty(stored)) + { + return JsonConvert.DeserializeObject(stored); + } + } + catch (Exception ex) { - return JsonConvert.DeserializeObject(stored); + _logger.LogWarning($"Error occurred: {ex.Message}"); } - return default; } public Task RemoveAsync(string id, CancellationToken cancellationToken = default) { - return _cache.RemoveAsync(id, cancellationToken); + try + { + return _cache.RemoveAsync(id, cancellationToken); + } + catch (Exception ex) + { + _logger.LogWarning($"Error occurred: {ex.Message}"); + return default; + } } } } \ No newline at end of file