I’m encountering an issue with SNAT port exhaustion in my web API written in.Net6 and hosted on an Azure Web App while making calls to another API. We’re using a custom library developed by another team within our company to handle HTTP calls, which allows us to centrally manage features like retry, circuit breaker, timeout, etc. Below is the code we’re using. I’d like to confirm whether the TCP connections are being reused, connection pooling is utilized, and connections are being properly disposed of during HTTP calls.
public class PolyHttpClient : IPolyHttpClient
{
#region Private variables
protected readonly HttpClient _httpClient;
public PolyHttpClient()
{
HttpClientHandler clientHandler = new HttpClientHandler();
if (clientHandler.SupportsAutomaticDecompression)
{
clientHandler.AutomaticDecompression = DecompressionMethods.GZip |
DecompressionMethods.Deflate;
}
clientHandler.AllowAutoRedirect = _serviceSetting.AllowAutoRedirect.Value;
clientHandler.UseCookies = false;
_httpClient = new HttpClient(clientHandler)
{
BaseAddress = new System.Uri(serviceSetting.ServiceUrl),
// provide max request timeout, it will be handled by cancellation token
Timeout = Timeout.InfiniteTimeSpan
};
}
public virtual async Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage, CancellationToken cancellationToken)
{
response = await _httpClient.SendAsync(clonedRequest, additionalRequestOptions.HttpCompletionOptionType, cancellationTokenToUse);
return response
}
}