Skip to content

ThrottlingHandler

Troy Willmot edited this page Sep 25, 2016 · 1 revision

Throttled Concurrent Request Handler

Summary

The request throttling handler/filter restricts the number of concurrent requests. This can be used to prevent overloading the available network bandwidth.

Each instance of the handler/filter restricts requests independently. If two instances of the handler/filter are created (and used with different HttpClient instances) each supporting 4 concurrent requests, then the total number of allow simultaneous requests will be 8 (assuming all requests are made through one of those two handlers).

Conditional Support

Conditional logic is NOT currently supported, all requests made via through the handler/filter will be throttled if the concurrent request limit is reached.

Samples

This sample shows how to construct and use throttling handler allowing a maximum of 4 concurrent requests.

// Configure the http client
var innerHandler = new System.Net.Http.HttpClientHandler()
var handler = new Yort.Http.ClientPipeline.ThrottledConcurrentRequestHandler(4, innerHandler)
var client = new System.Net.Http.HttpClient(handler);

// Send a bunch of requests, only 4 will actually be active at a time.
// This creates 10 POST requests, then waits for all of them to complete, however
// only four requests will be active at a time.
var requestTasks = new List<Task<HttpResponseMessage>>(10);
for (int cnt = 0; cnt < 10; cnt++)
{
    requestTasks.Add(client.PostAsync("http://www.someserver.com/SomeEndpoint", 
        new StringContent("Test content")));
}
Task.WaitAll(requestTasks.ToArray());