Skip to content

Commit 9fa5e6a

Browse files
committed
net: persist net.Socket options before connect
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
1 parent c86e383 commit 9fa5e6a

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

lib/net.js

+20
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ function Socket(options) {
121121
this._hadError = false;
122122
this._handle = null;
123123
this._host = null;
124+
this._unref = false;
125+
this._setNoDelay = null;
126+
this._setKeepAlive = null;
124127

125128
if (typeof options === 'number')
126129
options = { fd: options }; // Legacy interface.
@@ -325,12 +328,16 @@ Socket.prototype.setNoDelay = function(enable) {
325328
// backwards compatibility: assume true when `enable` is omitted
326329
if (this._handle && this._handle.setNoDelay)
327330
this._handle.setNoDelay(enable === undefined ? true : !!enable);
331+
else
332+
this._setNoDelay = enable === undefined ? true : !!enable;
328333
};
329334

330335

331336
Socket.prototype.setKeepAlive = function(setting, msecs) {
332337
if (this._handle && this._handle.setKeepAlive)
333338
this._handle.setKeepAlive(setting, ~~(msecs / 1000));
339+
else
340+
this._setKeepAlive = [setting, ~~(msecs / 1000)];
334341
};
335342

336343

@@ -766,6 +773,15 @@ function connect(self, address, port, addressType, localAddress, localPort) {
766773

767774
var err;
768775

776+
if (self._setNoDelay)
777+
self.setNoDelay(self._setNoDelay);
778+
779+
if (self._setKeepAlive)
780+
self.setKeepAlive(self._setKeepAlive[0], self._setKeepAlive[1]);
781+
782+
if (self._unref)
783+
self.unref();
784+
769785
if (localAddress || localPort) {
770786
var bind;
771787

@@ -933,12 +949,16 @@ Socket.prototype.connect = function(options, cb) {
933949

934950

935951
Socket.prototype.ref = function() {
952+
this._unref = false;
953+
936954
if (this._handle)
937955
this._handle.ref();
938956
};
939957

940958

941959
Socket.prototype.unref = function() {
960+
this._unref = new Date();
961+
942962
if (this._handle)
943963
this._handle.unref();
944964
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
var net = require('net');
4+
5+
var serverConnection;
6+
var echoServer = net.createServer(function(connection) {
7+
serverConnection = connection;
8+
connection.setTimeout(0);
9+
assert.notEqual(connection.setKeepAlive, undefined);
10+
connection.on('end', function() {
11+
connection.end();
12+
});
13+
});
14+
echoServer.listen(common.PORT);
15+
16+
echoServer.on('listening', function() {
17+
var clientConnection = new net.Socket({});
18+
// send a keepalive packet after 1000 ms
19+
// and make sure it persists
20+
clientConnection.setKeepAlive(true, 1000);
21+
clientConnection.connect(common.PORT);
22+
clientConnection.setTimeout(0);
23+
24+
setTimeout(function() {
25+
// make sure both connections are still open
26+
assert.equal(serverConnection.readyState, 'open');
27+
assert.equal(clientConnection.readyState, 'open');
28+
serverConnection.end();
29+
clientConnection.end();
30+
echoServer.close();
31+
}, 1200);
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
var net = require('net');
4+
5+
var serverConnection;
6+
var echoServer = net.createServer(function(connection) {
7+
serverConnection = connection;
8+
connection.end()
9+
});
10+
echoServer.listen(common.PORT);
11+
12+
var setTrue = false;
13+
14+
var Socket = net.Socket;
15+
16+
Socket.prototype.setNoDelay = function(enable) {
17+
if (this._handle && this._handle.setNoDelay) {
18+
if (enable === undefined || !!enable) {
19+
setTrue = true;
20+
}
21+
this._handle.setNoDelay(enable === undefined ? true : !!enable);
22+
} else {
23+
this._setNoDelay = enable === undefined ? true : !!enable;
24+
}
25+
}
26+
27+
echoServer.on('listening', function() {
28+
var sock1 = new Socket({});
29+
// setNoDelay before the handle is created
30+
// there is probably a better way to test this
31+
32+
sock1.setNoDelay();
33+
sock1.connect(common.PORT, function() {
34+
var sock2 = new Socket({});
35+
sock2.connect(common.PORT, function() {
36+
assert.ok(setTrue);
37+
setTimeout(function() {
38+
echoServer.close();
39+
}, 500);
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)