Cannot make the RateLimit.Redis 4.0.1 work
luccasmf opened this issue · comments
I'm trying to implement the Ratelimit with redis but I keep receiving a lot of errors that I couldn't handle yet, so I'm begging for help.
My extesion method that implements all the rateLimit configuration:
public static IServiceCollection ConfigureRateLimit(this IServiceCollection services, IConfiguration configuration)
{
services.AddOptions();
//load general configuration from appsettings.json
services.Configure<IpRateLimitOptions>(configuration.GetSection("IpRateLimiting"));
services.AddStackExchangeRedisCache(options =>
{
options.ConfigurationOptions = new ConfigurationOptions
{
//silently retry in the background if the Redis connection is temporarily down
AbortOnConnectFail = false
};
options.Configuration = configuration.GetConnectionString("Redis");
options.InstanceName = "HubRateLimit";
});
var redisOptions = ConfigurationOptions.Parse(configuration["ConnectionStrings:Redis"]);
services.AddSingleton<IConnectionMultiplexer>(provider => ConnectionMultiplexer.Connect(redisOptions););
services.AddRedisRateLimiting();
// configuration (resolvers, counter key builders)
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
return services;
}
The following block was added since I was receiving the IDistributedCache error, since I added this block this error doesn't occur anymore
services.AddStackExchangeRedisCache(options =>
{
options.ConfigurationOptions = new ConfigurationOptions
{
//silently retry in the background if the Redis connection is temporarily down
AbortOnConnectFail = false
};
options.Configuration = configuration.GetConnectionString("Redis");
options.InstanceName = "HubRateLimit";
});
But I still have issues, since I get the following error when I try to call ANY endpoint:
My appsettings configuration is set as the following JSON:
"IpRateLimiting": {
"EnableEndpointRateLimiting": false,
"StackBlockedRequests": false,
"RealIpHeader": "X-Real-IP",
"ClientIdHeader": "X-ClientId",
"HttpStatusCode": 429,
"GeneralRules": [
{
"Endpoint": "*:/api/*",
"Period": "1s",
"Limit": 2
}
]
}
Am I doing something wrong ?
Try adding the following at the end of extension method
// configuration (resolvers, counter key builders)
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
// config to add
services.AddSingleton<IDistributedCache, RedisCache>();
// inject counter and rules distributed cache stores
services.AddSingleton<IClientPolicyStore, DistributedCacheClientPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, DistributedCacheRateLimitCounterStore>();
I got it working with above configuration.
hey @brk114 tks for your reply.
Still not working, I still receiving the same error..
No endpoints specified (Parameter 'configuration') at StackExchange.Redis.ConnectionMultiplexer.PrepareConfig(Object configuration, Boolean sentinel) in /_/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 917
my Redis connection string is in the following format:
elasticcache-xxxx.com:port,password=ABC123PWD,ssl=false,abortConnect=False,ConnectTimeout=10000
Am I missing some point? This is my method:
public static IServiceCollection ConfigureRateLimit(this IServiceCollection services, IConfiguration configuration)
{
services.AddOptions();
//load general configuration from appsettings.json
services.Configure<IpRateLimitOptions>(configuration.GetSection("IpRateLimiting"));
services.AddStackExchangeRedisCache(options =>
{
options.ConfigurationOptions = new ConfigurationOptions
{
//silently retry in the background if the Redis connection is temporarily down
AbortOnConnectFail = false
};
options.Configuration = configuration.GetConnectionString("Redis");
options.InstanceName = "HubRateLimit";
});
var redisOptions = ConfigurationOptions.Parse(configuration["ConnectionStrings:Redis"]);
services.AddSingleton<IConnectionMultiplexer>(provider => ConnectionMultiplexer.Connect(redisOptions));
services.AddRedisRateLimiting();
// configuration (resolvers, counter key builders)
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
services.AddSingleton<IDistributedCache, RedisCache>();
// inject counter and rules distributed cache stores
services.AddSingleton<IClientPolicyStore, DistributedCacheClientPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, DistributedCacheRateLimitCounterStore>();
return services;
}
Maybe I'm just being dumb AF.
@luccasmf Hello, I also encountered the same problem. No more errors after using the following method.
services.AddStackExchangeRedisCache(options =>
{
options.ConfigurationOptions = new ConfigurationOptions
{
AbortOnConnectFail = false,
EndPoints = { "192.168.1.173:16379" },
Password = "redis_123",
Ssl = false,
ConnectTimeout = 15000,
SyncTimeout = 15000
};
//options.Configuration = Configuration["RedisConnectionStrings"];
options.InstanceName = "AspNetRateLimit";
});
@298029lkk Thx bro, By setting this way I could see an error on the connection string itself (it's not an actual error, but it's for this lib). Now it is working nicely. OMG
I used the following section in order to make it work:
// Register to Redis
services.AddSingleton<IConnectionMultiplexer>( _ => ConnectionMultiplexer.Connect(connectionString));
// Add distributed cache to Redis
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = connectionString;
});