Each my sandbox process connects to mongodb with mongoose.
How to connect and disconnect correctly? Should I disconnect when job done or anywhere else?
I have 4 workers with concurrency - 1, 1, 5, 5. And the number of db connections sometimes reaches 55. I don't know why.
connection on each process.js
mongoose.connect(url, {
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false
});
app.js on shutdown:
process.on("SIGINT", async function() {
// ...
// array of queues with .close() function
await Promise.all(queuesCloses);
// close all mongoose connections
mongoose.disconnect();
process.exit(0);
});
3.8.1
Mongoose does connection pooling by default it will open up to 5 connections per process so you could really see up to (1 + 1 + 5 + 5) * 5 = 48 for just the workers.
I would consider setting the pool size for workers to 1 since each sandboxed process will only run one task at a time.
also consider mongoose.disconnect(); should have an await
Most helpful comment
Mongoose does connection pooling by default it will open up to 5 connections per process so you could really see up to
(1 + 1 + 5 + 5) * 5 = 48for just the workers.I would consider setting the pool size for workers to 1 since each sandboxed process will only run one task at a time.
also consider
mongoose.disconnect();should have anawait