简体   繁体   English

Mongo DB Connection 会自动关闭或过期吗

[英]Will Mongo DB Connection closes or expires automatically

I have written a Queue Trigger Azure Function App(Node JS) where on each queue trigger data will be inserted into MongoDB.我编写了一个队列触发器 Azure Function 应用程序(节点 JS),其中每个队列触发器数据将插入 MongoDB。 I am creating MongoClient above function level and re-using same MongoClient for all the Triggers我正在创建高于 function 级别的 MongoClient 并为所有触发器重新使用相同的 MongoClient

if(mongoClient.topology.isConnected())
      //Use Same Connection
    else //Creating new client
      mongoClient = await mongoDB.MongoClient.connect();

Sometimes on my mongo db cluster i am getting error connections to your cluster(s) have exceeded i dont understand is it because i am keeping connection open for too long?有时在我的 mongo db 集群上,我收到与集群的错误连接已超出我不明白是因为我保持连接打开时间过长吗? will connection automatically expire after sometime?连接会在一段时间后自动过期吗? Is it good to keep Client Connection above function level and reuse it?将客户端连接保持在 function 级别并重用它是否很好? Can some one suggest please.有人可以建议吗。

If i do open and close connection at function level then i am getting another error in function Cannot use Session that has ended如果我在 function 级别打开和关闭连接,那么我在 function 中遇到另一个错误,无法使用 Session 已经结束

If you've deployed the Function App in Consumption Plan, then the number of outbound connections is limited (~600/instance) but you'll get the connections exceeded when you exceed the limit.如果您在消费计划中部署了 Function 应用程序,则出站连接数是有限的(~600/实例),但是当您超过限制时,您将获得超过连接数。

I would suggest enabling the Application Insights on the Function App to track the requests time, response time and other metrics that helps to troubleshoot more.我建议在 Function 应用程序上启用 Application Insights 来跟踪请求时间、响应时间和其他有助于排除更多故障的指标。

Is it good to keep Client Connection above function level and reuse it?将客户端连接保持在 function 级别并重用它是否很好?

Yes, you can keep client connections above function level and reuse them instead of creating new connection whatever the client connection it is Http Client, Document Client or Database client.是的,您可以将客户端连接保持在 function 级别之上并重用它们,而不是创建新连接,无论客户端连接是 Http 客户端、文档客户端还是数据库客户端。

  • Do not create a new client with every function invocation.不要在每个 function 调用中创建新客户端。 Do create a single, static client that every function invocation can use.创建单个 static 客户端,每个 function 调用都可以使用该客户端。 Consider creating a single, static client in a shared helper class if different functions use the same service.如果不同的功能使用相同的服务,请考虑在共享帮助程序 class 中创建单个 static 客户端。

Refer to MSFT Doc of Azure Functions Client Connections regarding the best practices when managing the client connections in Function Instances.有关在 Function 实例中管理客户端连接的最佳实践,请参阅 Azure 功能客户端连接的MSFT 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM