当前位置:网站首页>. net core current limiting control - aspnetcoreratelimit
. net core current limiting control - aspnetcoreratelimit
2022-04-22 01:25: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/202204220104106175.html
边栏推荐
- PRANET training file train Py detailed explanation -- Shaoshuai
- What are the advantages of an automated testing framework?
- Click, walk and move of characters in 3D sandbox game
- 天边的彩虹很美丽
- Cloud security daily 220421: Cisco virtualization infrastructure manager software found a privilege upgrade vulnerability and needs to be upgraded as soon as possible
- PaddlePaddle基本用法详解(四)、PaddlePaddle训练文本分类模型
- 2022年4月21日,第14天
- 系统工程师面试问题,大专生三面蚂蚁金服
- Draw2d custom dashboard
- 【PraNet】论文代码解读(损失函数部分)——Blank
猜你喜欢

网络模型 LSTM模型内容详解

【Pranet】论文及代码解读(Res2Net部分)——peiheng jia

Solve the problem that the idea web project does not have small blue dots

matlab:进行控制系统仿真,使用ode45求解LC电路

系统工程师面试问题,大专生三面蚂蚁金服

How does PR resize the cropped video to the desired size?

【音视频】RTCP

落地实例:带你六步拆解DDD

This problem occurs if you are running in 64 bit mode with 32-bit Oracle client components installed

draw2d自定义仪表盘
随机推荐
华为云云主机体验有感:你的软件收纳专家
R language ggplot2 visualizes scatter plot, highlights the specified data points based on combination rules, sets the size and color of data points
matlab:进行控制系统仿真,使用ode45求解LC电路
April 21, 2022, day 14
R language generalized linear model function GLM, generalized linear models and GLM function to build logistic regression model
Kotlin - Introduction to higher order functions
贼厉害,最新Android面试合集
MySQL creates and authorizes remote login users
Detailed explanation of network model LSTM model content
Fast r-cnn code detailed annotation data shape
腾讯T3大牛手把手教你,90%的人看完都说好
MySQL创建远程登陆用户并授权
Prometheus 的使用
为什么PR导出来的视频,偏紫色?
[PRANET] thesis and code interpretation (RESNET part) -- Jiang Nie
2022年春招大厂面试升级笔记,光CRUD已经不能满足了
Tencent team strength to create an introduction course to fluent, 1-3 years of Android Development Engineer Interview Experience Sharing
Faster R-CNN代码详解 标注数据形状
[PRANET] paper and code interpretation -- cfsong
MySQL crée un utilisateur de connexion à distance et autorise