穆罕默德 - Moh
穆罕默德 - Moh

Reputation: 1373

What does HttpClient do in face of concurrent request exactly at the same time?

As we know it's best practice to use HttpClient in a shared state (Singletone) in Dotnet core instead of creating and dispose of it for each request. We do it to prevent the Port Exhaustion problem.(More Info here).

My problem is What does HttpClient do in face of concurrent requests exactly at the same time when it is shared?

For Example, assume that Request A and Request B sent to a shared HttpClient object at the same time.

Is any of this assumption correct? If no Any other Idea?

Upvotes: 0

Views: 1360

Answers (1)

Ibrahim Abdelkareem
Ibrahim Abdelkareem

Reputation: 957

It's safe to say that your first assumption is incorrect and the second assumption is closer to the truth. each HttpClient instance manages its own connection pool to execute requests concurrently with int.MaxValue as a default value for the number of concurrent connections per server.

Meaning that HttpClient will execute all requests concurrently. Every time you send a new request it'll try to use an existing connection to the server (if it's not in use) otherwise it'll open a new connection to execute the request.

There's an excellent article explaining this in details here:

https://www.stevejgordon.co.uk/httpclient-connection-pooling-in-dotnet-core

Upvotes: 1

Related Questions