stefanprodan / AspNetCoreRateLimit

ASP.NET Core rate limiting middleware
MIT License
3.11k stars 445 forks source link

4.0 Breaking Change Wiki out of date #211

Open lacutah opened 3 years ago

lacutah commented 3 years ago

I guess I got lucky and updated my project references first and had to wade through the test code to find out what was missing... I have no clue how to submit pull request for a wiki, LOL.

Might want a 4.0 breaking changes document along with 3.0 breaking changes.

Wiki examples needs updated to show (this is missing):

// register stores
services.AddInMemoryRateLimiting();
// OR
// services.AddDistributedRateLimiting<AsyncKeyLockProcessingStrategy>();

The two pages: https://github.com/stefanprodan/AspNetCoreRateLimit/wiki/ClientRateLimitMiddleware#setup https://github.com/stefanprodan/AspNetCoreRateLimit/wiki/IpRateLimitMiddleware

Thanks for the great package!

hedronn commented 3 years ago

Me too, thanks for your help.

ghost commented 3 years ago

+1 for 4.0 breaking changes document

mayne92 commented 3 years ago

@lacutah Thanks for pointing out the breaking change, fix. +1 for 4.0 breaking changes doc.

rebeccapowell commented 3 years ago

+1 for RateLimitConfiguration and IClientResolveContributor docs in 4.X

deivydas321 commented 3 years ago

Until there are no official docs, can someone share a working example of Client rate limiting using Redis?

services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));

//load general configuration from appsettings.json
services.Configure<ClientRateLimitOptions>(Configuration.GetSection("ClientRateLimiting"));
services.Configure<ClientRateLimitPolicies>(Configuration.GetSection("ClientRateLimitPolicies"));

services.AddDistributedRateLimiting<AsyncKeyLockProcessingStrategy>();
services.AddDistributedRateLimiting<RedisProcessingStrategy>();
services.AddSingleton<IIpPolicyStore, DistributedCacheIpPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, DistributedCacheRateLimitCounterStore>();

var redisOptions = ConfigurationOptions.Parse(Configuration["ConnectionStrings:Redis"]);
services.AddSingleton<IConnectionMultiplexer>(provider => ConnectionMultiplexer.Connect(redisOptions));
services.AddRedisRateLimiting();

services.AddSingleton<IRateLimitConfiguration, CustomRateLimitConfiguration>();

I am getting this error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: AspNetCoreRateLimit.IIpPolicyStore Lifetime: Singleton ImplementationType: AspNetCoreRateLimit.DistributedCacheIpPolicyStore

Current documentation is confusing version v3 vs v4

timonmasberg commented 2 years ago

@deivyd321 have you figured it out? Would be wonderful if you could share your results.

madebyluque commented 1 year ago

@deivyd321 have you figured it out? Would be wonderful if you could share your results.

Have you found any way to solve this issue? I'm still getting it :/

hedronn commented 1 year ago

@madebyluque This is the implementation I have for my .NET 6 API using IP Rate Limiting with Azure Redis Cache. I have not tested this in a load balancer setup as yet, only a single server development environment. But if this works for you in a cluster environment I would be grateful for the confirmation.

[Nuget Packages]

AspNetCoreRateLimit 5.0.0 StackExchange.Redis 2.2.4

[Startup.cs]

public void ConfigureServices(IServiceCollection services) { ...

services.AddDistributedRateLimiting();
services.AddStackExchangeRedisCache(option => { option.Configuration = Configuration["RedisCache:ConnectionString"]; option.InstanceName = "RedisCacheInstanceName"; });

services.Configure(Configuration.GetSection("IpRateLimiting")); services.Configure(Configuration.GetSection("IpRateLimitPolicies"));

services.AddSingleton<IIpPolicyStore, DistributedCacheIpPolicyStore>(); services.AddSingleton<IRateLimitCounterStore, DistributedCacheRateLimitCounterStore>(); services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();

... }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ...

app.UseIpRateLimiting();

... }

---------- Throttled Response -------- Error: response status is 429

{ "message": "Status code: 429", "details": "API call quota exceeded" }

Hope this helps