Manage rates
ManageRates is an open-source and cross-platform framework for managing throttling from asp.net core applications. In simple case it is an alternative project to AspNetCoreRateLimit.
Installation
To install ManageRates packange, run the following command in the Nuget Package Manager Console:
PM> Install-Package ManageRates.AspnetCore
Efficiency
Test of throttling. There is a log of work to this component it I want it works correctly.
Samples
Controller action restriction
Firstly we need to regitster neccessary services:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Manage rates service uses IMemoryCache, sow we need to add it.
services.AddMemoryCache();
// Register rate strictions services.
services.AddRateStrictions();
}
and add the middleware:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseManageRates();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
Finally, we can use attribute on action of a controller to restrinct it's usage:
[HttpGet]
[ManageRate(2, Period.Second, KeyType.RequestPath)]
public string Endpoint()
{
return nameof(Endpoint);
}
Restriction set of endpoints
Firstly we need to regitster neccessary services:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Manage rates service uses IMemoryCache, sow we need to add it.
services.AddMemoryCache();
// Register rate strictions services.
services.AddRateStrictions();
}
Then we can add the middleware where we describe set of endpoints by regular expression:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseManageRates(policyBuilder => policyBuilder
.AddPolicy("/raw/.*endpoint.*", 2, Period.Second, KeyType.None)
);
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
Add restriction functionality during endpoint configuration:
Firstly we need to regitster neccessary services:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Manage rates service uses IMemoryCache, sow we need to add it.
services.AddMemoryCache();
// Register rate strictions services.
services.AddRateStrictions();
}
Then we can add the middleware where we describe set of endpoints by regular expression:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/endpoint", context => context.Response.WriteAsync("endpoint"))
.ManageRates(2, Period.Second, KeyType.RequestPath);
endpoints.MapControllers();
});
}
Benchmark results
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19042
Intel Core i7-9750H CPU 2.60GHz, 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.201
[Host] : .NET Core 5.0.4 (CoreCLR 5.0.421.11614, CoreFX 5.0.421.11614), X64 RyuJIT
DefaultJob : .NET Core 5.0.4 (CoreCLR 5.0.421.11614, CoreFX 5.0.421.11614), X64 RyuJIT
Method | Mean | Error | StdDev | Ratio |
---|---|---|---|---|
Single | 12.447 ms | 0.0452 ms | 0.0422 ms | 2.03 |
First | 6.143 ms | 0.0432 ms | 0.0361 ms | 1.00 |
License
ManageRates.AspnetCore licensed under the MIT License.