diff --git a/.gitignore b/.gitignore index 88b672dd..439db5fd 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ build/ node_modules/ package-lock.json *.swp +/.idea diff --git a/lib/client.js b/lib/client.js index 2016a6ff..3228571d 100644 --- a/lib/client.js +++ b/lib/client.js @@ -269,6 +269,9 @@ Client.prototype.getStartupConf = function () { if (params.replication) { data.replication = '' + params.replication } + if (params.statement_timeout) { + data.statement_timeout = String(parseInt(params.statement_timeout, 10)) + } return data } diff --git a/lib/connection-parameters.js b/lib/connection-parameters.js index 2ba409c2..f31f28dd 100644 --- a/lib/connection-parameters.js +++ b/lib/connection-parameters.js @@ -64,6 +64,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) } // 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 e99abef4..30214b1d 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -52,7 +52,10 @@ module.exports = { application_name: undefined, fallback_application_name: undefined, - parseInputDatesAsUTC: false + parseInputDatesAsUTC: false, + + // max milliseconds any query using this connection will execute for before timing out in error. false=unlimited + statement_timeout: false } var pgTypes = require('pg-types') diff --git a/test/integration/client/statement_timeout-tests.js b/test/integration/client/statement_timeout-tests.js new file mode 100644 index 00000000..5bf1e943 --- /dev/null +++ b/test/integration/client/statement_timeout-tests.js @@ -0,0 +1,69 @@ +'use strict' +var helper = require('./test-helper') +var Client = helper.Client + +var suite = new helper.Suite() + +var conInfo = helper.config + +function getConInfo (override) { + var newConInfo = {} + Object.keys(conInfo).forEach(function (k) { + newConInfo[k] = conInfo[k] + }) + Object.keys(override || {}).forEach(function (k) { + newConInfo[k] = override[k] + }) + return newConInfo +} + +function getStatementTimeout (conf, cb) { + var client = new Client(conf) + client.connect(assert.success(function () { + client.query('SHOW statement_timeout', assert.success(function (res) { + var statementTimeout = res.rows[0].statement_timeout + cb(statementTimeout) + client.end() + })) + })) +} + +if (!helper.args.native) { // statement_timeout is not supported with the native client + suite.test('No default statement_timeout ', function (done) { + getConInfo() + getStatementTimeout({}, function (res) { + assert.strictEqual(res, '0') // 0 = no timeout + done() + }) + }) + + suite.test('statement_timeout integer is used', function (done) { + var conf = getConInfo({ + 'statement_timeout': 3000 + }) + getStatementTimeout(conf, function (res) { + assert.strictEqual(res, '3s') + done() + }) + }) + + suite.test('statement_timeout float is used', function (done) { + var conf = getConInfo({ + 'statement_timeout': 3000.7 + }) + getStatementTimeout(conf, function (res) { + assert.strictEqual(res, '3s') + done() + }) + }) + + suite.test('statement_timeout string is used', function (done) { + var conf = getConInfo({ + 'statement_timeout': '3000' + }) + getStatementTimeout(conf, function (res) { + assert.strictEqual(res, '3s') + done() + }) + }) +} diff --git a/test/unit/connection-parameters/creation-tests.js b/test/unit/connection-parameters/creation-tests.js index a3fa2513..8563a404 100644 --- a/test/unit/connection-parameters/creation-tests.js +++ b/test/unit/connection-parameters/creation-tests.js @@ -22,6 +22,7 @@ var compare = function (actual, expected, type) { assert.equal(actual.host, expected.host, type + ' host') assert.equal(actual.password, expected.password, type + ' password') assert.equal(actual.binary, expected.binary, type + ' binary') + assert.equal(actual.statement_timout, expected.statement_timout, type + ' binary') } test('ConnectionParameters initializing from defaults', function () { @@ -60,7 +61,8 @@ test('ConnectionParameters initializing from config', function () { host: 'yo', ssl: { asdf: 'blah' - } + }, + statement_timeout: 15000 } var subject = new ConnectionParameters(config) compare(subject, config, 'config')