2

I have a node.js app running on heroku backed by a Mongo db which breaks down like this:

Node app connects to db and stores db and collection into "top level" variables (not sure if Global is the right word)

App iterates through each document in the db using the foreach() function in node mongo driver.

Each iteration sends the document id to another function that uses the id to access fields on that document and take actions based on that data. In this case its making requests against api's from amazon and walmart getting updated pricing info. This function is also being throttled so as not to make too many requests too quickly.

My question is this, how can I know its safe to close the db connection. My best idea is to get a count of the documents, multiply that by the number of external api hits per document and then increment a variable by one each time a api transaction finishes and then test that number against the total number expected and if it hits that close the connection. This sounds so hackish there has to be a better way. Any ideas?

nickthedude
  • 4,925
  • 10
  • 36
  • 51
  • 2
    Since all MongoDB drivers maintain a connection pool, the connection should be established during application startup. You shouldn't need to close MongoDB connections manually. See http://stackoverflow.com/questions/10656574/how-to-manage-mongodb-connections-in-a-nodejs-weapp for details. – Markus W Mahlberg Jun 01 '15 at 19:22
  • I saw that question and changed my code accordingly previous to posting this, but I did not see anything about the connection closing automatically. If that is the case I guess problem solved. Thanks for the comment. – nickthedude Jun 01 '15 at 23:45

0 Answers0