Skip to content

Repository files navigation

SharpApiRateLimit

Super simple API throttling / rate limit framework based on attributes

How to Use

Install the SharpApiRateLimit package via Nuget:

Install-Package SharpApiRateLimit

Open your Startup.cs and configure your reverse proxy:

publicvoidConfigureServices(IServiceCollectionservices){services.AddRateLimit();//other
...}

Add your attributes on a controller or action level:

[RateLimitByHeader("X-UserId","1s",calls:1)]publicclassValuesController:ControllerBase{[HttpGet][RateLimitByHeader("X-UserId","15s",calls:2)]publicActionResult<IEnumerable<string>>Get(){returnnewstring[]{"value1","value2"};}[HttpGet("{id}")][RateLimitByHeader("X-UserId","10m",calls:100)]publicActionResult<string>Get(intid){return"value";}[HttpPost]publicvoidPost([FromBody]stringvalue){}[HttpPut("{id}")]publicvoidPut(intid,[FromBody]stringvalue){}[HttpDelete("{id}")]publicvoidDelete(intid){}}

In the example above, all calls will be rate limited to 1 call per second. Users will be identified using the header 'X-UserId', use the one you need.

The limits for the actions GET:/values/get and GET:/values/get/{id} will be overridden with the local attribute.

Valid periods:

  • s: seconds. Ex: 1s, 4s
  • m: minutes. Ex: 10m, 30m
  • h: hours. Ex: 1h, 10h
  • d: days. Ex. 1d, 2d

Customize your storage:

Information about current calls are stored using Asp.Net IMemoryCache. If you want to change that, just create a class that implements IRateLimitStorage and register it with the ServiceLocator.

Ex:

services.AddSingleton<IRateLimitStorage, MyCustomStorage>();

Customize your 429 message response

To change the error message, create a class implementing IRateLimitResponseSerializer and register it:

Ex:

services.AddSingleton<IRateLimitResponseSerializer, MyCustomResponseSerializer>();

Enjoy!

About

Super simple API throttling / rate limit framework based on attributes

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages