Skip to content

Commit 6051035

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 0d6d3dd commit 6051035

4 files changed

+129
-6
lines changed

lib/net.js

+25-6
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,25 @@ Socket.prototype._onTimeout = function() {
321321

322322

323323
Socket.prototype.setNoDelay = function(enable) {
324+
if (!this._handle) {
325+
this.once('connect',
326+
enable ? this.setNoDelay : this.setNoDelay.bind(this, enable));
327+
return;
328+
}
329+
324330
// backwards compatibility: assume true when `enable` is omitted
325-
if (this._handle && this._handle.setNoDelay)
331+
if (this._handle.setNoDelay)
326332
this._handle.setNoDelay(enable === undefined ? true : !!enable);
327333
};
328334

329335

330336
Socket.prototype.setKeepAlive = function(setting, msecs) {
331-
if (this._handle && this._handle.setKeepAlive)
337+
if (!this._handle) {
338+
this.once('connect', this.setKeepAlive.bind(this, setting, msecs));
339+
return;
340+
}
341+
342+
if (this._handle.setKeepAlive)
332343
this._handle.setKeepAlive(setting, ~~(msecs / 1000));
333344
};
334345

@@ -971,14 +982,22 @@ function connectErrorNT(self, err, options) {
971982

972983

973984
Socket.prototype.ref = function() {
974-
if (this._handle)
975-
this._handle.ref();
985+
if (!this._handle) {
986+
this.once('connect', this.ref);
987+
return;
988+
}
989+
990+
this._handle.ref();
976991
};
977992

978993

979994
Socket.prototype.unref = function() {
980-
if (this._handle)
981-
this._handle.unref();
995+
if (!this._handle) {
996+
this.once('connect', this.unref);
997+
return;
998+
}
999+
1000+
this._handle.unref();
9821001
};
9831002

9841003

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, 400);
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+
}, 600);
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
var net = require('net');
4+
var TCPWrap = process.binding('tcp_wrap').TCP;
5+
6+
var echoServer = net.createServer(function(connection) {
7+
connection.end();
8+
});
9+
echoServer.listen(common.PORT);
10+
11+
var setTrue = 0;
12+
13+
var Socket = net.Socket;
14+
var setNoDelay = TCPWrap.prototype.setNoDelay;
15+
16+
TCPWrap.prototype.setNoDelay = function(enable) {
17+
setNoDelay.call(this, enable);
18+
setTrue++;
19+
};
20+
21+
echoServer.on('listening', function() {
22+
var sock1 = new Socket();
23+
// setNoDelay before the handle is created
24+
// there is probably a better way to test this
25+
26+
sock1.setNoDelay();
27+
sock1.connect(common.PORT);
28+
sock1.on('end', process.exit);
29+
});
30+
31+
process.on('exit', function() {
32+
assert.ok(setTrue);
33+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
var net = require('net');
4+
var TCPWrap = process.binding('tcp_wrap').TCP;
5+
6+
var echoServer = net.createServer(function(conn) {
7+
conn.end();
8+
});
9+
10+
var ref = TCPWrap.prototype.ref;
11+
var unref = TCPWrap.prototype.unref;
12+
13+
var refed = false;
14+
var unrefed = false;
15+
16+
TCPWrap.prototype.ref = function() {
17+
ref.call(this);
18+
refed = true;
19+
};
20+
21+
TCPWrap.prototype.unref = function() {
22+
unref.call(this);
23+
unrefed = true;
24+
};
25+
26+
echoServer.listen(common.PORT);
27+
28+
echoServer.on('listening', function() {
29+
var sock = new net.Socket();
30+
sock.unref();
31+
sock.ref();
32+
sock.connect(common.PORT);
33+
sock.on('end', process.exit);
34+
});
35+
36+
process.on('exit', function() {
37+
assert.ok(refed);
38+
assert.ok(unrefed);
39+
});

0 commit comments

Comments
 (0)