From d927b400bd8acd6399dff9bc636703c97d22cbf3 Mon Sep 17 00:00:00 2001 From: NumminorihSF Date: Mon, 11 Jan 2016 14:22:05 +0700 Subject: [PATCH 1/2] add heartbeat --- lib/client.js | 3 ++- lib/connection-parameters.js | 2 ++ lib/connection.js | 15 +++++++++++++++ lib/defaults.js | 6 +++++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index f0a374d32..fd5906444 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, + heartbeat: this.connectionParameters.heartbeatInterval }); this.queryQueue = []; this.binary = c.binary || defaults.binary; diff --git a/lib/connection-parameters.js b/lib/connection-parameters.js index 2ba462bbb..5fcfc3d2f 100644 --- a/lib/connection-parameters.js +++ b/lib/connection-parameters.js @@ -48,6 +48,8 @@ var ConnectionParameters = function(config) { this.application_name = val('application_name', config, 'PGAPPNAME'); this.fallback_application_name = val('fallback_application_name', config, false); + + this.heartbeatInterval = parseInt(val('heartbeat_interval', config), 10); }; var add = function(params, config, paramName) { diff --git a/lib/connection.js b/lib/connection.js index f606de136..505305f08 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -26,6 +26,9 @@ var Connection = function(config) { headerSize: 1, lengthPadding: -4 }); + this._heartbeatEnable = !!config.heartbeat; + this._heartbeatInterval = Number(config.heartbeat); + this._heartbeatObject = null; var self = this; this.on('newListener', function(eventName) { if(eventName == 'message') { @@ -48,6 +51,16 @@ Connection.prototype.connect = function(port, host) { this.stream.on('connect', function() { self.emit('connect'); + if (self._heartbeatEnable) { + self._heartbeatObject = setInterval(function() { + self.stream.write('', function (err) { + if (err) { + clearInterval(self._heartbeatObject); + self.emit('error', err); + } + }) + }, self._heartbeatInterval); + } }); this.stream.on('error', function(error) { @@ -56,6 +69,7 @@ Connection.prototype.connect = function(port, host) { if(self._ending && error.code == 'ECONNRESET') { return; } + clearInterval(self._heartbeatObject); self.emit('error', error); }); @@ -63,6 +77,7 @@ Connection.prototype.connect = function(port, host) { // NOTE: node-0.10 emits both 'end' and 'close' // for streams closed by the peer, while // node-0.8 only emits 'close' + clearInterval(self._heartbeatObject); self.emit('end'); }); diff --git a/lib/defaults.js b/lib/defaults.js index d3cb911a4..8b27fc83f 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -41,7 +41,11 @@ var defaults = module.exports = { ssl: false, application_name : undefined, - fallback_application_name: undefined + fallback_application_name: undefined, + + //heartbeat interval to check, that socket is alive. if 0 - heartbeat is disabled (for backward capability). + //else - timeout in ms. + heartbeat_interval: 0 }; //parse int8 so you can get your count values as actual numbers From eb9727aaceee70cd03fceabfe9651633143d9557 Mon Sep 17 00:00:00 2001 From: NumminorihSF Date: Mon, 11 Jan 2016 14:38:28 +0700 Subject: [PATCH 2/2] add semicolon --- lib/connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/connection.js b/lib/connection.js index 505305f08..c8f736543 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -58,7 +58,7 @@ Connection.prototype.connect = function(port, host) { clearInterval(self._heartbeatObject); self.emit('error', err); } - }) + }); }, self._heartbeatInterval); } });