0

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?

2
  • how about user ulimit, e.g, ulimit -n 110 Commented Nov 6, 2019 at 15:36
  • @DarrenSmith I'm running some other server in some other port also. so I cant change ulimit.
    – Somnath
    Commented Nov 7, 2019 at 4:13

1 Answer 1

1

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.

3

Not the answer you're looking for? Browse other questions tagged or ask your own question.