Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: return this from setNoDelay and setKeepAlive #1779

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/net.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ algorithm, they buffer data before sending it off. Setting `true` for
`noDelay` will immediately fire off data each time `socket.write()` is called.
`noDelay` defaults to `true`.

Returns `socket`.

### socket.setKeepAlive([enable][, initialDelay])

Enable/disable keep-alive functionality, and optionally set the initial
Expand All @@ -475,6 +477,8 @@ data packet received and the first keepalive probe. Setting 0 for
initialDelay will leave the value unchanged from the default
(or previous) setting. Defaults to `0`.

Returns `socket`.

### socket.address()

Returns the bound address, the address family name and port of the
Expand Down
8 changes: 6 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,27 @@ Socket.prototype.setNoDelay = function(enable) {
if (!this._handle) {
this.once('connect',
enable ? this.setNoDelay : this.setNoDelay.bind(this, enable));
return;
return this;
}

// backwards compatibility: assume true when `enable` is omitted
if (this._handle.setNoDelay)
this._handle.setNoDelay(enable === undefined ? true : !!enable);

return this;
};


Socket.prototype.setKeepAlive = function(setting, msecs) {
if (!this._handle) {
this.once('connect', this.setKeepAlive.bind(this, setting, msecs));
return;
return this;
}

if (this._handle.setKeepAlive)
this._handle.setKeepAlive(setting, ~~(msecs / 1000));

return this;
};


Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-net-persistent-keepalive.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ 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);
var s = clientConnection.setKeepAlive(true, 400);
assert.ok(s instanceof net.Socket);
clientConnection.connect(common.PORT);
clientConnection.setTimeout(0);

Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-net-persistent-nodelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ echoServer.on('listening', function() {
// setNoDelay before the handle is created
// there is probably a better way to test this

sock1.setNoDelay();
var s = sock1.setNoDelay();
assert.ok(s instanceof net.Socket);
sock1.connect(common.PORT);
sock1.on('end', function() {
assert.equal(callCount, 1);
Expand Down