Skip to content

Commit

Permalink
Support idle_in_transaction_session_timeout and statement_timeout for…
Browse files Browse the repository at this point in the history
… native driver

Right now when using `pg` with `pg-native` the config options
`idle_in_transaction_session_timeout` and `statement_timeout`
do not get applied. This is due to, when building the connection
string for native driver these options aren't passed in.

I took advantage of `options` conn parameter and passed both the config options
into it using `-c` which postgres supports. This builds on top of the work happened in <TBD LINK>.

Also added some tests. Very much open to feedback and comments :).
  • Loading branch information
Shayon Mukherjee committed Aug 30, 2020
1 parent 95b5daa commit 1bda055
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/pg/lib/connection-parameters.js
Expand Up @@ -45,6 +45,21 @@ var add = function (params, config, paramName) {
}
}

var escapeOptionValue = function (value) {
return ('' + value).replace(/\\/g, '\\\\').replace(/ /g, '\\ ')
}

var addOption = function (options, config, optionName) {
if (!config[optionName]) {
return
}

var value = config[optionName]
if (value !== undefined && value !== null) {
options.push('-c ' + optionName + '=' + escapeOptionValue(value))
}
}

class ConnectionParameters {
constructor(config) {
// if a string is passed, it is a raw connection string so we parse it into a config
Expand Down Expand Up @@ -115,6 +130,8 @@ class ConnectionParameters {

getLibpqConnectionString(cb) {
var params = []
var pgOptions = []

add(params, this, 'user')
add(params, this, 'password')
add(params, this, 'port')
Expand All @@ -123,6 +140,16 @@ class ConnectionParameters {
add(params, this, 'connect_timeout')
add(params, this, 'options')

addOption(pgOptions, this, 'statement_timeout')
addOption(pgOptions, this, 'idle_in_transaction_session_timeout')

if (this.options) {
pgOptions.push(this.options)
}
if (pgOptions.length > 0) {
params.push('options=' + quoteParamValue(pgOptions.join(' ')))
}

var ssl = typeof this.ssl === 'object' ? this.ssl : this.ssl ? { sslmode: this.ssl } : {}
add(params, ssl, 'sslmode')
add(params, ssl, 'sslca')
Expand Down
20 changes: 20 additions & 0 deletions packages/pg/test/unit/connection-parameters/creation-tests.js
Expand Up @@ -172,6 +172,26 @@ test('libpq connection string building', function () {
)
})

test('builds conn string with options', function () {
var config = {
user: 'brian',
password: 'xyz',
port: 888,
host: 'localhost',
database: 'bam',
statement_timeout: 5000,
idle_in_transaction_session_timeout: 5000,
}
var subject = new ConnectionParameters(config)
subject.getLibpqConnectionString(
assert.calls(function (err, constring) {
assert(!err)
var parts = constring.split(/ (?=([^\']*\'[^\']*\')*[^\']*$)/)
checkForPart(parts, "options='-c statement_timeout=5000 -c idle_in_transaction_session_timeout=5000'")
})
)
})

test('builds dns string', function () {
var config = {
user: 'brian',
Expand Down

0 comments on commit 1bda055

Please sign in to comment.