Skip to content

Commit 4bc19e6

Browse files
juliuszaedevil
authored andcommitted
Add read_timeout to connection settings
1 parent a3295b4 commit 4bc19e6

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

lib/client.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ Client.prototype.query = function (config, values, callback) {
403403
if (config === null || config === undefined) {
404404
throw new TypeError('Client was passed a null or undefined query')
405405
} else if (typeof config.submit === 'function') {
406+
var readTimeoutTimer
407+
var queryCallback
408+
var readTimeout = config.query_timeout || this.connectionParameters.query_timeout
409+
406410
result = query = config
407411
if (typeof values === 'function') {
408412
query.callback = query.callback || values
@@ -416,6 +420,32 @@ Client.prototype.query = function (config, values, callback) {
416420
}
417421
}
418422

423+
if (readTimeout) {
424+
queryCallback = query.callback;
425+
426+
readTimeoutTimer = setTimeout(() => {
427+
var error = new Error('Query read timeout');
428+
429+
this.emit('error', error)
430+
queryCallback(error)
431+
432+
// we already returned an error,
433+
// just do nothing when query completes
434+
delete query.callback
435+
436+
// Remove from queue
437+
var index = this.queryQueue.indexOf(query)
438+
if (index > -1) {
439+
this.queryQueue.splice(index, 1)
440+
}
441+
}, readTimeout)
442+
443+
query.callback = (err, res) => {
444+
clearTimeout(readTimeoutTimer)
445+
queryCallback(err, res)
446+
}
447+
}
448+
419449
if (this.binary && !query.binary) {
420450
query.binary = true
421451
}

lib/connection-parameters.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var ConnectionParameters = function (config) {
6565
this.application_name = val('application_name', config, 'PGAPPNAME')
6666
this.fallback_application_name = val('fallback_application_name', config, false)
6767
this.statement_timeout = val('statement_timeout', config, false)
68+
this.query_timeout = val('query_timeout', config, false)
6869
}
6970

7071
// Convert arg to a string, surround in single quotes, and escape single quotes and backslashes

lib/defaults.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ module.exports = {
5555
parseInputDatesAsUTC: false,
5656

5757
// max milliseconds any query using this connection will execute for before timing out in error. false=unlimited
58-
statement_timeout: false
58+
statement_timeout: false,
59+
60+
// max miliseconds to wait for query to complete (client side)
61+
query_timeout: false
5962
}
6063

6164
var pgTypes = require('pg-types')

0 commit comments

Comments
 (0)