简体   繁体   English

如何使用 websockets/ws lib 限制 Websocket 连接的数量?

[英]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.我想将最大 WS 连接数硬限制为最大 100。我正在编写一个 node.js 服务器,我不应该允许超过 100 个连接。

How to limit the number of Websocket connection without touching system ulimit ?如何在触及系统ulimit的情况下限制Websocket连接的数量?

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.只是为了扩展我添加的评论,我可以使用per-session ulimit -n命令来限制我的服务器打开的 websocket 连接的数量。

Ie, before starting my websocket server, in a console I run:即,在启动我的 websocket 服务器之前,在控制台中我运行:

ulimit -n 12

.. which succeeds (run ulimit -a to confirm) .. 成功(运行ulimit -a确认)

I then start my server and trace its system calls by prefixing the command with strace :然后我启动我的服务器并通过在命令前加上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:在建立了大约 6 到 8 个连接后,进一步的客户端尝试失败,我在服务器 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.请注意,新的终端会话看不到这个 ulimit,我没有更改任何服务器范围的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM