-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, 400); | ||
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(); | ||
}, 600); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
var net = require('net'); | ||
var TCPWrap = process.binding('tcp_wrap').TCP; | ||
|
||
var echoServer = net.createServer(function(connection) { | ||
connection.end(); | ||
}); | ||
echoServer.listen(common.PORT); | ||
|
||
var setTrue = 0; | ||
|
||
var Socket = net.Socket; | ||
var setNoDelay = TCPWrap.prototype.setNoDelay; | ||
|
||
TCPWrap.prototype.setNoDelay = function(enable) { | ||
setNoDelay.call(this, enable); | ||
setTrue++; | ||
} | ||
|
||
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) | ||
sock1.on('connect', function() { | ||
setTimeout(function() { | ||
echoServer.close(); | ||
}, 500); | ||
}); | ||
}); | ||
|
||
process.on('exit', function() { | ||
assert.ok(setTrue); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TCPWrap = process.binding('tcp_wrap').TCP; | ||
|
||
var echoServer = net.createServer(function(conn) { | ||
conn.end(); | ||
}); | ||
|
||
var ref = TCPWrap.prototype.ref; | ||
var unref = TCPWrap.prototype.unref; | ||
|
||
var refed = false; | ||
var unrefed = false; | ||
|
||
TCPWrap.prototype.ref = function() { | ||
ref.call(this); | ||
refed = true; | ||
} | ||
|
||
TCPWrap.prototype.unref = function() { | ||
unref.call(this); | ||
unrefed = true; | ||
} | ||
|
||
echoServer.listen(common.PORT); | ||
|
||
echoServer.on('listening', function() { | ||
var sock = new net.Socket(); | ||
sock.unref(); | ||
sock.ref(); | ||
sock.connect(common.PORT, function() { | ||
setTimeout(function() { | ||
echoServer.close(); | ||
}, 500); | ||
}) | ||
}) | ||
|
||
process.on('exit', function() { | ||
assert.ok(refed); | ||
assert.ok(unrefed); | ||
}) |