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?

Somnath
  • 3,247
  • 4
  • 28
  • 42

1 Answers1

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.

Darren Smith
  • 2,261
  • 16
  • 16
  • I m running some other server in some other port also. so I cant change system ulimit. That I have already specified in my question. – Somnath Nov 07 '19 at 04:09
  • this is not changing the *system* limit, but the limits associated with a shell. So can be set for starting individual processes. More here: https://unix.stackexchange.com/questions/75996/modify-ulimit-open-files-of-a-specific-process and https://unix.stackexchange.com/questions/55319/are-limits-conf-values-applied-on-a-per-process-basis – Darren Smith Nov 07 '19 at 07:50
  • Additionally, you can modify your node.js server to sets the ulimit itself, see here: https://stackoverflow.com/questions/8141057/can-i-set-ulimit-from-node-js – Darren Smith Nov 07 '19 at 07:52