简体   繁体   English

Node.js 中的阻塞事件循环仍然存在问题吗?

[英]Is blocking event loop in Node.js still problem?

I was thinking about this for a long time.我想了很久。

You have NGINX load balancer, which assigns web requests to particular node.js instance (you can have many of them).您有 NGINX 负载均衡器,它将 Web 请求分配给特定的 node.js 实例(您可以拥有多个)。 If one of those blocks for long time, there are web requests who are assigned to it, and are waiting inside to be run and they are stuck.如果其中之一长时间阻塞,则分配给它的 Web 请求正在内部等待运行并被卡住。 This happens with round-robin load balancer.这发生在循环负载均衡器上。 But you can choose "least-connections" option for NGINX, it means that NGINX will assign incoming web request to node.js instance, which has the lowest number of connections inside it, so web request will not go to node.js instance, where there are more of them waiting inside, it will go to one, which processes them quickly and has because of that less of them waiting inside.但是你可以为 NGINX 选择“least-connections”选项,这意味着 NGINX 会将传入的 web 请求分配给 node.js 实例,它的内部连接数最少,因此 web 请求不会转到 node.js 实例,如果有更多的人在里面等待,它将转到一个,它会快速处理它们并且因为在里面等待的人较少。

So, no need to care about blocking event loop in your code.因此,无需关心代码中的阻塞事件循环。

Am I right?我对吗?

But you can choose "least-connections" option for NGINX但是您可以为 NGINX 选择“最少连接”选项

Suppose Process A has a single client connected, and Process B has 100 clients.假设进程 A 连接了一个客户端,进程 B 有 100 个客户端。

Process A could do something to block it from accepting new connections.进程 A 可以做一些事情来阻止它接受新的连接。 Nginx routes traffic to it anyway.无论如何,Nginx 都会将流量路由到它。 All Nginx knows in this case is that it has 100 open connections to Process B, which is more than Process A.在这种情况下,Nginx 只知道它有 100 个到进程 B 的打开连接,这比进程 A 多。

The least-connections balancing option is of no practical use in this case.在这种情况下, least-connections平衡选项没有实际用途。

So, no need to care about blocking event loop in your code.因此,无需关心代码中的阻塞事件循环。

You should at least somewhat care about this at all times.您至少应该始终在乎这一点。 It's likely that you have things in your code that need to run periodically.您的代码中可能有一些需要定期运行的内容。 If you block, they won't be able to run.如果你阻止,他们将无法运行。

Generally , if you're actually blocking, there's probably something broken or designed poorly in your application.通常,如果您确实在阻塞,那么您的应用程序中可能存在一些损坏或设计不佳的问题。 (Of course there is no problem with blocking if that's what you intend to do, for some particular reason. These reasons are rare.) (当然,如果您出于某种特殊原因打算这样做,则阻止没有问题。这些原因很少见。)

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

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