简体   繁体   English

Node.js CPU 在多个 CPU 内核上负载平衡 Websocket 客户端

[英]Node.js CPU Load balancing Websocket Client over Multiple CPU Cores

My node.js app currently subscribes to a number of websocket servers that is starting to push a lot of data over to the app every second.我的 node.js 应用程序当前订阅了许多 websocket 服务器,这些服务器开始每秒向应用程序推送大量数据。 This websocket client app has several event handlers that does some work upon receiving the websocket data.这个 websocket 客户端应用程序有几个事件处理程序,它们在接收 websocket 数据时做一些工作。

However, Node.js appears to be only using 1 CPU core at any one time, leave the remaining cores under utilized.然而,Node.js 似乎在任何时候都只使用 1 个 CPU 核心,而其余的核心未被利用。 This is expected as Node.js uses a single-threaded event loop model.这是预期的,因为 Node.js 使用单线程事件循环模型。

Is it possible to load balance the incoming websocket data handling over multiple CPU cores?是否可以在多个 CPU 内核上对传入的 websocket 数据处理进行负载平衡? I understand that Node Cluster and pm2 Cluster Mode are able to load balance if you are running websocket servers, but how about websocket clients?我知道如果您正在运行 websocket 服务器,节点集群pm2集群模式能够实现负载平衡,但是 websocket 客户端呢?

From the client side, I can think of the following options:从客户端,我可以想到以下选项:

  1. Create some child processes (likely one for each CPU core you have) and then divide your webSocket connections among those child processes.创建一些子进程(可能为您拥有的每个 CPU 核心创建一个),然后在这些子进程之间划分您的 webSocket 连接。

  2. Create node.js WorkerThreads (likely one for each CPU core you have) and then divide your webSocket connections among those WorkerThreads.创建 node.js WorkerThreads (可能为您拥有的每个 CPU 内核创建一个),然后在这些 WorkerThreads 之间划分您的 webSocket 连接。

  3. Create node.js WorkerThreads (likely one for each CPU core you have) and create a work queue where each incoming piece of data from the various webSocket connections is put into the work queue.创建 node.js WorkerThreads (可能为您拥有的每个 CPU 内核创建一个)并创建一个工作队列,其中来自各种 webSocket 连接的每个传入数据都被放入工作队列。 Then, the WorkerThreads are regular dispatched data from the queue to work on.然后,WorkerThreads 是从队列中定期分派的数据以进行处理。 As they finish a piece of data, they are given the next piece of data from the queue and so on...当他们完成一条数据时,他们会从队列中获得下一条数据,依此类推……

How to best solve this issue really depends upon where the CPU time is mostly being spent.如何最好地解决这个问题实际上取决于 CPU 时间主要花在哪里。 If it is the processing of the incoming data that is taking the most time, then any of these solutions will help apply multiple CPUs to that task.如果处理传入数据的时间最长,那么这些解决方案中的任何一个都将有助于将多个 CPU 应用于该任务。 If it is the actual receiving of the incoming data, then you may need to move the incoming webSockets themselves to a new thread/process as in the first two options.如果是实际接收传入数据,那么您可能需要像前两个选项一样将传入的 webSockets 本身移动到新的线程/进程。

If it's a system bandwidth issue due to the volume of data, then you may need to increase the bandwidth of your network connection of may need multiple network adapters involved.如果这是由于数据量导致的系统带宽问题,那么您可能需要增加网络连接的带宽,可能需要涉及多个网络适配器。

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

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