快站公众号,青岛软件开发公司有哪些,合肥做企业建网站那家好,如何在微信公众平台添加wordpressMicrosoft.AspNetCore.RateLimiting 中间件提供速率限制#xff08;限流#xff09;中间件。
它是.NET 7 以上版本才支持的中间件#xff0c;刚看了一下#xff0c;确实挺好用#xff0c;下面给大家简单介绍一下#xff1a;
RateLimiterOptionsExtensions 类提供下列用…Microsoft.AspNetCore.RateLimiting 中间件提供速率限制限流中间件。
它是.NET 7 以上版本才支持的中间件刚看了一下确实挺好用下面给大家简单介绍一下
RateLimiterOptionsExtensions 类提供下列用于限制速率的扩展方法
固定窗口限制器滑动窗口限制器令牌桶限制器并发限制器
固定窗口限制器
AddFixedWindowLimiter 方法使用固定的时间窗口来限制请求。 当时间窗口过期时会启动一个新的时间窗口并重置请求限制。
滑动窗口限制器
滑动窗口算法
与固定窗口限制器类似但为每个窗口添加了段。 窗口在每个段间隔滑动一段。 段间隔的计算方式是(窗口时间)/(每个窗口的段数)。将窗口的请求数限制为 permitLimit 个请求。每个时间窗口划分为一个窗口 n 个段。从倒退一个窗口的过期时间段当前段之前的 n 个段获取的请求会添加到当前的段。 我们将倒退一个窗口最近过期时间段称为“过期的段”。
令牌桶限制器
令牌桶限制器与滑动窗口限制器类似但它不会结存从过期段获取的请求数而是在每个补充期间添加固定数量的令牌。 每个段添加的令牌数不能使可用令牌数超过令牌桶限制。 下表显示了一个令牌桶限制器其中令牌数限制为 100 个补充期为 10 秒。
并发限制器
并发限制器会限制并发请求数。 每添加一个请求在并发限制中减去 1。 一个请求完成时在限制中增加 1。 其他请求限制器限制的是指定时间段的请求总数而与它们不同并发限制器仅限制并发请求数不对一段时间内的请求数设置上限。 以上介绍是参照微软学习文档相当枯燥无味简单总结就是以上限制器都是为了限制客户端频繁请求接口从而导致服务器压力过大的措施比如可以防止dos攻击。具体实现直接看代码会比较清楚
var builder WebApplication.CreateBuilder(args);builder.WebHost.ConfigureKestrel((context, serverOptions)
{serverOptions.Listen(IPAddress.Any, 5000);
});builder.Services.AddRateLimiter(configureOptions
{//固定窗口限制器configureOptions.AddFixedWindowLimiter(fixed, options {//options.QueueLimit 1;options.PermitLimit 3;options.Window TimeSpan.FromSeconds(10);options.QueueProcessingOrder QueueProcessingOrder.OldestFirst;});//滑动窗口限制器configureOptions.AddSlidingWindowLimiter(policyName: sliding, options {options.PermitLimit 3;options.Window TimeSpan.FromSeconds(10);options.SegmentsPerWindow 1;options.QueueProcessingOrder QueueProcessingOrder.OldestFirst;//options.QueueLimit 1;});//令牌桶限制器configureOptions.AddTokenBucketLimiter(policyName: token, options {options.TokenLimit 3;options.QueueProcessingOrder QueueProcessingOrder.OldestFirst;//options.QueueLimit 1;options.ReplenishmentPeriod TimeSpan.FromSeconds(10);options.TokensPerPeriod 2;options.AutoReplenishment true;});//并发configureOptions.AddConcurrencyLimiter(ConLimit, options {options.QueueLimit 10; //达到并发限制进入队列排队的数量多余会被丢弃options.PermitLimit 200;//并发请求最大数量options.QueueProcessingOrder QueueProcessingOrder.OldestFirst;});
});// Add services to the container.
//builder.Services.AddRazorPages();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();var app builder.Build();// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{app.UseExceptionHandler(/Error);// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.app.UseHsts();
}
app.UseSwagger();
app.UseSwaggerUI(c { });app.UseHttpsRedirection();
//app.UseStaticFiles();
//app.UseRouting();app.UseAuthorization();//app.MapRazorPages();
app.MapControllers();//使用限制器
app.UseRateLimiter();//固定窗口限制器
static string GetTicks() DateTime.Now.ToString(HH:mm:ss);
app.MapGet(/fixed, () Results.Ok($固定窗口限制器 {GetTicks()})).RequireRateLimiting(fixed);
//滑动窗口限制器
app.MapGet(/sliding, () Results.Ok($滑动窗口限制器 {GetTicks()})).RequireRateLimiting(sliding);
//令牌桶限制器
app.MapGet(/token, () Results.Ok($令牌桶限制器 {GetTicks()})).RequireRateLimiting(token);
//并发限制器
app.MapGet(/ConLimit, () Results.Ok($并发限制器 {GetTicks()})).RequireRateLimiting(ConLimit);app.Run();
核心代码就这些 运行程序请求http://localhost:5000/fixed 如果10s内请求大于3次就失败 10秒后又可以正常访问 对指定接口进行限制只需要在指定接口上使用EnableRateLimiting特性就行
[EnableRateLimiting(sliding)]
[HttpGet]
public string GetString()
{return 111;
}
10秒内请求超3次就失败 注意上面的 options.QueueLimit 被我注释了这个主要是设置排队的请求的最大累计允许计数如果设置了就不会报错而是等待请求数值是具体可以同时等待的请求数。 文章转载自: http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.mljtx.cn.gov.cn.mljtx.cn http://www.morning.plydc.cn.gov.cn.plydc.cn http://www.morning.ggmls.cn.gov.cn.ggmls.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.qqnp.cn.gov.cn.qqnp.cn http://www.morning.fkyqm.cn.gov.cn.fkyqm.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.brjq.cn.gov.cn.brjq.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.pyswr.cn.gov.cn.pyswr.cn http://www.morning.ndpwg.cn.gov.cn.ndpwg.cn http://www.morning.jrslj.cn.gov.cn.jrslj.cn http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.lmrcq.cn.gov.cn.lmrcq.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.kbdjn.cn.gov.cn.kbdjn.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.qhkdt.cn.gov.cn.qhkdt.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.rkxqh.cn.gov.cn.rkxqh.cn http://www.morning.jsphr.cn.gov.cn.jsphr.cn http://www.morning.jhrlk.cn.gov.cn.jhrlk.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.gjlst.cn.gov.cn.gjlst.cn http://www.morning.tjsxx.cn.gov.cn.tjsxx.cn http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.rtsd.cn.gov.cn.rtsd.cn http://www.morning.lhwlp.cn.gov.cn.lhwlp.cn http://www.morning.wfyzs.cn.gov.cn.wfyzs.cn http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.pwdgy.cn.gov.cn.pwdgy.cn http://www.morning.rkqqf.cn.gov.cn.rkqqf.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.bqdgr.cn.gov.cn.bqdgr.cn http://www.morning.nyhtf.cn.gov.cn.nyhtf.cn http://www.morning.mngh.cn.gov.cn.mngh.cn http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.nzwp.cn.gov.cn.nzwp.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.drqrl.cn.gov.cn.drqrl.cn http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn http://www.morning.xgzwj.cn.gov.cn.xgzwj.cn http://www.morning.ylsxk.cn.gov.cn.ylsxk.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.pwqyd.cn.gov.cn.pwqyd.cn