首页 > 解决方案 > 避免 - 由于系统缺少足够的缓冲区空间或队列已满,因此无法对套接字执行操作

问题描述

使用 cosmos DB 时,我从 Azure Function App 收到以下错误。我对 HttpClient 也有同样的看法,但似乎通过做 HttpClient static 来解决这个问题。你能通过将 CosmosDB 客户端设为静态来解决同样的问题吗?就像是:

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");
    }
}

标签: azure-cosmosdbazure-function-app

解决方案


是的,请将其设为静态。Azure 函数的推荐做法是在应用程序的整个生命周期内使用 Singleton 客户端。当您使用静态客户端时,CosmosClient 可以管理连接。

以下是建议

  • 不要在每次函数调用时都创建一个新客户端。
  • 请创建一个每个函数调用都可以使用的单个静态客户端。
  • 如果不同的功能使用相同的服务,请考虑在共享帮助程序类中创建单个静态客户端。

这些也记录Azure 文档中


推荐阅读