title: An API rate limiter
body: <p>I wrote an API rate limiter to use with Last.fm's API. </p>
  
  <p>Last.fm's Tos states that I cannot make more than 5 requests per originating IP address per second, averaged over a 5 minute period.</p>
  
  <p>Here is the class I wrote:</p>
  
  <pre><code>public class RateLimiter
  {
      private static readonly List&lt;DateTime&gt; _requests = new List&lt;DateTime&gt;();
  
      private const double _perMillisecond = 1000.1;
      private const int _rateLimit = 5;
      private const int _rateLimitCooldownMs = 500;
  
      public static void CheckLimiter()
      {
          _requests.Add(DateTime.Now);
  
          var requestsDuringRateLimit = _requests.Where(w =&gt; (DateTime.Now - w).TotalMilliseconds &lt; _perMillisecond).ToArray();
  
          if (requestsDuringRateLimit.Count() &gt;= _rateLimit)
          {
              Thread.Sleep(_rateLimitCooldownMs);
              _requests.Clear();
              Console.Clear();
          }
      }
  }
  </code></pre>
  
  <p>The <code>CheckLimiter</code> method is called before the <code>HttpWebRequest</code> is initiated, is this a good way to limit API requests?</p>