Jar*_*xon 208 asp.net-mvc throttling
我们正在尝试各种方法来限制用户在给定时间段内的操作:
目前,我们正在使用Cache来简单地插入用户活动的记录 - 如果用户执行相同活动时存在该记录,我们就会加油.
使用缓存自动为我们提供过时的数据清理和用户的滑动活动窗口,但它如何扩展可能是一个问题.
还有哪些其他方法可以确保可以有效地限制请求/用户操作(强调稳定性)?
Jar*_*xon 236
这是我们过去一年在Stack Overflow上使用的通用版本:
/// <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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
样品用法:
[Throttle(Name="TestThrottle", Message = "You must wait {n} seconds before accessing this url again.", Seconds = 5)]
public ActionResult TestThrottle()
{
return Content("TestThrottle executed");
}
Run Code Online (Sandbox Code Playgroud)
ASP.NET Cache就像一个冠军 - 通过使用它,您可以自动清理您的油门条目.随着我们不断增长的流量,我们没有看到这是服务器上的问题.
随意提供有关此方法的反馈; 当我们使Stack Overflow更好时,你的Ewok修复速度更快:)
not*_*ndy 68
微软在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 millisecondsIP地址将被阻止Y milliseconds,然后请求将被再次允许.
Rob*_*aft 11
我们使用从这个URL http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx借来的技术,不是为了限制,而是为了穷人的拒绝服务(DOS).这也是基于缓存的,可能与您正在执行的操作类似.你是否限制防止DOS攻击?路由器肯定可以用来减少DOS; 你认为路由器可以处理你需要的节流吗?
| 归档时间: |
|
| 查看次数: |
46835 次 |
| 最近记录: |