3

I am trying to limit the number of max connections i have to an endpoint and I am doing this by setting ServicePointManager.DefaultConnectionLimit but it is not working. To test this I set up an endpoint that sleeps for 5 seconds and then returns.

Here is my code:

class Program
{
    private static Uri uri = new Uri("http://myservice.com?sleepForMillisecond=5000"));

    static void Main(string[] args)
    {
        ServicePointManager.DefaultConnectionLimit = 1;
        MainAsync().Wait();
    }

    static async Task MainAsync()
    {
        var watch = Stopwatch.StartNew();
        var tasks = new List<Task>()
            {
                Task.Run(async () => await MakeCallWithHttpClient()),
                Task.Run(async () => await MakeCallWithHttpClient()),
                Task.Run(async () => await MakeCallWithHttpClient())
            };

        await Task.WhenAll(tasks);
        watch.Stop();
        Console.WriteLine("Elapsed Time For HttpClient: " + watch.Elapsed);

        watch.Restart();
        tasks = new List<Task>()
            {
                Task.Run(async () => await MakeCallWithWebClient()),
                Task.Run(async () => await MakeCallWithWebClient()),
                Task.Run(async () => await MakeCallWithWebClient())
            };

        await Task.WhenAll(tasks);
        watch.Stop();
        Console.WriteLine("Elapsed Time For WebClient: " + watch.Elapsed);
        Console.WriteLine("Press Enter To Exit.");
        Console.ReadLine();
    }

    private static async Task MakeCallWithHttpClient()
    {
        var client = new HttpClient();
        await client.GetStringAsync(uri);
        Console.WriteLine("Done");
    }

    private static async Task MakeCallWithWebClient()
    {
        var client = new WebClient();
        await client.DownloadStringTaskAsync(uri);
        Console.WriteLine("Done");
    }
} 

Below is the output:

Done
Done
Done
Elapsed Time For HttpClient: 00:00:05.0953943
Done
Done
Done
Elapsed Time For WebClient: 00:00:15.0450096

As you can see the async HttpClient is not limited by the max connections of 1 since making 3 calls in parallel takes 5 seconds and the async WebClient is limited by the 1 connection since making 3 calls in parallel takes 15 seconds.

My question is...How do I limit the max connections using the asynchronous HttpClient?

Thanks.

Matt
  • 334
  • 5
  • 15
  • To become enlightened have a read of this interesting SO post... http://stackoverflow.com/questions/16194054/is-async-httpclient-from-net-4-5-a-bad-choice-for-intensive-load-applications async or multithreaded? – Paul Zahra Jul 23 '14 at 08:00

1 Answers1

0

Use the same HttpClient instance for all requests and the connection limit will work as expected.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • I'm using the same instance of httpclient however I don't see it makes any effect. Any hints ? – Tomasz Jaskuλa Dec 22 '16 at 14:10
  • @TomaszJaskuλa When you make multiple requests, how many connections are being opened? What is your DefaultConnectionLimit set to? – Darrel Miller Dec 22 '16 at 14:24
  • 1
    I was doing for tests 1000 async calls on the same HttpClient instance. Setting DefaultConnectionLimit to 5 (or 100) changed nothing. I had something like 300 TCP connections opened. However this worked for me new HttpClient(new HttpClientHandler { MaxConnectionsPerServer = 5 }). This worked and I had only 5 connections opened for 1000 calls. – Tomasz Jaskuλa Dec 22 '16 at 15:23