title: Simple way to rate limit HttpClient requests
body: <p>I am using the HTTPClient in System.Net.Http to make requests against an API. The API is limited to 10 requests per second. </p>
  
  <p>My code is roughly like so:</p>
  
  <pre><code>    List&lt;Task&gt; tasks = new List&lt;Task&gt;();
      items..Select(i =&gt; tasks.Add(ProcessItem(i));
  
      try
      {
          await Task.WhenAll(taskList.ToArray());
      }
      catch (Exception ex)
      {
      }
  </code></pre>
  
  <p>The ProcessItem method does a few things but always calls the API using the following:
  <code>await SendRequestAsync(..blah)</code>. Which looks like:</p>
  
  <pre><code>private async Task&lt;Response&gt; SendRequestAsync(HttpRequestMessage request, CancellationToken token)
  {    
      token.ThrowIfCancellationRequested();
      var response = await HttpClient
          .SendAsync(request: request, cancellationToken: token).ConfigureAwait(continueOnCapturedContext: false);
  
      token.ThrowIfCancellationRequested();
      return await Response.BuildResponse(response);
  }
  </code></pre>
  
  <p>Originally the code worked fine but when I started using Task.WhenAll I started getting 'Rate Limit Exceeded' messages from the API. How can I limit the rate at which requests are made?</p>
  
  <p>Its worth noting that ProcessItem can make between 1-4 API calls depending on the item. </p>