Skip to content

Commit

Permalink
Adding ability to pass through idle_in_transaction_session_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
aheuermann committed Dec 27, 2019
1 parent 69345eb commit 26a6f2b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/pg/lib/client.js
Expand Up @@ -371,6 +371,9 @@ Client.prototype.getStartupConf = function () {
if (params.statement_timeout) {
data.statement_timeout = String(parseInt(params.statement_timeout, 10))
}
if (params.idle_in_transaction_session_timeout) {
data.idle_in_transaction_session_timeout = String(parseInt(params.idle_in_transaction_session_timeout, 10))
}

return data
}
Expand Down
1 change: 1 addition & 0 deletions packages/pg/lib/connection-parameters.js
Expand Up @@ -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.idle_in_transaction_session_timeout = val('idle_in_transaction_session_timeout', config, false)
this.query_timeout = val('query_timeout', config, false)

if (config.connectionTimeoutMillis === undefined) {
Expand Down
4 changes: 4 additions & 0 deletions packages/pg/lib/defaults.js
Expand Up @@ -59,6 +59,10 @@ module.exports = {
// false=unlimited
statement_timeout: false,

// Terminate any session with an open transaction that has been idle for longer than the specified duration in milliseconds
// false=unlimited
idle_in_transaction_session_timeout: false

// max milliseconds to wait for query to complete (client side)
query_timeout: false,

Expand Down
@@ -0,0 +1,63 @@
'use strict'
var helper = require('./test-helper')
var Client = helper.Client

var suite = new helper.Suite()

var conInfo = helper.config

function getConInfo (override) {
return Object.assign({}, conInfo, override )
}

function getIdleTransactionSessionTimeout (conf, cb) {
var client = new Client(conf)
client.connect(assert.success(function () {
client.query('SHOW idle_in_transaction_session_timeout', assert.success(function (res) {
var timeout = res.rows[0].idle_in_transaction_session_timeout
cb(timeout)
client.end()
}))
}))
}

if (!helper.args.native) { // idle_in_transaction_session_timeout is not supported with the native client
suite.test('No default idle_in_transaction_session_timeout ', function (done) {
getConInfo()
getIdleTransactionSessionTimeout({}, function (res) {
assert.strictEqual(res, '0') // 0 = no timeout
done()
})
})

suite.test('idle_in_transaction_session_timeout integer is used', function (done) {
var conf = getConInfo({
'idle_in_transaction_session_timeout': 3000
})
getIdleTransactionSessionTimeout(conf, function (res) {
assert.strictEqual(res, '3s')
done()
})
})

suite.test('idle_in_transaction_session_timeout float is used', function (done) {
var conf = getConInfo({
'idle_in_transaction_session_timeout': 3000.7
})
getIdleTransactionSessionTimeout(conf, function (res) {
assert.strictEqual(res, '3s')
done()
})
})

suite.test('idle_in_transaction_session_timeout string is used', function (done) {
var conf = getConInfo({
'idle_in_transaction_session_timeout': '3000'
})
getIdleTransactionSessionTimeout(conf, function (res) {
assert.strictEqual(res, '3s')
done()
})
})

}
Expand Up @@ -23,6 +23,7 @@ var compare = function (actual, expected, type) {
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 + ' statement_timeout')
assert.equal(actual.idle_in_transaction_session_timeout, expected.idle_in_transaction_session_timeout, type + 'idle_in_transaction_session_timeout')
}

test('ConnectionParameters initializing from defaults', function () {
Expand Down Expand Up @@ -62,7 +63,8 @@ test('ConnectionParameters initializing from config', function () {
ssl: {
asdf: 'blah'
},
statement_timeout: 15000
statement_timeout: 15000,
idle_in_transaction_session_timeout: 15000
}
var subject = new ConnectionParameters(config)
compare(subject, config, 'config')
Expand Down

0 comments on commit 26a6f2b

Please sign in to comment.