question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot make the RateLimit.Redis 4.0.1 work

See original GitHub issue

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:

image

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 ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
298029lkkcommented, Dec 2, 2021

@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";
            });
1reaction
brk114commented, Nov 26, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to see keys set by django-ratelimit in redis server?
I am trying to set up a rate-limit to limit the number of times a POST request is called. I have this in...
Read more >
Rate Limiting is not working in Istio 1.9 - Networking
It shows 500 response code for all the requests. But local rate limiting seems to be working fine. This is the redis config...
Read more >
p-ratelimit
This is an easy-to-use utility for calling rate-limited APIs. It will prevent you from exceeding rate limits by queueing requests that would ...
Read more >
rate-limit-redis
Start using rate-limit-redis in your project by running `npm i ... Create and use the rate limiter const limiter = rateLimit({ // Rate ......
Read more >
Rate Limiting - What is Rate Limiting? | Redis
In web applications, rate limiting restricts the number of requests that a client can make to a server within a given time period...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found