diff --git a/Readme.md b/Readme.md index 5af3ed261..d5a30abc1 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/lib/Connection.js b/lib/Connection.js index 2a37798e6..cea36429d 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -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'); }; diff --git a/lib/ConnectionConfig.js b/lib/ConnectionConfig.js index 147aa0abb..f65490f46 100644 --- a/lib/ConnectionConfig.js +++ b/lib/ConnectionConfig.js @@ -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 diff --git a/test/unit/connection/test-connect-keepalive.js b/test/unit/connection/test-connect-keepalive.js new file mode 100644 index 000000000..912e0f366 --- /dev/null +++ b/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); +}); diff --git a/test/unit/test-ConnectionConfig.js b/test/unit/test-ConnectionConfig.js index 7d7fe2d00..4ad9a97e5 100644 --- a/test/unit/test-ConnectionConfig.js +++ b/test/unit/test-ConnectionConfig.js @@ -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); } });