Remove old abort checker and use a Node-ish mechanism to achieve the same goal

This commit is contained in:
Daniel García Aubert 2019-07-05 15:41:26 +02:00
parent dbccd762ed
commit 987f8f942a
3 changed files with 15 additions and 26 deletions

View File

@ -22,6 +22,7 @@ const rateLimitsMiddleware = require('../middlewares/rate-limit');
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitsMiddleware;
const handleQueryMiddleware = require('../middlewares/handle-query');
const logMiddleware = require('../middlewares/log');
const cancelOnClientAbort = require('../middlewares/cancel-on-client-abort');
const ONE_YEAR_IN_SECONDS = 31536000; // ttl in cache provider
const FIVE_MINUTES_IN_SECONDS = 60 * 5; // ttl in cache provider
@ -49,6 +50,7 @@ QueryController.prototype.route = function (app) {
timeoutLimitsMiddleware(this.metadataBackend),
handleQueryMiddleware(),
logMiddleware(logMiddleware.TYPES.QUERY),
cancelOnClientAbort(),
this.handleQuery.bind(this),
errorMiddleware()
];
@ -80,24 +82,6 @@ QueryController.prototype.handleQuery = function (req, res, next) {
var dp = params.dp; // decimal point digits (defaults to 6)
var gn = "the_geom"; // TODO: read from configuration FILE
req.aborted = false;
req.on("close", function() {
if (req.formatter && _.isFunction(req.formatter.cancel)) {
req.formatter.cancel();
}
req.aborted = true; // TODO: there must be a builtin way to check this
});
function checkAborted(step) {
if ( req.aborted ) {
var err = new Error("Request aborted during " + step);
// We'll use status 499, same as ngnix in these cases
// see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
err.http_status = 499;
throw err;
}
}
try {
// sanitize and apply defaults to input
@ -145,8 +129,6 @@ QueryController.prototype.handleQuery = function (req, res, next) {
function queryExplain() {
var next = this;
checkAborted('queryExplain');
var pg = new PSQL(authDbParams);
var skipCache = authorizationLevel === 'master';
@ -169,7 +151,6 @@ QueryController.prototype.handleQuery = function (req, res, next) {
req.profiler.done('queryExplain');
}
checkAborted('setHeaders');
if(!pgEntitiesAccessValidator.validate(affectedTables, authorizationLevel)) {
const syntaxError = new SyntaxError("system tables are forbidden");
syntaxError.http_status = 403;
@ -218,7 +199,6 @@ QueryController.prototype.handleQuery = function (req, res, next) {
if (err) {
throw err;
}
checkAborted('generateFormat');
// TODO: drop this, fix UI!
sql = new PSQL.QueryWrapper(sql).orderBy(orderBy, sortOrder).window(limit, offset).query();
@ -234,7 +214,6 @@ QueryController.prototype.handleQuery = function (req, res, next) {
filename: filename,
bufferedRows: global.settings.bufferedRows,
callback: params.callback,
abortChecker: checkAborted,
timeout: userLimits.timeout
};

View File

@ -0,0 +1,13 @@
'use strict';
module.exports = function cancelOnClientAbort () {
return function cancelOnClientAbortMiddleware (req, res, next) {
req.on('aborted', () => {
if (req.formatter && typeof req.formatter.cancel === 'function') {
req.formatter.cancel();
}
});
next();
};
};

View File

@ -75,9 +75,6 @@ PostgresFormat.prototype.handleQueryEnd = function(result) {
step (
function packageResult() {
if ( that.opts.abortChecker ) {
that.opts.abortChecker('packageResult');
}
that.transform(result, that.opts, this);
},
function sendResults(err, out){