I'm running Express NodeJS in GCP App Engine Standard, with MongoDB hosted in Atlas. My observation is when I declare the Mongoose Connection globally with no explicit Close, the connections keep mounting up until it hits the limit of 100.
My assumption is that, since this is GCP App Engine Standard which is running by Instance, each Method call might be running its own Instance and connection is not pooling. So instead of Global MongoDB Connection, each method will have its local connection instead.
So then code was updated to the following:
router.post('/subscribe',function(req,res,next){
var email = req.body.email
if(email&&email!=''){
let mongoose = require('mongoose')
mongoose.connect(process.env.MONGODBHOST).then(resp=>{
Subscribers.findOne({"email":email},function(error,results){
if(!results){
var newSubscriber = new Subscribers({
email: email
})
newSubscriber.save().then(resp=>{
console.log('Subscriber added')
if(mongoose.connection){
mongoose.connection.close(true).then(r=>{}).catch(e=>{})
}
res.status(200).send({ success: true, message: 'Added' })
}).catch(err=>{
console.log(err)
if(mongoose.connection){
mongoose.connection.close(true).then(r=>{}).catch(e=>{})
}
res.status(200).send({ success: false, message: err })
})
} else {
console.log('Subscriber existing')
if(mongoose.connection){
mongoose.connection.close(true).then(r=>{}).catch(e=>{})
}
res.status(200).send({ success: true, message: 'Existing' })
}
})
}).catch(err=>{
res.status(200).send({ success: false, message: 'DB connection failed' })
})
} else {
console.log('No email found')
res.status(200).send({ success: false, message: 'No parameter found' })
}
})
Another method also exists that takes relatively longer than the method above
router.post('/getDetails',function(req,res,next){
let mongoose = require('mongoose')
mongoose.connect(process.env.MONGODBHOST).then(resp=>{
Transactions.find({"active":true},function(error,results){
if(!results){
if(mongoose.connection){
mongoose.connection.close(true).then(r=>{}).catch(e=>{})
}
res.status(200).send({ success: false, message: err })
} else {
if(mongoose.connection){
mongoose.connection.close(true).then(r=>{}).catch(e=>{})
}
res.status(200).send({ success: true, message: 'Existing' })
}
})
}).catch(err=>{
res.status(200).send({ success: false, message: 'DB connection failed' })
})
})
If run independently, it works fine, but sometimes, if run at exactly the same time, the second function sometimes throws an error that says: "Pool force destroyed"
For some reason, it seems like the Connection.close() from Function1 affected the MongoDB connection in GetDetails even though Mongoose is local to each Method.
Please advise what is the best approach in this set up.
Thanks!