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

- Added Redis Rate Limit Configuration. #431

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
16 changes: 13 additions & 3 deletions src/AspNetCoreRateLimit.Redis/RedisProcessingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;

namespace AspNetCoreRateLimit.Redis
{
Expand All @@ -12,15 +13,24 @@ public class RedisProcessingStrategy : ProcessingStrategy
private readonly IRateLimitConfiguration _config;
private readonly ILogger<RedisProcessingStrategy> _logger;

public RedisProcessingStrategy(IConnectionMultiplexer connectionMultiplexer, IRateLimitConfiguration config, ILogger<RedisProcessingStrategy> logger)
public RedisProcessingStrategy(IConnectionMultiplexer connectionMultiplexer,
IRateLimitConfiguration config,
ILogger<RedisProcessingStrategy> logger,
IOptions<RedisRateLimitConfiguration> optionAccessor
)
: base(config)
{
_connectionMultiplexer = connectionMultiplexer ?? throw new ArgumentException("IConnectionMultiplexer was null. Ensure StackExchange.Redis was successfully registered");
_config = config;
_logger = logger;
var options = optionAccessor?.Value;
_connectionMultiplexer = options?.ConnectionMultiplexerFactory == null
? connectionMultiplexer ?? throw new ArgumentException(
"IConnectionMultiplexer was null. Ensure StackExchange.Redis was successfully registered")
: options.ConnectionMultiplexerFactory().GetAwaiter().GetResult();

}

static private readonly LuaScript _atomicIncrement = LuaScript.Prepare("local count = redis.call(\"INCRBYFLOAT\", @key, tonumber(@delta)) local ttl = redis.call(\"TTL\", @key) if ttl == -1 then redis.call(\"EXPIRE\", @key, @timeout) end return count");
private static readonly LuaScript _atomicIncrement = LuaScript.Prepare("local count = redis.call(\"INCRBYFLOAT\", @key, tonumber(@delta)) local ttl = redis.call(\"TTL\", @key) if ttl == -1 then redis.call(\"EXPIRE\", @key, @timeout) end return count");

public override async Task<RateLimitCounter> ProcessRequestAsync(ClientRequestIdentity requestIdentity, RateLimitRule rule, ICounterKeyBuilder counterKeyBuilder, RateLimitOptions rateLimitOptions, CancellationToken cancellationToken = default)
{
Expand Down
20 changes: 20 additions & 0 deletions src/AspNetCoreRateLimit.Redis/RedisRateLimitConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using StackExchange.Redis;

namespace AspNetCoreRateLimit.Redis
{
/// <summary>
/// Configuration options for <see cref="RedisProcessingStrategy"/>.
/// </summary>
public class RedisRateLimitConfiguration : IOptions<RedisRateLimitConfiguration>
{
/// <summary>
/// Gets or sets a delegate to create the ConnectionMultiplexer instance.
/// </summary>
public Func<Task<IConnectionMultiplexer>> ConnectionMultiplexerFactory { get; set; }

public RedisRateLimitConfiguration Value => this;
}
}
10 changes: 8 additions & 2 deletions src/AspNetCoreRateLimit.Redis/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.Extensions.DependencyInjection;

namespace AspNetCoreRateLimit.Redis
{
public static class StartupExtensions
{
public static IServiceCollection AddRedisRateLimiting(this IServiceCollection services)
public static IServiceCollection AddRedisRateLimiting(this IServiceCollection services, Action<RedisRateLimitConfiguration> setupAction = null)
{
services.AddOptions();
if (setupAction != null)
{
services.Configure(setupAction);
}
services.AddDistributedRateLimiting<RedisProcessingStrategy>();
return services;
}
Expand Down