Skip to content

Commit ad36063

Browse files
Joshbrianc
Josh
authored andcommitted
support statement_timeout
1 parent 4936033 commit ad36063

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ build/
55
node_modules/
66
package-lock.json
77
*.swp
8+
/.idea

lib/client.js

+3
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ Client.prototype.getStartupConf = function () {
269269
if (params.replication) {
270270
data.replication = '' + params.replication
271271
}
272+
if (params.statement_timeout) {
273+
data.statement_timeout = String(parseInt(params.statement_timeout, 10))
274+
}
272275

273276
return data
274277
}

lib/connection-parameters.js

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var ConnectionParameters = function (config) {
6464

6565
this.application_name = val('application_name', config, 'PGAPPNAME')
6666
this.fallback_application_name = val('fallback_application_name', config, false)
67+
this.statement_timeout = val('statement_timeout', config, false)
6768
}
6869

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

lib/defaults.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ module.exports = {
5252
application_name: undefined,
5353
fallback_application_name: undefined,
5454

55-
parseInputDatesAsUTC: false
55+
parseInputDatesAsUTC: false,
56+
57+
// max milliseconds any query using this connection will execute for before timing out in error. false=unlimited
58+
statement_timeout: false
5659
}
5760

5861
var pgTypes = require('pg-types')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict'
2+
var helper = require('./test-helper')
3+
var Client = helper.Client
4+
5+
var suite = new helper.Suite()
6+
7+
var conInfo = helper.config
8+
9+
function getConInfo (override) {
10+
var newConInfo = {}
11+
Object.keys(conInfo).forEach(function (k) {
12+
newConInfo[k] = conInfo[k]
13+
})
14+
Object.keys(override || {}).forEach(function (k) {
15+
newConInfo[k] = override[k]
16+
})
17+
return newConInfo
18+
}
19+
20+
function getStatementTimeout (conf, cb) {
21+
var client = new Client(conf)
22+
client.connect(assert.success(function () {
23+
client.query('SHOW statement_timeout', assert.success(function (res) {
24+
var statementTimeout = res.rows[0].statement_timeout
25+
cb(statementTimeout)
26+
client.end()
27+
}))
28+
}))
29+
}
30+
31+
if (!helper.args.native) { // statement_timeout is not supported with the native client
32+
suite.test('No default statement_timeout ', function (done) {
33+
getConInfo()
34+
getStatementTimeout({}, function (res) {
35+
assert.strictEqual(res, '0') // 0 = no timeout
36+
done()
37+
})
38+
})
39+
40+
suite.test('statement_timeout integer is used', function (done) {
41+
var conf = getConInfo({
42+
'statement_timeout': 3000
43+
})
44+
getStatementTimeout(conf, function (res) {
45+
assert.strictEqual(res, '3s')
46+
done()
47+
})
48+
})
49+
50+
suite.test('statement_timeout float is used', function (done) {
51+
var conf = getConInfo({
52+
'statement_timeout': 3000.7
53+
})
54+
getStatementTimeout(conf, function (res) {
55+
assert.strictEqual(res, '3s')
56+
done()
57+
})
58+
})
59+
60+
suite.test('statement_timeout string is used', function (done) {
61+
var conf = getConInfo({
62+
'statement_timeout': '3000'
63+
})
64+
getStatementTimeout(conf, function (res) {
65+
assert.strictEqual(res, '3s')
66+
done()
67+
})
68+
})
69+
}

test/unit/connection-parameters/creation-tests.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var compare = function (actual, expected, type) {
2222
assert.equal(actual.host, expected.host, type + ' host')
2323
assert.equal(actual.password, expected.password, type + ' password')
2424
assert.equal(actual.binary, expected.binary, type + ' binary')
25+
assert.equal(actual.statement_timout, expected.statement_timout, type + ' binary')
2526
}
2627

2728
test('ConnectionParameters initializing from defaults', function () {
@@ -60,7 +61,8 @@ test('ConnectionParameters initializing from config', function () {
6061
host: 'yo',
6162
ssl: {
6263
asdf: 'blah'
63-
}
64+
},
65+
statement_timeout: 15000
6466
}
6567
var subject = new ConnectionParameters(config)
6668
compare(subject, config, 'config')

0 commit comments

Comments
 (0)