Skip to content

Make tcp-keepalive configurable #1058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
keepAlive: c.keepAlive || false
});
this.queryQueue = [];
this.binary = c.binary || defaults.binary;
Expand Down
5 changes: 4 additions & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Connection = function(config) {
EventEmitter.call(this);
config = config || {};
this.stream = config.stream || new net.Stream();
this._keepAlive = config.keepAlive;
this.lastBuffer = false;
this.lastOffset = 0;
this.buffer = null;
Expand Down Expand Up @@ -47,8 +48,10 @@ Connection.prototype.connect = function(port, host) {
var self = this;

this.stream.on('connect', function() {
if (self._keepAlive) {
self.stream.setKeepAlive(true);
}
self.emit('connect');
self.stream.setKeepAlive(true);
});

this.stream.on('error', function(error) {
Expand Down
27 changes: 19 additions & 8 deletions test/unit/connection/startup-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ test('connection can take existing stream', function() {
});

test('using closed stream', function() {
var stream = new MemoryStream();
stream.readyState = 'closed';
stream.connect = function(port, host) {
this.connectCalled = true;
this.port = port;
this.host = host;
}
var makeStream = function() {
var stream = new MemoryStream();
stream.readyState = 'closed';
stream.connect = function(port, host) {
this.connectCalled = true;
this.port = port;
this.host = host;
}
return stream;
};

var stream = makeStream();

var con = new Connection({stream: stream});

Expand Down Expand Up @@ -46,14 +51,20 @@ test('using closed stream', function() {

test('after stream emits connected event init TCP-keepalive', function() {

var stream = makeStream();
var con = new Connection({ stream: stream, keepAlive: true });
con.connect(123, 'test');

var res = false;

stream.setKeepAlive = function(bit) {
res = bit;
};

assert.ok(stream.emit('connect'));
assert.equal(res, true);
setTimeout(function() {
assert.equal(res, true);
})
});
});

Expand Down