ASP.NET MVC 3中的每日调用限制是什么?

3
我看过Jarrod Dixon的解决方案 (Best way to implement request throttling in ASP.NET MVC?) 来实现每秒钟调用次数的限制。我现在正在尝试构建一个类似的过滤器,来实现N次每天的调用限制。
我正在构建一个开发者API,免费账户每天可以进行约100次调用,付费账户则有更高的速率限制。在MVC 3中,实现每日调用次数限制的最佳方法是什么?
1个回答

3
我认为在这种需要长时间测量的情况下,内存结构是不够的。在这种情况下,IIS回收会有问题。因此,我建议记录用户对资源的访问,并仅允许在过去24小时内计数100次。
另一方面,以下是我们实现的漏桶限制器(在短期限制中更加方便,其中失败相对不重要)。使用.NET 4并发集合可能会改善此实现中有些粗暴的锁定:
public class RateLimiter
{
    private readonly double numItems;
    private readonly double ratePerSecond;
    private readonly Dictionary<object, RateInfo> rateTable = 
        new Dictionary<object, RateInfo>();
    private readonly object rateTableLock = new object();
    private readonly double timePeriod;

    public RateLimiter(double numItems, double timePeriod)
    {
        this.timePeriod = timePeriod;
        this.numItems = numItems;
        ratePerSecond = numItems / timePeriod;
    }

    public double Count
    {
        get
        {
            return numItems;
        }
    }

    public double Per
    {
        get
        {
            return timePeriod;
        }
    }

    public bool IsPermitted(object key)
    {
        RateInfo rateInfo;
        var permitted = true;
        var now = DateTime.UtcNow;
        lock (rateTableLock)
        {
            var expiredKeys = 
                rateTable
                .Where(kvp => 
                    (now - kvp.Value.LastCheckTime) 
                    > TimeSpan.FromSeconds(timePeriod))
                .Select(k => k.Key)
                .ToArray();
            foreach (var expiredKey in expiredKeys)
            {
                rateTable.Remove(expiredKey);
            }
            var dataExists = rateTable.TryGetValue(key,
                                                   out rateInfo);
            if (dataExists)
            {
                var timePassedSeconds = (now - rateInfo.LastCheckTime).TotalSeconds;
                var newAllowance = 
                     Math.Min(
                         rateInfo.Allowance 
                         + timePassedSeconds 
                         * ratePerSecond,
                         numItems);
                if (newAllowance < 1d)
                {
                    permitted = false;
                }
                else
                {
                    newAllowance -= 1d;
                }
                rateTable[key] = new RateInfo(now,
                                              newAllowance);
            }
            else
            {
                rateTable.Add(key,
                              new RateInfo(now,
                                           numItems - 1d));
            }

        }
        return permitted;
    }

    public void Reset(object key)
    {
        lock (rateTableLock)
        {
            rateTable.Remove(key);
        }
    }

    private struct RateInfo
    {
        private readonly double allowance;
        private readonly DateTime lastCheckTime;

        public RateInfo(DateTime lastCheckTime, double allowance)
        {
            this.lastCheckTime = lastCheckTime;
            this.allowance = allowance;
        }

        public DateTime LastCheckTime
        {
            get
            {
                return lastCheckTime;
            }
        }

        public double Allowance
        {
            get
            {
                return allowance;
            }
        }
    }

}

2
我强烈建议不要在锁语句之外声明(和初始化)now。虽然对于你的应用程序来说这是不太可能发生的,但如果调用线程在lock语句上长时间阻塞,你可能会得到与你在IsPermitted调用中期望的结果不同的结果。 - Mike Bailey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接