2023-02-17 410
我们正在尝试各种方式来在给定的时间段:
的
暂时,我们正在使用缓存来简单地插入用户活动的记录 – 如果该记录存在,如果用户在用户执行相同的活动时,我们会扼杀.
使用缓存自动为用户提供陈旧的数据清洁和滑动活动窗口,但它将如何缩放可能是一个问题.
确保可以有效地限制请求/用户操作的其他方法是什么?
这是我们在过去一年中使用的堆栈溢出的通用版本:
/// <summary>
/// Decorates any MVC route that needs to have client requests limited by time.
/// </summary>
/// <remarks>
/// Uses the current System.Web.Caching.Cache to store each client request to the decorated route.
/// </remarks>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ThrottleAttribute : ActionFilterAttribute
{
/// <summary>
/// A unique name for this Throttle.
/// </summary>
/// <remarks>
/// We'll be inserting a Cache record based on this name and client IP, e.g. "Name-192.168.0.1"
/// </remarks>
public string Name { get; set; }
/// <summary>
/// The number of seconds clients must wait before executing this decorated route again.
/// </summary>
public int Seconds { get; set; }
/// <summary>
/// A text message that will be sent to the client upon throttling. You can include the token {n} to
/// show this.Seconds in the message, e.g. "Wait {n} seconds before trying again".
/// </summary>
public string Message { get; set; }
public override void OnActionExecuting(ActionExecutingContext c)
{
var key = string.Concat(Name, "-", c.HttpContext.Request.UserHostAddress);
var allowExecute = false;
if (HttpRuntime.Cache[key] == null)
{
HttpRuntime.Cache.Add(key,
true, // is this the smallest data we can have?
null, // no dependencies
DateTime.Now.AddSeconds(Seconds), // absolute expiration
Cache.NoSlidingExpiration,
CacheItemPriority.Low,
null); // no callback
allowExecute = true;
}
if (!allowExecute)
{
if (String.IsNullOrEmpty(Message))
Message = "You may only perform this action every {n} seconds.";
c.Result = new ContentResult { Content = Message.Replace("{n}", Seconds.ToString()) };
// see 409 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
c.HttpContext.Response.StatusCode = (int)HttpStatusCode.Conflict;
}
}
}
样品用法:
[Throttle(Name="TestThrottle", Message = "You must wait {n} seconds before accessing this url again.", Seconds = 5)]
public ActionResult TestThrottle()
{
return Content("TestThrottle executed");
}
ASP.NET缓存在这里像CHAMP一样 – 通过使用它,您可以自动清理油门条目.随着我们越来越多的流量,我们没有看到服务器上的问题.
随意提供有关此方法的反馈;当我们使堆栈溢出更好时,您将获得 ewok修复甚至更快:)
Microsoft对IIS 7.0 – Beta的IIS 7具有IIS 7的新扩展名.
“IIS 7.0的动态IP限制是一个模块,它提供对Web服务器和网站上的拒绝服务和蛮力攻击的保护.通过临时阻止具有异常高的HTTP客户端的IP地址来提供这种保护同时请求或谁在较小的时间内制作大量请求.”
http://learn.iis.net/page.aspx/548/使用-动态IP限制/
示例:
如果将X requests in Y milliseconds或X concurrent connections in Y milliseconds在Y milliseconds中将标准设置为块,则为Y milliseconds,则将再次允许请求.
我们使用从此URL performance.aspx ,不用于节令,但对于穷人的拒绝服务(DoS).这也是基于缓存的,并且可能与您正在做的类似.你是为了防止d.o.s..攻击?路由器肯定可以用于减少D.O.S;你认为路由器可以处理你需要的限制吗?
以上所述是小编给大家介绍的在ASP.NET MVC中实现请求节流的最佳方式?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/34223.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态