简体   繁体   中英

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. I am creating MongoClient above function level and re-using same MongoClient for all the Triggers

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? will connection automatically expire after sometime? Is it good to keep Client Connection above function level and reuse it? 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

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.

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.

Is it good to keep Client Connection above function level and reuse it?

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.

  • 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.

Refer to MSFT Doc of Azure Functions Client Connections regarding the best practices when managing the client connections in Function Instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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