Prevent HttpClient throttling

773 views Asked by At

I need to perform many parallel requests to an HTTP microservice, but it seems HttpClient is being throttled, and although I have find other posts about how to disable such throttling, it does not seem to work, tweaking ServicePointManager.DefaultConnectionLimit has no effect.

This small application shows me that there is a limitation around 10 parallel requests, causing big delays on some of the requests.

In the first batch of 20, there are is massive gap around the half of the batch. In subsequent batches it gets more uniform, but still there is a big difference between the fastest and the slowest.

static void Main(string[] args)
{
    ServicePointManager.DefaultConnectionLimit = 500;
    ServicePointManager.UseNagleAlgorithm = false;

    var sp = ServicePointManager.FindServicePoint(new Uri("http://google.ie/"));
    sp.ConnectionLimit = 500;
    sp.MaxIdleTime = 20 * 1000;

    using (var client = new HttpClient(new HttpClientHandler() { UseProxy = false , Proxy = null}, true))
    {
        client.BaseAddress = new Uri("http://google.ie/");

        while (true)
        {
            var tasks = Enumerable
                            .Range(0, 20)
                            .Select(i => PerformCall(client))
                            .ToArray();

            Task.WaitAll(tasks);
            Console.WriteLine();
            Console.ReadKey(true);
        }

    }

    Console.WriteLine("End");
    Console.ReadKey(true);
}

static int counter = 0;
static async Task PerformCall(HttpClient client)
{
    var sw = new Stopwatch();
    sw.Start();
    var response = await client.GetAsync("", HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);
    sw.Stop();
    Console.WriteLine($"{Interlocked.Increment(ref counter).ToString("00")} {sw.ElapsedMilliseconds} {response.StatusCode}");
}

Results vary, but I usually get something like:

20:34:02.110
01 153 OK
02 156 OK
03 163 OK
04 181 OK
05 184 OK
06 185 OK
07 189 OK
08 200 OK
09 351 OK
10 362 OK
11 363 OK
12 370 OK
13 402 OK
14 402 OK
15 3127 OK
16 3128 OK
17 3131 OK
18 3138 OK
19 3158 OK
20 3292 OK

20:34:10.525
21 77 OK
22 85 OK
23 86 OK
24 86 OK
25 87 OK
26 104 OK
27 156 OK
28 156 OK
29 156 OK
30 156 OK
31 175 OK
32 175 OK
33 175 OK
34 205 OK
35 263 OK
36 298 OK
37 308 OK
38 312 OK
39 362 OK
40 453 OK

20:34:18.141
41 69 OK
42 78 OK
43 79 OK
44 85 OK
45 91 OK
46 92 OK
47 94 OK
48 101 OK
49 103 OK
50 104 OK
51 105 OK
52 111 OK
53 300 OK
54 308 OK
55 312 OK
56 312 OK
57 313 OK
58 313 OK
59 327 OK
60 332 OK

Is there a way to avoid this throttling somehow?

0

There are 0 answers