Prerequisites
馃殌 Feature Proposal
We've done some complex connection management, and wanted to share some patterns we used.
For example, if you have an Express or Koa app, such as app.js, you can set it up such as:
For single connection:
const express = require('express');
const mongoose = require('mongoose');
const Graceful = require('graceful');
const pRetry = require('p-retry'); // use v4.x for CJS and v5.x+ for ESM
const getPort = require('get-port'); // use v5.x for CJS and v6.x+ for ESM
try {
const app = express();
const port = await getPort();
const server = await new Promise((resolve, reject) => {
app.listen(port, function (err) {
if (err) return reject(err);
resolve(this);
});
});
const graceful = new Graceful({ servers: [server] });
graceful.listen();
console.log('express app started on %d', port);
//
// this will attempt to reconnect with exponential backoff up to 10x
// however if there is not a MongooseServerSelectionError, it will throw
// (this is useful to keep http server running and retry db connection)
// (apps such as pm2 are recommended to cause app to reboot if this eventually throws)
//
await pRetry(
() => mongoose.connection.asPromise(),
{
// <https://github.com/sindresorhus/p-retry/issues/58>
// <https://github.com/tim-kos/node-retry/issues/84>
// forever: true,
// retries: Infinity,
onFailedAttempt(err) {
console.error(err);
if (!(err instanceof mongoose.Error.MongooseServerSelectionError))
throw err;
}
}
);
} catch (err) {
console.error(err);
process.exit(1);
}
For multiple connections:
const express = require('express');
const mongoose = require('mongoose');
const Graceful = require('graceful');
const pRetry = require('p-retry'); // use v4.x for CJS and v5.x+ for ESM
const getPort = require('get-port'); // use v5.x for CJS and v6.x+ for ESM
try {
const app = express();
const port = await getPort();
const server = await new Promise((resolve, reject) => {
app.listen(port, function (err) {
if (err) return reject(err);
resolve(this);
});
});
const graceful = new Graceful({ servers: [server] });
graceful.listen();
console.log('express app started on %d', port);
//
// this will attempt to reconnect with exponential backoff up to 10x
// however if there is not a MongooseServerSelectionError, it will throw
// (this is useful to keep http server running and retry db connection)
// (apps such as pm2 are recommended to cause app to reboot if this eventually throws)
//
await pRetry(
() => Promise.all(mongoose.connections.map((connection) => connection.asPromise())),
{
// <https://github.com/sindresorhus/p-retry/issues/58>
// <https://github.com/tim-kos/node-retry/issues/84>
// forever: true,
// retries: Infinity,
onFailedAttempt(err) {
console.error(err);
if (!(err instanceof mongoose.Error.MongooseServerSelectionError))
throw err;
}
}
);
} catch (err) {
console.error(err);
process.exit(1);
}
Did you want to put this in documentation and as an example somewhere for users? If so let us know where - or feel free to add it wherever you deem best fit!
Motivation
Lack of real documentation and examples surrounding connection on application boot with serverSelectionTimeoutMS and how to properly handle it in real-world Express, Koa, Fastify, etc.
Prerequisites
馃殌 Feature Proposal
We've done some complex connection management, and wanted to share some patterns we used.
For example, if you have an Express or Koa app, such as
app.js, you can set it up such as:For single connection:
For multiple connections:
Did you want to put this in documentation and as an example somewhere for users? If so let us know where - or feel free to add it wherever you deem best fit!
Motivation
Lack of real documentation and examples surrounding connection on application boot with
serverSelectionTimeoutMSand how to properly handle it in real-world Express, Koa, Fastify, etc.