Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timeout to mongoldb connections #15258

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions website/common/script/libs/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ export class TooManyRequests extends CustomError {
}
}

export class RequestTimeout extends CustomError {
constructor (customMessage) {
super();
this.name = this.constructor.name;
this.httpCode = 408;
this.message = customMessage || 'The request timed out.';
}
}
export class NotImplementedError extends CustomError {
constructor (str) {
super();
Expand Down
13 changes: 13 additions & 0 deletions website/server/libs/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ export const { Forbidden } = common.errors;
*/
export const { TooManyRequests } = common.errors;

/**
* @apiDefine RequestTimeout
* @apiError RequestTimeout The request took too long to complete.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 408 RequestTimeout
* {
* "error": "RequestTimeout",
* "message": "Access forbidden."
* }
*/
export const { RequestTimeout } = common.errors;

/**
* @apiDefine NotificationNotFound
* @apiError NotificationNotFound The notification was not found.
Expand Down
2 changes: 2 additions & 0 deletions website/server/libs/setupMongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {
const IS_PROD = nconf.get('IS_PROD');
const MAINTENANCE_MODE = nconf.get('MAINTENANCE_MODE');
const POOL_SIZE = nconf.get('MONGODB_POOL_SIZE');
const SOCKET_TIMEOUT = nconf.get('MONGODB_SOCKET_TIMEOUT');

// Do not connect to MongoDB when in maintenance mode
if (MAINTENANCE_MODE !== 'true') {
const mongooseOptions = getDefaultConnectionOptions();

if (POOL_SIZE) mongooseOptions.maxPoolSize = Number(POOL_SIZE);
if (SOCKET_TIMEOUT) mongooseOptions.socketTimeoutMS = Number(SOCKET_TIMEOUT);

const DB_URI = nconf.get('IS_TEST') ? nconf.get('TEST_DB_URI') : nconf.get('NODE_DB_URI');
const connectionUrl = IS_PROD ? DB_URI : getDevelopmentConnectionUrl(DB_URI);
Expand Down
5 changes: 5 additions & 0 deletions website/server/middlewares/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CustomError,
BadRequest,
InternalServerError,
RequestTimeout,
} from '../libs/errors';

export default function errorHandler (err, req, res, next) { // eslint-disable-line no-unused-vars
Expand Down Expand Up @@ -46,6 +47,10 @@ export default function errorHandler (err, req, res, next) { // eslint-disable-l
}));
}

if (err.name === 'MongoNetworkTimeoutError') {
responseErr = new RequestTimeout();
}

// Handle Stripe Card errors errors (can be safely shown to the users)
// https://stripe.com/docs/api/node#errors
if (err.type === 'StripeCardError') {
Expand Down
Loading