Skip to content

Commit

Permalink
net: persist net.Socket options before connect
Browse files Browse the repository at this point in the history
Remembers net.Socket options called before connect and retroactively
applies them after the handle has been created.

This change makes the following function calls more user-friendly:

- setKeepAlive()
- setNoDelay()
- ref()
- unref()

Related: nodejs/node-v0.x-archive#7077 and
nodejs/node-v0.x-archive#8572
  • Loading branch information
evanlucas committed Feb 20, 2015
1 parent c86e383 commit 7eccc69
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ function Socket(options) {
this._hadError = false;
this._handle = null;
this._host = null;
this._unref = false;
this._setNoDelay = null;
this._setKeepAlive = null;

if (typeof options === 'number')
options = { fd: options }; // Legacy interface.
Expand Down Expand Up @@ -325,12 +328,16 @@ Socket.prototype.setNoDelay = function(enable) {
// backwards compatibility: assume true when `enable` is omitted
if (this._handle && this._handle.setNoDelay)
this._handle.setNoDelay(enable === undefined ? true : !!enable);
else
this._setNoDelay = enable === undefined ? true : !!enable;
};


Socket.prototype.setKeepAlive = function(setting, msecs) {
if (this._handle && this._handle.setKeepAlive)
this._handle.setKeepAlive(setting, ~~(msecs / 1000));
else
this._setKeepAlive = [setting, ~~(msecs / 1000)];
};


Expand Down Expand Up @@ -766,6 +773,14 @@ function connect(self, address, port, addressType, localAddress, localPort) {

var err;

self.setNoDelay(self._setNoDelay);

if (self._setKeepAlive)
self.setKeepAlive(self._setKeepAlive[0], self._setKeepAlive[1]);

if (self._unref)
self.unref();

if (localAddress || localPort) {
var bind;

Expand Down Expand Up @@ -933,12 +948,16 @@ Socket.prototype.connect = function(options, cb) {


Socket.prototype.ref = function() {
this._unref = false;

if (this._handle)
this._handle.ref();
};


Socket.prototype.unref = function() {
this._unref = true;

if (this._handle)
this._handle.unref();
};
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-net-persistent-keepalive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var common = require('../common');
var assert = require('assert');
var net = require('net');

var serverConnection;
var echoServer = net.createServer(function(connection) {
serverConnection = connection;
connection.setTimeout(0);
assert.notEqual(connection.setKeepAlive, undefined);
connection.on('end', function() {
connection.end();
});
});
echoServer.listen(common.PORT);

echoServer.on('listening', function() {
var clientConnection = new net.Socket({});
// send a keepalive packet after 1000 ms
// and make sure it persists
clientConnection.setKeepAlive(true, 1000);
clientConnection.connect(common.PORT);
clientConnection.setTimeout(0);

setTimeout(function() {
// make sure both connections are still open
assert.equal(serverConnection.readyState, 'open');
assert.equal(clientConnection.readyState, 'open');
serverConnection.end();
clientConnection.end();
echoServer.close();
}, 1200);
});
42 changes: 42 additions & 0 deletions test/parallel/test-net-persistent-nodelay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var common = require('../common');
var assert = require('assert');
var net = require('net');

var serverConnection;
var echoServer = net.createServer(function(connection) {
serverConnection = connection;
connection.end()
});
echoServer.listen(common.PORT);

var setTrue = false;

var Socket = net.Socket;

Socket.prototype.setNoDelay = function(enable) {
if (this._handle && this._handle.setNoDelay) {
if (enable === undefined || !!enable) {
setTrue = true;
}
this._handle.setNoDelay(enable === undefined ? true : !!enable);
} else {
this._setNoDelay = enable === undefined ? true : !!enable;
}
}

echoServer.on('listening', function() {
var sock1 = new Socket({});
// setNoDelay before the handle is created
// there is probably a better way to test this

sock1.setNoDelay();
sock1.connect(common.PORT, function() {
var sock2 = new Socket({});
sock2.connect(common.PORT, function() {
assert.ok(setTrue);
setTimeout(function() {
echoServer.close();
}, 500);
});
});
});

0 comments on commit 7eccc69

Please sign in to comment.