A rate limit library, support .NET Framework and .NET core.
Project: FireflySoft.RateLimit.Core.UnitTest
Code coverage:
Module | Line | Branch | Method |
---|---|---|---|
FireflySoft.RateLimit.Core | 95.84% | 85.77% | 99.06% |
Project | Descriptioin |
---|---|
FireflySoft.RateLmit.Core | algorithm, rules, persistence and other core codes. |
FireflySoft.RateLmit.Core.Sample | FireflySoft.RateLmit.Core sample program. |
FireflySoft.RateLimit.AspNet | ASP.NET rate-limit middleware based on .NET Framework. |
FireflySoft.RateLimit.AspNet.Sample | FireflySoft.RateLimit.AspNet smample program. |
FireflySoft.RateLimit.AspNetCore | ASP.NET Core rate-limit middleware. |
FireflySoft.RateLimit.AspNetCore.Sample | FireflySoft.RateLimit.AspNetCore smample program. |
FireflySoft.RateLimit.Core.UnitTest | Unit test for FireflySoft.RateLimit.Core. |
FireflySoft.RateLimit.Core.BenchmarkTest | Benchmark test for FireflySoft.RateLimit.Core. |
1、Install Nuget Package
Package Manager:
Install-Package FireflySoft.RateLimit.AspNetCore
Or .NET CLI:
dotnet add package FireflySoft.RateLimit.AspNetCore
Or Project file:
<ItemGroup>
<PackageReference Include="FireflySoft.RateLimit.AspNetCore" Version="2.*" />
</ItemGroup>
2、Use Middleware
The following code calls the rate-limit middleware from Startup.Configure:
public void ConfigureServices(IServiceCollection services)
{
...
app.AddRateLimit(new InProcessFixedWindowAlgorithm(
new[] {
new FixedWindowRule()
{
ExtractTarget = context =>
{
return (context as HttpContext).Request.Path.Value;
},
CheckRuleMatching = context =>
{
return true;
},
Name="default limit rule",
LimitNumber=30,
StatWindow=TimeSpan.FromSeconds(1)
}
})
);
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseRateLimit();
...
}
1、Install Nuget Package:
Package Manager:
Install-Package FireflySoft.RateLimit.AspNet
2、Register MessageHandler
Open Global.asax.cs, the following code adds the rate limit message handle:
protected void Application_Start()
{
...
GlobalConfiguration.Configuration.MessageHandlers.Add(
new RateLimitHandler(
new Core.InProcessAlgorithm.InProcessFixedWindowAlgorithm(
new[] {
new FixedWindowRule()
{
ExtractTarget = context =>
{
return (context as HttpRequestMessage).RequestUri.AbsolutePath;
},
CheckRuleMatching = context =>
{
return true;
},
Name="default limit rule",
LimitNumber=30,
StatWindow=TimeSpan.FromSeconds(1)
}
})
));
...
}
1、Install Nuget Package
Package Manager:
Install-Package FireflySoft.RateLimit.Core
Or .NET CLI:
dotnet add package FireflySoft.RateLimit.Core
2、Use IAlgorithm
Use IAlgorithm to filter every request, process the return value of Check method.
// Rule
var fixedWindowRules = new FixedWindowRule[]
{
new FixedWindowRule()
{
Id = "3",
StatWindow=TimeSpan.FromSeconds(1),
LimitNumber=30,
ExtractTarget = (request) =>
{
return (request as SimulationRequest).RequestResource;
},
CheckRuleMatching = (request) =>
{
return true;
},
}
};
// Algorithm
IAlgorithm algorithm = new InProcessFixedWindowAlgorithm(fixedWindowRules);
// Check
var result = algorithm.Check(new SimulationRequest()
{
RequestId = Guid.NewGuid().ToString(),
RequestResource = "home",
Parameters = new Dictionary<string, string>() {
{ "from","sample" },
}
});
SimulationRequest is a custom request that you can modify to any type.
概述 FireflySoft.RateLimit自2021年1月发布第一个版本以来,经历了多次升级迭代,目前已经十分稳定,被很多开发者应用到了生产系统中,最新发布的版本是3.0.0。 Github:https://github.com/bosima/FireflySoft.RateLimit 码云:https://gitee.com/bosima/FireflySoft.RateLimit 它的核