Skip to content
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

Add new optional parameter to have all sockets send KeepAlive packets #2110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
Expand Up @@ -235,6 +235,7 @@ issue [#501](https://github.com/mysqljs/mysql/issues/501). (Default: `false`)
also possible to blacklist default ones. For more information, check
[Connection Flags](#connection-flags).
* `ssl`: object with ssl parameters or a string containing name of ssl profile. See [SSL options](#ssl-options).
* `keepAliveDelay`: Delay in milliseconds after which the connection socket will send a keepalive packet. See [Net.socket.setKeepAlive](https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay).


In addition to passing these options as an object, you can also use a url
Expand Down
3 changes: 3 additions & 0 deletions lib/Connection.js
Expand Up @@ -431,6 +431,9 @@ Connection.prototype._handleProtocolDrain = function() {

Connection.prototype._handleProtocolConnect = function() {
this.state = 'connected';
if (this.config.keepAliveDelay) {
this._socket.setKeepAlive(true, this.config.keepAliveDelay);
}
this.emit('connect');
};

Expand Down
1 change: 1 addition & 0 deletions lib/ConnectionConfig.js
Expand Up @@ -37,6 +37,7 @@ function ConnectionConfig(options) {
this.typeCast = (options.typeCast === undefined)
? true
: options.typeCast;
this.keepAliveDelay = options.keepAliveDelay || 0;

if (this.timezone[0] === ' ') {
// "+" is a url encoded char for space so it
Expand Down
16 changes: 16 additions & 0 deletions test/unit/connection/test-connect-keepalive.js
@@ -0,0 +1,16 @@
var assert = require('assert');
var common = require('../../common');
var connection = common.createConnection({keepAliveDelay: 60000, port: common.fakeServerPort});

var server = common.createFakeServer();

server.listen(common.fakeServerPort, function (err) {
assert.ifError(err);

connection.on('connect', function () {
connection.destroy();
server.destroy();
});

connection.connect(assert.ifError);
});
11 changes: 11 additions & 0 deletions test/unit/test-ConnectionConfig.js
Expand Up @@ -64,6 +64,17 @@ test('ConnectionConfig#Constructor', {
'blacklists unsupported client flags': function() {
var config = new ConnectionConfig({ flags: '+CONNECT_ATTRS' });
assert.equal(config.clientFlags & common.ClientConstants.CLIENT_CONNECT_ATTRS, 0);
},

'Socket keepAlive defaults to 0 (default)': function() {
var config = new ConnectionConfig({});
assert.equal(config.keepAliveDelay, 0);
},

'Socket keepAlive is set in config': function()
{
var config = new ConnectionConfig({ keepAliveDelay: 60000 });
assert.equal(config.keepAliveDelay, 60000);
}
});

Expand Down