How to remove the delay between HTTP requests when using asynchronous actions in ASP.NET?

I use HttpClient to send a GET request to the server inside the while loop

while (cycle < maxcycle) { var searchParameters = new ASearchParameters { Page = cycle++, id = getid }; var searchResponse = await Client.SearchAsync(searchParameters); } 

and SearchAsync contains

 public async Task<AuctionResponse> SearchAsync() { var uriString = "Contains a https url with parameters" var searchResponseMessage = await HttpClient.GetAsync(uriString); return await Deserialize<AuctionResponse>(searchResponseMessage); } 

The fact is that after each request there is a delay before starting the next request. you can see this on the timeline of the violinist, as well as in the violinist there is "Tunnel To" example.com-00-0043 before each request

enter image description here

Question: Why is there a delay and how to remove it?

+6
source share
2 answers

I see two things that are happening here. First, depending on the deserializer, it may take some time to translate the response back to the object. You might want to take this step and see if it is spent on most of your time. Secondly, SSL handshaking (the origin of your "tunnel") requires mutual disconnection in order to establish an SSL channel. I thought the HttpClient sent the Keep-Alive header by default, but you can see if it is A) not sent or B) rejected. If you reinstall the SSL channel for each request, which can easily take on the order of hundreds of ms separately (depending on the load on the server / network).

If you use Fiddler, you can enable the ability to check SSL traffic to find out what the request / response headers are.

+2
source

I believe that you see this delay for several reasons. Based on the code you provided, all other actions besides the request itself take a certain amount of time between requests. Therefore, deserializing the response will result in a delay.

In addition, the delay can be tied to the amount of data that is returned and processed further along the stack. I tried to recreate the script that you describe in your question with the following code:

 const int MaxNumberOfCycles = 10; static void Main() { Start().Wait(); } async Task Start() { var client = new Client(); var cycle = 0; while (cycle < MaxNumberOfCycles) { var response = await client.SearchAsync(cycle++); } } class Client { public async Task<HttpResponseMessage> SearchAsync(int n) { // parameter 'n' used to vary web service response data var url = ... // url removed for privacy using (var client = new HttpClient()) using (var response = await client.GetAsync(url)) { return response; } } } 

With small response sizes, I did not see a delay between requests. As the size of the responses increased, I began to see somewhat longer delays. Here's a screenshot for a series of queries returning 1 MB responses:

fiddler network transfer timeline graph

One thing I noticed about your scenario is that your transfer activity graph shows a solid black line at the end of each request. This line indicates "time to first byte", which means that the processing of the response did not even begin until the very end of your request.

Another problem you might think is that Fiddler causes these delays. I noticed that your answers are not passed to Fiddler, which probably affects the results. You can learn more about the response flow in Fiddler .

I hope some of this information helps ...

0
source

All Articles