From 01853eef87357e8c4df057a3c9a003b2e3784685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20=C5=BDaromskis?= Date: Mon, 27 Aug 2018 17:38:54 +0300 Subject: [PATCH 1/4] Add read_timeout to connection settings --- lib/client.js | 30 ++++++++++++++++++++++++++++++ lib/connection-parameters.js | 1 + lib/defaults.js | 5 ++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 0d023db7c..193628f79 100644 --- a/lib/client.js +++ b/lib/client.js @@ -352,6 +352,10 @@ Client.prototype.query = function (config, values, callback) { // can take in strings, config object or query object var query var result + var readTimeoutTimer + var queryCallback + var readTimeout = config.query_timeout || this.connectionParameters.query_timeout + if (typeof config.submit === 'function') { result = query = config if (typeof values === 'function') { @@ -369,6 +373,32 @@ Client.prototype.query = function (config, values, callback) { } } + if (readTimeout) { + queryCallback = query.callback; + + readTimeoutTimer = setTimeout(() => { + var error = new Error('Query read timeout'); + + this.emit('error', error) + queryCallback(error) + + // we already returned an error, + // just do nothing when query completes + delete query.callback + + // Remove from queue + var index = this.queryQueue.indexOf(query) + if (index > -1) { + this.queryQueue.splice(index, 1) + } + }, readTimeout) + + query.callback = (err, res) => { + clearTimeout(readTimeoutTimer) + queryCallback(err, res) + } + } + if (this.binary && !query.binary) { query.binary = true } diff --git a/lib/connection-parameters.js b/lib/connection-parameters.js index f31f28dd3..782473c91 100644 --- a/lib/connection-parameters.js +++ b/lib/connection-parameters.js @@ -65,6 +65,7 @@ var ConnectionParameters = function (config) { this.application_name = val('application_name', config, 'PGAPPNAME') this.fallback_application_name = val('fallback_application_name', config, false) this.statement_timeout = val('statement_timeout', config, false) + this.query_timeout = val('query_timeout', config, false) } // Convert arg to a string, surround in single quotes, and escape single quotes and backslashes diff --git a/lib/defaults.js b/lib/defaults.js index 30214b1d8..6b0a98f1b 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -55,7 +55,10 @@ module.exports = { parseInputDatesAsUTC: false, // max milliseconds any query using this connection will execute for before timing out in error. false=unlimited - statement_timeout: false + statement_timeout: false, + + // max miliseconds to wait for query to complete (client side) + query_timeout: false } var pgTypes = require('pg-types') From 7507138f7b1e7d031ee0af857d51291b581e4fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20=C5=BDaromskis?= Date: Tue, 28 Aug 2018 11:43:13 +0300 Subject: [PATCH 2/4] Fix uncaught error issue --- lib/client.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index 193628f79..c72e2a236 100644 --- a/lib/client.js +++ b/lib/client.js @@ -377,20 +377,22 @@ Client.prototype.query = function (config, values, callback) { queryCallback = query.callback; readTimeoutTimer = setTimeout(() => { - var error = new Error('Query read timeout'); + var error = new Error('Query read timeout') this.emit('error', error) queryCallback(error) // we already returned an error, - // just do nothing when query completes - delete query.callback + // just do nothing if query completes + query.callback = () => {} // Remove from queue var index = this.queryQueue.indexOf(query) if (index > -1) { this.queryQueue.splice(index, 1) } + + this._pulseQueryQueue() }, readTimeout) query.callback = (err, res) => { From dc12e9aa1cbf347c6d3da016216f85fb6284fc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20=C5=BDaromskis?= Date: Tue, 28 Aug 2018 14:50:44 +0300 Subject: [PATCH 3/4] Fix lint --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index c72e2a236..cd2c485fa 100644 --- a/lib/client.js +++ b/lib/client.js @@ -374,7 +374,7 @@ Client.prototype.query = function (config, values, callback) { } if (readTimeout) { - queryCallback = query.callback; + queryCallback = query.callback readTimeoutTimer = setTimeout(() => { var error = new Error('Query read timeout') From 404cf92b4dbf1c786aaf0069517a6df16c6c384b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20=C5=BDaromskis?= Date: Tue, 28 Aug 2018 16:19:29 +0300 Subject: [PATCH 4/4] Fix "queryCallback is not a function" --- lib/client.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/client.js b/lib/client.js index cd2c485fa..040b3a04f 100644 --- a/lib/client.js +++ b/lib/client.js @@ -380,11 +380,15 @@ Client.prototype.query = function (config, values, callback) { var error = new Error('Query read timeout') this.emit('error', error) - queryCallback(error) - // we already returned an error, - // just do nothing if query completes - query.callback = () => {} + if (typeof queryCallback === 'function') { + queryCallback(error) + + // we already returned an error, + // just do nothing if query completes + query.callback = () => { + } + } // Remove from queue var index = this.queryQueue.indexOf(query)