Kitwradr
Kitwradr

Reputation: 2178

Making bulk REST calls using HttpClient without waiting for previous request to finish

I have a bunch of independent REST calls to make (say 1000) , each call has differing body. How to make these calls in the least amount of time?

I am using a Parallel.foreach loop to to make the calls , but doesn't a call wait for the previous call to finish (on a single thread) , is there any callback kind of system to prevent this and make the process faster?

Parallel.foreach(...){
    (REST call)
    HttpResponseMessage response = this.client.PostAsync(new Uri(url), content).Result;
}

Using await also gives almost same results.

Upvotes: 1

Views: 1423

Answers (2)

Dennis Tretyakov
Dennis Tretyakov

Reputation: 31

The simplest way to do many async actions in parallel, is to start them without waiting, capture tasks in a collection and then wait untill all tasks will be completed.

For example

var httpClient = new HttpClient();
var payloads = new List<string>(); // this is 1000 payloads

var tasks = payloads.Select(p => httpClient.PostAsync("https://addresss", new StringContent(p)));

await Task.WhenAll(tasks);

It should be enough for start, but mind 2 things.

  1. There is still a connection pool per hostname, what defaults to 4. You can use HttpSocketsHandler to control the pool size.

  2. It will really start or the 1000 items in parallel, what sometimes might be not what you want. To control MAX amount of parallel items you can check ActionBlock

Upvotes: 1

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

Make all the calls with PostAsync:

var urlAndContentArray = ...

// The fast part
IEnumerable<Task<HttpResponseMessage>> tasks = urlAndContentArray.Select
      (x => this.client.PostAsync(new Uri(x.Url), x.Content));

// IF you care about results: here is the slow part:
var responses = await Task.WhenAll(tasks);

Note that this will make all the calls very quickly as requested, but indeed time it takes to get results is mostly not related to how many requests you send out - it's limited by number of outgoing requests .Net will run in parallel as well as how fast those servers reply (and if they have any DOS protection/throttling).

Upvotes: 4

Related Questions