diff --git a/lib/client.js b/lib/client.js index 18f258705..af30389d3 100644 --- a/lib/client.js +++ b/lib/client.js @@ -25,7 +25,8 @@ var Client = function(config) { this.connection = c.connection || new Connection({ stream: c.stream, - ssl: this.connectionParameters.ssl + ssl: this.connectionParameters.ssl, + keepalives: this.connectionParameters.keepalives }); this.queryQueue = []; this.binary = c.binary || defaults.binary; diff --git a/lib/connection-parameters.js b/lib/connection-parameters.js index 2ba462bbb..12e6841fc 100644 --- a/lib/connection-parameters.js +++ b/lib/connection-parameters.js @@ -48,6 +48,9 @@ var ConnectionParameters = function(config) { this.application_name = val('application_name', config, 'PGAPPNAME'); this.fallback_application_name = val('fallback_application_name', config, false); + + this.keepalives = 'keepalives' in config ? config.keepalives : val('keepalives', config); + this.keepalives = this.keepalives === true || this.keepalives === 1 || this.keepalives === 'true' || this.keepalives === '1'; }; var add = function(params, config, paramName) { @@ -77,6 +80,9 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) { if(this.client_encoding) { params.push("client_encoding='" + this.client_encoding + "'"); } + if(this.keepalives) { + params.push("keepalives=1"); + } dns.lookup(this.host, function(err, address) { if(err) return cb(err, null); params.push("hostaddr=" + address); diff --git a/lib/connection.js b/lib/connection.js index 44b023cbe..9d39ce453 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -11,6 +11,9 @@ var Connection = function(config) { EventEmitter.call(this); config = config || {}; this.stream = config.stream || new net.Stream(); + if (typeof this.stream.setKeepAlive === 'function') { + this.stream.setKeepAlive(config.keepalives); + } this.lastBuffer = false; this.lastOffset = 0; this.buffer = null; diff --git a/lib/defaults.js b/lib/defaults.js index 3b37e3676..e5220d5ac 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -43,6 +43,8 @@ var defaults = module.exports = { application_name: undefined, fallback_application_name: undefined, + keepalives: true, + parseInputDatesAsUTC: false }; diff --git a/test/unit/client/configuration-tests.js b/test/unit/client/configuration-tests.js index d42ee0eea..09abab914 100644 --- a/test/unit/client/configuration-tests.js +++ b/test/unit/client/configuration-tests.js @@ -11,6 +11,7 @@ test('client settings', function() { assert.equal(client.user, pguser); assert.equal(client.database, pgdatabase); assert.equal(client.port, pgport); + assert.equal(client.keepalives, true); }); test('custom', function() { diff --git a/test/unit/connection-parameters/creation-tests.js b/test/unit/connection-parameters/creation-tests.js index 8ccd98866..0541adf2a 100644 --- a/test/unit/connection-parameters/creation-tests.js +++ b/test/unit/connection-parameters/creation-tests.js @@ -98,7 +98,8 @@ test('libpq connection string building', function() { password: 'xyz', port: 888, host: 'localhost', - database: 'bam' + database: 'bam', + keepalives: true } var subject = new ConnectionParameters(config); subject.getLibpqConnectionString(assert.calls(function(err, constring) { @@ -109,6 +110,7 @@ test('libpq connection string building', function() { checkForPart(parts, "port='888'"); checkForPart(parts, "hostaddr=127.0.0.1"); checkForPart(parts, "dbname='bam'"); + checkForPart(parts, "keepalives=1"); })); });