Reputation: 163
My function app is using huge HTTP requests frequently. And so I got an error following
Only one usage of each socket address is normally permitted
And function app will automatically shut down immediately for a few minutes after that. I searched about this and understood that Opening a new connection in each restSharp request caused that. And the better solution is to open only one HttpClient as static. So I changed the function to use a static HttpClient for all requests.
Now I want to know this change is working fine or not. I checked connections in the azure matrics but it always showing count "30". But it was the same in the case of RestSharp also. Somebody please help me to understand analyzing connections graph. See the image
Any help is appreciated..
Upvotes: 1
Views: 1741
Reputation: 4870
As far as the error message is concerned, if you have only 30 connections, it should not be an issue according to this article, until and unless you have some configuration set to 30 in Function App's host.json.
Coming to creation of static instance of HttpClient, if not already using, you can use DI to inject singleton instance of HttpClient
in FunctionsStartup
-
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
namespace MyNamespace
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton<IHttpClient, HttpClient>();
}
}
}
Then, use your Client like below:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Net.Http;
using System.Threading.Tasks;
namespace MyNamespace
{
public class MyHttpTrigger
{
private readonly IHttpClient _client;
public MyHttpTrigger(IHttpClient httpClient)
{
this._client = httpClient;
}
[FunctionName("MyHttpTrigger")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
var response = await _client.GetAsync("https://microsoft.com");
return new OkObjectResult("Response from function with injected dependencies.");
}
}
}
For complete implementation and other details, you can visit - https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
For information about the metrics of Azure App service plans, you can refer this doc.
It depends on what level you have made the HttpClient static, it is always a recommended practice to use DI for a singleton scenario.
As per the design of Function App, it scales out automatically, even during periods of high load. I am not sure about the first graph as it does not reflect aggregation. The second graph shows the average of the number of bound sockets existing in the sandboxes (w3wp.exe and its child processes), and as you mentioned and I understand there are huge outbound Http requests in your function app and they are gradually increasing, if this is the case when there are highest outbound requests (137 as per graph), then your graph of Azure Function App looks good. Please note that the limit of Connections for each sand box of Free/Shared/Consumption is 600 and Basic+ Limit is Unlimited (VM limit still applies). If your outbound Http requests are going beyond these limits, then you will have to redesign Function App in such a way that it keeps the connections within these limits.
If you want to dig deeper in future, you can refer to this document for detailed information about Azure Function App Sandbox. This document is maintained by PG regularly.
Upvotes: 1