Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Issue #2762. Add callback to close function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal authored and isaacs committed Feb 19, 2012
1 parent 9633843 commit d530ee6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion doc/api/http.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ a listener for the ['listening'](net.html#event_listening_) event.
See also [net.Server.listen()](net.html#server.listen).


### server.close()
### server.close([cb])

Stops the server from accepting new connections.
See [net.Server.close()](net.html#server.close).
Expand Down
4 changes: 2 additions & 2 deletions doc/api/net.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ Stop accepting connections for the given number of milliseconds (default is
one second). This could be useful for throttling new connections against
DoS attacks or other oversubscription.

#### server.close()
#### server.close([cb])

Stops the server from accepting new connections. This function is
asynchronous, the server is finally closed when the server emits a `'close'`
event.
event. Optionally, you can pass a callback to listen for the `'close'` event.


#### server.address()
Expand Down
5 changes: 4 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,12 +885,15 @@ function onconnection(clientHandle) {
}


Server.prototype.close = function() {
Server.prototype.close = function(cb) {
if (!this._handle) {
// Throw error. Follows net_legacy behaviour.
throw new Error('Not running');
}

if (cb) {
this.once('close', cb);
}
this._handle.close();
this._handle = null;
this._emitCloseIfDrained();
Expand Down
10 changes: 4 additions & 6 deletions test/simple/test-net-server-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ var assert = require('assert');
var net = require('net');

var server = net.createServer(function(socket) {
server.close();
server.close(function() {
assert.equal(server.connections, 0);
});
process.nextTick(function() {
socket.destroy();
});
});

server.listen(common.PORT, function() {
net.createConnection(common.PORT);
});

server.on('close', function() {
assert.equal(server.connections, 0);
});
});

1 comment on commit d530ee6

@Filirom1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a simple thing but very useful :)

Please sign in to comment.