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,dgram: return this from ref and unref methods #1768

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/dgram.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,12 @@ Calling `unref` on a socket will allow the program to exit if this is the only
active socket in the event system. If the socket is already `unref`d calling
`unref` again will have no effect.

Returns `socket`.

### socket.ref()

Opposite of `unref`, calling `ref` on a previously `unref`d socket will *not*
let the program exit if it's the only socket left (the default behavior). If
the socket is `ref`d calling `ref` again will have no effect.

Returns `socket`.
8 changes: 8 additions & 0 deletions doc/api/net.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,16 @@ Calling `unref` on a server will allow the program to exit if this is the only
active server in the event system. If the server is already `unref`d calling
`unref` again will have no effect.

Returns `server`.

### server.ref()

Opposite of `unref`, calling `ref` on a previously `unref`d server will *not*
let the program exit if it's the only server left (the default behavior). If
the server is `ref`d calling `ref` again will have no effect.

Returns `server`.

### server.maxConnections

Set this property to reject connections when the server's connection count gets
Expand Down Expand Up @@ -484,12 +488,16 @@ Calling `unref` on a socket will allow the program to exit if this is the only
active socket in the event system. If the socket is already `unref`d calling
`unref` again will have no effect.

Returns `socket`.

### socket.ref()

Opposite of `unref`, calling `ref` on a previously `unref`d socket will *not*
let the program exit if it's the only socket left (the default behavior). If
the socket is `ref`d calling `ref` again will have no effect.

Returns `socket`.

### socket.remoteAddress

The string representation of the remote IP address. For example,
Expand Down
4 changes: 4 additions & 0 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,14 @@ function onMessage(nread, handle, buf, rinfo) {
Socket.prototype.ref = function() {
if (this._handle)
this._handle.ref();

return this;
};


Socket.prototype.unref = function() {
if (this._handle)
this._handle.unref();

return this;
};
12 changes: 10 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -984,20 +984,24 @@ function connectErrorNT(self, err, options) {
Socket.prototype.ref = function() {
if (!this._handle) {
this.once('connect', this.ref);
return;
return this;
}

this._handle.ref();

return this;
};


Socket.prototype.unref = function() {
if (!this._handle) {
this.once('connect', this.unref);
return;
return this;
}

this._handle.unref();

return this;
};


Expand Down Expand Up @@ -1506,13 +1510,17 @@ Server.prototype.ref = function() {

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

return this;
};

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

if (this._handle)
this._handle.unref();

return this;
};


Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-ref-unref-return.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
var assert = require('assert');
var net = require('net');
var dgram = require('dgram');
var common = require('../common');

assert.ok((new net.Server()).ref() instanceof net.Server);
assert.ok((new net.Server()).unref() instanceof net.Server);
assert.ok((new net.Socket()).ref() instanceof net.Socket);
assert.ok((new net.Socket()).unref() instanceof net.Socket);
assert.ok((new dgram.Socket('udp4')).ref() instanceof dgram.Socket);
assert.ok((new dgram.Socket('udp6')).unref() instanceof dgram.Socket);