我们正在尝试各种方法来限制用户在给定时间段内的操作:
限制问答帖子
限制编辑
限制Feed检索
目前,我们正在使用Cache来简单地插入用户活动的记录 - 如果用户执行相同活动时存在该记录,我们就会加油.
使用缓存自动为我们提供过时的数据清理和用户的滑动活动窗口,但它如何扩展可能是一个问题.
还有哪些其他方法可以确保可以有效地限制请求/用户操作(强调稳定性)?
这是我们过去一年在Stack Overflow上使用的通用版本:
////// Decorates any MVC route that needs to have client requests limited by time. /// ////// Uses the current System.Web.Caching.Cache to store each client request to the decorated route. /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class ThrottleAttribute : ActionFilterAttribute { ////// A unique name for this Throttle. /// ////// We'll be inserting a Cache record based on this name and client IP, e.g. "Name-192.168.0.1" /// public string Name { get; set; } ////// The number of seconds clients must wait before executing this decorated route again. /// public int Seconds { get; set; } ////// 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". /// 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 Cache就像一个冠军 - 通过使用它,您可以自动清理您的油门条目.随着我们不断增长的流量,我们没有看到这是服务器上的问题.
随意提供有关此方法的反馈; 当我们使Stack Overflow更好时,你的Ewok修复速度更快:)
微软在IIS 7称为动态IP限制扩展IIS 7.0的一个新的扩展 - 测试版.
"IIS 7.0的动态IP限制是一个模块,可以防止对Web服务器和Web站点的拒绝服务和暴力攻击.这种保护是通过临时阻止HTTP客户端的IP地址来提供的,这些HTTP客户端会产生异常多的并发请求或者在很短的时间内提出大量请求." http://learn.iis.net/page.aspx/548/using-dynamic-ip-restrictions/
例:
如果您所设定的标准,以阻止后X requests in Y milliseconds
或X concurrent connections in Y milliseconds
IP地址将被阻止Y milliseconds
,然后请求将被再次允许.
我们使用从这个URL http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx借来的技术,不是为了限制,而是为了穷人的拒绝服务(DOS).这也是基于缓存的,可能与您正在执行的操作类似.你是否限制防止DOS攻击?路由器肯定可以用来减少DOS; 你认为路由器可以处理你需要的节流吗?