Extract socket timeout middleware

This commit is contained in:
Daniel García Aubert 2019-10-01 15:36:04 +02:00
parent f9c467a628
commit c60a44252d
3 changed files with 22 additions and 11 deletions

View File

@ -17,10 +17,11 @@ const QueryController = require('./query_controller');
const CopyController = require('./copy_controller');
const JobController = require('./job_controller');
const cors = require('../middlewares/cors');
const servedByHostHeader = require('../middlewares/served-by-host-header');
const socketTimeout = require('../middlewares/socket-timeout');
const logger = require('../middlewares/logger');
const profiler = require('../middlewares/profiler');
const cors = require('../middlewares/cors');
const servedByHostHeader = require('../middlewares/served-by-host-header');
module.exports = class ApiRouter {
constructor ({ redisPool, metadataBackend, statsClient, dataIngestionLogger }) {
@ -68,6 +69,7 @@ module.exports = class ApiRouter {
route (app) {
const apiRouter = router({ mergeParams: true });
apiRouter.use(socketTimeout());
apiRouter.use(logger());
apiRouter.use(profiler({ statsClient: this.statsClient }));
apiRouter.use(cors());

View File

@ -0,0 +1,18 @@
'use strict';
module.exports = function socketTimeout () {
if (!global.settings.hasOwnProperty('node_socket_timeout')) {
return function dummySocketTimeoutMiddleware (req, res, next) {
next();
};
}
const timeout = parseInt(global.settings.node_socket_timeout);
return function socketTimeoutMiddleware (req, res, next) {
// Set connection timeout
req.connection.setTimeout(timeout);
next();
}
}

View File

@ -74,15 +74,6 @@ function App(statsClient) {
mkdirp.sync(global.settings.tmpDir);
}
// Set connection timeout
if (global.settings.hasOwnProperty('node_socket_timeout')) {
var timeout = parseInt(global.settings.node_socket_timeout);
app.use(function(req, res, next) {
req.connection.setTimeout(timeout);
next();
});
}
app.enable('jsonp callback');
app.set("trust proxy", true);
app.disable('x-powered-by');