Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4273940
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:46:48+00:00 2026-05-21T07:46:48+00:00

I’ve seen Jarrod Dixon’s solution ( Best way to implement request throttling in ASP.NET

  • 0

I’ve seen Jarrod Dixon’s solution (Best way to implement request throttling in ASP.NET MVC?) for implementing calls-per-second rate limiting. I’m now trying to figure out how to build a similar filter for N-calls-per-day.

I am building a developer API where free accounts get ~100 calls per day and paid accounts get a higher rate limit. What’s the best way to do calls-per-day rate limiting in MVC 3?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:46:49+00:00Added an answer on May 21, 2026 at 7:46 am

    I don’t think that an in-memory structure would suffice here, due to the long duration that you need to measure. IIS recycling would be problematic in this case. As such, I’d recommend recording user access to the resource in the DB and only allowing a count of 100 in the last 24 hours.

    On the other hand, here’s our implementation of a leaky bucket limiter (which is more handy for short term limiting where failure is relatively unimportant). Using .NET 4 concurrent collections might improve on the somewhat brute-force locking in this implementation:

    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;
                }
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.