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

Handled exceptions to complete serving request received #484

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 17 additions & 5 deletions src/AspNetCoreRateLimit.Redis/RedisProcessingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,24 @@ public async Task<RateLimitCounter> 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
};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace AspNetCoreRateLimit
Expand All @@ -8,11 +9,12 @@ public class DistributedCacheClientPolicyStore : DistributedCacheRateLimitStore<
{
private readonly ClientRateLimitOptions _options;
private readonly ClientRateLimitPolicies _policies;

private readonly ILogger<DistributedCacheClientPolicyStore> _logger;
public DistributedCacheClientPolicyStore(
IDistributedCache cache,
ILogger<DistributedCacheClientPolicyStore> logger,
IOptions<ClientRateLimitOptions> options = null,
IOptions<ClientRateLimitPolicies> policies = null) : base(cache)
IOptions<ClientRateLimitPolicies> policies = null) : base(cache,logger)
{
_options = options?.Value;
_policies = policies?.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace AspNetCoreRateLimit
Expand All @@ -8,11 +9,12 @@ public class DistributedCacheIpPolicyStore : DistributedCacheRateLimitStore<IpRa
{
private readonly IpRateLimitOptions _options;
private readonly IpRateLimitPolicies _policies;

private readonly ILogger<DistributedCacheIpPolicyStore> _logger;
public DistributedCacheIpPolicyStore(
IDistributedCache cache,
ILogger<DistributedCacheIpPolicyStore> logger,
IOptions<IpRateLimitOptions> options = null,
IOptions<IpRateLimitPolicies> policies = null) : base(cache)
IOptions<IpRateLimitPolicies> policies = null) : base(cache, logger)
{
_options = options?.Value;
_policies = policies?.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;

namespace AspNetCoreRateLimit
{
public class DistributedCacheRateLimitCounterStore : DistributedCacheRateLimitStore<RateLimitCounter?>, IRateLimitCounterStore
{
public DistributedCacheRateLimitCounterStore(IDistributedCache cache) : base(cache)
public DistributedCacheRateLimitCounterStore(IDistributedCache cache,ILogger<DistributedCacheRateLimitCounterStore> logger) : base(cache,logger)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Threading;
Expand All @@ -9,10 +10,12 @@ namespace AspNetCoreRateLimit
public class DistributedCacheRateLimitStore<T> : IRateLimitStore<T>
{
private readonly IDistributedCache _cache;
private readonly ILogger<DistributedCacheRateLimitStore<T>> _logger;

public DistributedCacheRateLimitStore(IDistributedCache cache)
public DistributedCacheRateLimitStore(IDistributedCache cache, ILogger<DistributedCacheRateLimitStore<T>> logger)
{
_cache = cache;
_logger = logger;
}

public Task SetAsync(string id, T entry, TimeSpan? expirationTime = null, CancellationToken cancellationToken = default)
Expand All @@ -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<bool> 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<T> 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<T>(stored);
}
}
catch (Exception ex)
{
return JsonConvert.DeserializeObject<T>(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;
}
}
}
}