Skip to content

Commit d927b40

Browse files
committed
add heartbeat
1 parent 5a2d01c commit d927b40

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

lib/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ var Client = function(config) {
2525

2626
this.connection = c.connection || new Connection({
2727
stream: c.stream,
28-
ssl: this.connectionParameters.ssl
28+
ssl: this.connectionParameters.ssl,
29+
heartbeat: this.connectionParameters.heartbeatInterval
2930
});
3031
this.queryQueue = [];
3132
this.binary = c.binary || defaults.binary;

lib/connection-parameters.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ var ConnectionParameters = function(config) {
4848

4949
this.application_name = val('application_name', config, 'PGAPPNAME');
5050
this.fallback_application_name = val('fallback_application_name', config, false);
51+
52+
this.heartbeatInterval = parseInt(val('heartbeat_interval', config), 10);
5153
};
5254

5355
var add = function(params, config, paramName) {

lib/connection.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ var Connection = function(config) {
2626
headerSize: 1,
2727
lengthPadding: -4
2828
});
29+
this._heartbeatEnable = !!config.heartbeat;
30+
this._heartbeatInterval = Number(config.heartbeat);
31+
this._heartbeatObject = null;
2932
var self = this;
3033
this.on('newListener', function(eventName) {
3134
if(eventName == 'message') {
@@ -48,6 +51,16 @@ Connection.prototype.connect = function(port, host) {
4851

4952
this.stream.on('connect', function() {
5053
self.emit('connect');
54+
if (self._heartbeatEnable) {
55+
self._heartbeatObject = setInterval(function() {
56+
self.stream.write('', function (err) {
57+
if (err) {
58+
clearInterval(self._heartbeatObject);
59+
self.emit('error', err);
60+
}
61+
})
62+
}, self._heartbeatInterval);
63+
}
5164
});
5265

5366
this.stream.on('error', function(error) {
@@ -56,13 +69,15 @@ Connection.prototype.connect = function(port, host) {
5669
if(self._ending && error.code == 'ECONNRESET') {
5770
return;
5871
}
72+
clearInterval(self._heartbeatObject);
5973
self.emit('error', error);
6074
});
6175

6276
this.stream.on('close', function() {
6377
// NOTE: node-0.10 emits both 'end' and 'close'
6478
// for streams closed by the peer, while
6579
// node-0.8 only emits 'close'
80+
clearInterval(self._heartbeatObject);
6681
self.emit('end');
6782
});
6883

lib/defaults.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ var defaults = module.exports = {
4141
ssl: false,
4242

4343
application_name : undefined,
44-
fallback_application_name: undefined
44+
fallback_application_name: undefined,
45+
46+
//heartbeat interval to check, that socket is alive. if 0 - heartbeat is disabled (for backward capability).
47+
//else - timeout in ms.
48+
heartbeat_interval: 0
4549
};
4650

4751
//parse int8 so you can get your count values as actual numbers

0 commit comments

Comments
 (0)