Skip to content

Commit dd2de41

Browse files
author
Gabriel
committed
Add new optional parameter to have all sockets send keepalive packets
1 parent cf5d1e3 commit dd2de41

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ issue [#501](https://github.com/mysqljs/mysql/issues/501). (Default: `false`)
235235
also possible to blacklist default ones. For more information, check
236236
[Connection Flags](#connection-flags).
237237
* `ssl`: object with ssl parameters or a string containing name of ssl profile. See [SSL options](#ssl-options).
238+
* `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).
238239

239240

240241
In addition to passing these options as an object, you can also use a url

lib/Connection.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ Connection.prototype._handleProtocolDrain = function() {
431431

432432
Connection.prototype._handleProtocolConnect = function() {
433433
this.state = 'connected';
434+
if (this.config.keepAliveDelay) {
435+
this._socket.setKeepAlive(true, this.config.keepAliveDelay);
436+
}
434437
this.emit('connect');
435438
};
436439

lib/ConnectionConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function ConnectionConfig(options) {
3737
this.typeCast = (options.typeCast === undefined)
3838
? true
3939
: options.typeCast;
40+
this.keepAliveDelay = options.keepAliveDelay || 0;
4041

4142
if (this.timezone[0] === ' ') {
4243
// "+" is a url encoded char for space so it
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var connection = common.createConnection({keepAliveDelay : 60000, port: common.fakeServerPort});
4+
5+
var server = common.createFakeServer();
6+
7+
server.listen(common.fakeServerPort, function (err) {
8+
assert.ifError(err);
9+
10+
connection.on('connect', function () {
11+
connection.destroy();
12+
server.destroy();
13+
});
14+
15+
connection.connect(assert.ifError);
16+
});

test/unit/test-ConnectionConfig.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ test('ConnectionConfig#Constructor', {
6464
'blacklists unsupported client flags': function() {
6565
var config = new ConnectionConfig({ flags: '+CONNECT_ATTRS' });
6666
assert.equal(config.clientFlags & common.ClientConstants.CLIENT_CONNECT_ATTRS, 0);
67+
},
68+
69+
'Socket keepAlive defaults to 0 (default)': function() {
70+
var config = new ConnectionConfig({});
71+
assert.equal(config.keepAliveDelay, 0);
72+
},
73+
74+
'Socket keepAlive is set in config': function()
75+
{
76+
var config = new ConnectionConfig({ keepAliveDelay : 60000 });
77+
assert.equal(config.keepAliveDelay, 60000);
6778
}
6879
});
6980

0 commit comments

Comments
 (0)