Thomas Segato
Thomas Segato

Reputation: 5221

Avoid - An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full

I get following error from an Azure Function App when using cosmos DB. I have got the same with HttpClient but seemed to solve that by doing HttpClient static. Can you solve the same problem just by making the CosmosDB client static? Something like:

public class DocRepoCoach
{
    public string ConnStr { get; set; }

    public Container XX1Container { get; set; }
    public Container XX2Container { get; set; }
    **public static CosmosClient Client { get; set; }**

    public DocRepoCoach(string connectionString)
    {
        ConnStr = connectionString;
        var options = new CosmosClientOptions() { AllowBulkExecution = true, MaxRetryAttemptsOnRateLimitedRequests = 1000 };
        Client = new CosmosClient(ConnStr, options);
        XX1Container = Client.GetContainer("XXXAPI", "XX");
        XX2Container = Client.GetContainer("XXXAPI", "XX");
    }
}

Upvotes: 2

Views: 3659

Answers (1)

Kalyan Chanumolu-MSFT
Kalyan Chanumolu-MSFT

Reputation: 1133

Yes, please make it static. The recommended practice with Azure functions is to use a Singleton client for the lifetime of your application. The CosmosClient can manage connections when you use a static client.

Below are the recommendations

  • Do not create a new client with every function invocation.
  • Do create a single, static client that every function invocation can use.
  • Consider creating a single, static client in a shared helper class if different functions use the same service.

These are also documented here on Azure docs

Upvotes: 3

Related Questions