当前位置:网站首页>. net core current limiting control - aspnetcoreratelimit
. net core current limiting control - aspnetcoreratelimit
2022-04-23 03:08:00 【Dotnet cross platform】
brief introduction
AspNetCoreRateLimit yes ASP.NET Core rate limiting framework , Can be right WebApi,Mvc Control current limiting in the system ,AspNetCoreRateLimit Package contains IpRateLimit Middleware and ClientRateLimit middleware , Each middleware can set multiple limits for different scenarios , The author of the framework is stefanprodan, project nuget The address is https://github.com/stefanprodan/AspNetCoreRateLimit.
IpRateLimitMiddleware(Github: AspNetCoreRateLimit) yes ASPNETCore A current limiting middleware , Used to control client calls API Frequency of , If the client accesses the server frequently , You can limit its frequency , Reduced pressure to access the server side . Or if a crawler is crawling for key data , You can also limit a certain / some API Or some IP Number of calls per day , This limits his climbing speed .
Usage method
NuGet install :
Install-Package AspNetCoreRateLimit
Install-Package AspNetCoreRateLimit.Redis
Startup.cs Code :
public void ConfigureServices(IServiceCollection services)
{
// needed to load configuration from appsettings.json
services.AddOptions();
// needed to store rate limit counters and ip rules
services.AddMemoryCache();
//load general configuration from appsettings.json
services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
//load ip rules from appsettings.json
services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
// inject counter and rules stores
services.AddInMemoryRateLimiting();
//services.AddDistributedRateLimiting<AsyncKeyLockProcessingStrategy>();
//services.AddDistributedRateLimiting<RedisProcessingStrategy>();
//services.AddRedisRateLimiting();
// Add framework services.
services.AddMvc();
// configuration (resolvers, counter key builders)
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseIpRateLimiting();
app.UseMvc();
}
You should register the middleware before registering any other components .
If you load balance your application , You need to IDistributedCache And Redis or SQLServer Use it together , So that all kestrel All instances have the same rate limit storage . You should inject distributed storage like this , Instead of memory storage :
// inject counter and rules distributed cache stores
services.AddSingleton<IIpPolicyStore, DistributedCacheIpPolicyStore>();
services.AddSingleton<IRateLimitCounterStore,DistributedCacheRateLimitCounterStore>();
Configuration and general rules application settings appsettings.json::
"IpRateLimiting": {
"EnableEndpointRateLimiting": false,
"StackBlockedRequests": false,
"RealIpHeader": "X-Real-IP",
"ClientIdHeader": "X-ClientId",
"HttpStatusCode": 429,
"IpWhitelist": [ "127.0.0.1", "::1/10", "192.168.0.0/24" ],
"EndpointWhitelist": [ "get:/api/license", "*:/api/status" ],
"ClientWhitelist": [ "dev-id-1", "dev-id-2" ],
"GeneralRules": [
{
"Endpoint": "*",
"Period": "1s",
"Limit": 2
},
{
"Endpoint": "*",
"Period": "15m",
"Limit": 100
},
{
"Endpoint": "*",
"Period": "12h",
"Limit": 1000
},
{
"Endpoint": "*",
"Period": "7d",
"Limit": 10000
}
]
}
If EnableEndpointRateLimiting Set to false, Then the limit will be applied globally , And only as endpoint* The rules of will apply . for example , If set per second 5 Limit of calls , Then for any HTTP Calls are counted against this limit .
If EnableEndpointRateLimiting Set to true, Then the restriction will apply to each endpoint , Such as {HTTP\u Verb}{PATH}. for example , If *:/api/values Set per second 5 Limit of calls , The client can call... Every second 5 Time GET/api/values, But you can also call 5 Time PUT/api/values.
If StackBlockedRequests Set to false, The rejected call will not be added to the throttle counter . If a client sends messages every second 3 A request , And you have set the limit to one call per second , So other restrictions ( Such as counter per minute or per day ) Only the first call that is not blocked will be recorded . If you want to reject a request, add other restrictions , You have to StackBlockedRequests Set to true.
When Kestrel When the server is behind the reverse proxy ,RealiPeader Used to extract client IP, If the proxy uses a different header , be X-Real-IP Use this option to set .
ClientHeader The client used to extract the whitelist id. If there is a client in this header id, And with ClientWhitelist Match the values specified in , The rate limit is not applied .
"IpRateLimitPolicies": {
"IpRules": [
{
"Ip": "84.247.85.224",
"Rules": [
{
"Endpoint": "*",
"Period": "1s",
"Limit": 10
},
{
"Endpoint": "*",
"Period": "15m",
"Limit": 200
}
]
},
{
"Ip": "192.168.3.22/25",
"Rules": [
{
"Endpoint": "*",
"Period": "1s",
"Limit": 5
},
{
"Endpoint": "*",
"Period": "15m",
"Limit": 150
},
{
"Endpoint": "*",
"Period": "12h",
"Limit": 500
}
]
}
]
}
IP Fields support IP v4 and v6 Value and range of , Such as “192.168.0.0/24”、“fe80::/10” or “192.168.0.0-192.168.0.255”.
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301333375.html
边栏推荐
- 全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆
- 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
- Use of slice grammar sugar in C #
- 2022年度Top9的任务管理系统
- The backtracking of stack is used to solve the problem of "the longest absolute path of file"
- Service avalanche effect
- .Net Core 限流控制-AspNetCoreRateLimit
- Xamarin effect Chapter 21 expandable floating operation button in GIS
- 腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
- Cherno_ Game engine series tutorial (5): 101~
猜你喜欢
![FileNotFoundError: [Errno 2] No such file or directory](/img/ea/0c3f2768d14c1f4bb42bd1309ab996.png)
FileNotFoundError: [Errno 2] No such file or directory

What kind of experience is it to prepare for a month to participate in ACM?

树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统

MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator

Fight leetcode again (290. Word law)

ASP.NET 6 中间件系列 - 执行顺序

Cherno_ Game engine series tutorial (5): 101~

MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure

Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)

Service avalanche effect
随机推荐
Summary of software test interview questions
Impact of AOT and single file release on program performance
[ncnn] - the meaning of - 23300 in param
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
2022年P气瓶充装培训试题及模拟考试
[new version release] componentone added Net 6 and blazor platform control support
Swap the left and right of each node in a binary tree
使用栈来解决”迷你语法分析器“的问题
中后二叉建树
数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
Passing object type parameters through openfeign
LNMP MySQL allows remote access
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
Middle and rear binary tree
Cherno_ Game engine series tutorial (5): 101~
Tips in MATLAB
[software testing] understand the basic knowledge of software testing
How to write the expected salary on your resume to double your salary during the interview?
TP5 multi conditional where query (using PHP variables)
Find the number of leaf nodes of binary tree