I have Azure function app on dedicated premium App service plan. Function in one execution has to delivery two times some data to one api's endpoint. For sending data, I use http client which is provided from IHttpClientFactory. So, httpClientFactory.CreateClient is invoked twice in one function execution. Sometimes, I have a big traffic in small frame, 100k executions in 20min. I noticed that Socket Connections for outbound requests are over 1000, so in my view, it is weird, because I thought that IHttpClientFactory should reuse socket connections better, particular that, it is always the same endpoint. I am afraid that it can cause socket exhaustion.
How to avoid too many established Socket Connections?
Do you have some experience with MaxConnectionsPerServer handler's property? It seems that it can set a established connections limit.
I don't want to use static httpClient instance, because it can cause other issue (Dns refreshing)
When dealing with a large number of outbound requests in a short period of time, it’s important to manage and optimize the usage of HttpClient instances to avoid issues such as socket exhaustion.
Although you mentioned that you don’t want to use a static HttpClient instance, it’s generally recommended to reuse instances to take advantage of connection pooling. However, if you’re concerned about issues like DNS refreshing, you can use a named client with a longer lifetime, allowing for better reuse.
You can set how many minutes you want as the HttpMessageHandler objects lifetime.
MaxConnectionsPerServerproperty to limit the number of concurrent connections to a particular server. This can help prevent socket exhaustion.Refer to this Microsoft GitHub Article on using the
MaxConnectionsPerServerproperty.3. If the default connection pooling behavior isn’t sufficient, you can implement a custom connection pooling strategy which involves managing the lifecycle of HttpClient instances manually, ensuring reuse and proper disposal.
4. Tools like Azure Application Insights or Azure Monitor can help with performance monitoring to adjust the connection pooling settings based on the observed behaviour of your application under load.