Somnath
Somnath

Reputation: 3267

How to limit number of Websocket connection using websockets/ws lib?

I want to put a hard limit on the max number of WS connections to max 100. I'm writing a node.js server where I should not allow more than 100 connections.

How to limit the number of Websocket connection without touching system ulimit?

Upvotes: 0

Views: 1392

Answers (1)

Darren Smith
Darren Smith

Reputation: 2488

Just to expand on a comment I added, I am able to restrict the amount of websocket connections my server opens by using the per-session ulimit -n command.

I.e., before starting my websocket server, in a console I run:

ulimit -n 12

.. which succeeds (run ulimit -a to confirm)

I then start my server and trace its system calls by prefixing the command with strace:

strace -F -f -e trace=network ./myserver --port 33345

I then make many concurrent client connections. After around 6 to 8 are connections are established, further client attempts fail, and I see this in the server strace output:

[pid 24344] accept4(10, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK) = -1 EMFILE (Too many open files)
[pid 24344] accept4(10, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK) = 9
[pid 24344] accept4(10, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK) = -1 EAGAIN (Resource temporarily unavailable)

Note that new terminal sessions don't see this ulimit, I have not changed any server-wide settings.

Upvotes: 1

Related Questions