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

support maxConnections event on net server #31337

Closed
wants to merge 3 commits 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
8 changes: 8 additions & 0 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ added: v0.1.90
Emitted when a new connection is made. `socket` is an instance of
`net.Socket`.

### Event: `'maxConnections'`
<!-- YAML
added: REPLACEME
-->

Emitted when the server has reached its connection limit defined in [`server.maxConnections`][].

### Event: `'error'`
<!-- YAML
added: v0.1.90
Expand Down Expand Up @@ -1251,6 +1258,7 @@ Returns `true` if input is a version 6 IP address, otherwise returns `false`.
[`server.listen(handle)`]: #net_server_listen_handle_backlog_callback
[`server.listen(options)`]: #net_server_listen_options_callback
[`server.listen(path)`]: #net_server_listen_path_backlog_callback
[`server.maxConnections`]: #net_server_maxconnections
[`socket(7)`]: http://man7.org/linux/man-pages/man7/socket.7.html
[`socket.connect()`]: #net_socket_connect
[`socket.connect(options)`]: #net_socket_connect_options_connectlistener
Expand Down
1 change: 1 addition & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,7 @@ function onconnection(err, clientHandle) {
}

if (self.maxConnections && self._connections >= self.maxConnections) {
self.emit('maxConnections');
Copy link
Member

Choose a reason for hiding this comment

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

@mcollina @addaleax @BridgeAR ... would be good to get some more opinions on this... do we want to communicate this case this way or using an error instead?

Copy link
Member

Choose a reason for hiding this comment

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

I think this is totally fine as is.

Copy link
Member

Choose a reason for hiding this comment

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

Works for me too then :-)

clientHandle.close();
return;
}
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-net-server-max-connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ server.listen(0, function() {

server.maxConnections = N / 2;

server.on('maxConnections', common.mustCall(server.maxConnections));

function makeConnection(index) {
const c = net.createConnection(server.address().port);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-net-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const buf = Buffer.alloc(SIZE, 'a');
const server = net.createServer(function(socket) {
socket.setNoDelay();

socket.on('error', function(err) {
socket.on('error', function() {
socket.destroy();
}).on('close', function() {
server.close();
Expand Down