简体   繁体   English

用于连接到不同 WebSocket 服务器的多个 WebSocket 客户端的 Node.js 集群?

[英]Node.js Cluster for Multiple WebSocket Clients Connecting to Different WebSocket Servers?

I am using Node.js to implement a Websocket client that subscribes to datafeed from multiple Websocket servers.我正在使用 Node.js 来实现一个 Websocket 客户端,该客户端从多个 Websocket 服务器订阅数据源。

foo = new WebSocket('ws://foo.host ...')
bar = new WebSocket('ws://barhost ...')
baz = new WebSocket('ws://baz.host ...')
qux = new WebSocket('ws://qux.host ...')

foo.on('data', data => doSomething(data))  // 5 events per second 
bar.on('data', data => doSomething(data))  // 1 events per second 
baz.on('data', data => doSomething(data))  // 1 events per second 
qux.on('data', data => doSomething(data))  // 1 events per second 

Question: If we have a multi-core system (eg. 4 cores), is it possible to make use of Node.js Cluster to load balance the processing of the incoming Websocket data, such that each core will approximately receive 2 events per second to be handled?问题:如果我们有一个多核系统(例如4核),是否可以利用Node.js Cluster对传入的Websocket数据进行负载均衡处理,使得每个核大约每秒接收2个事件要处理?

Or is it better to manually start 8 node.js instances and pass it an argument [foo|bar|baz|qux] for selecting the Websocket server it will connect to?或者最好手动启动 8 个 node.js 实例并传递一个参数[foo|bar|baz|qux]来选择它将连接到的 Websocket 服务器?

The nodejs clustering module solves one specific problem. nodejs 集群模块解决了一个特定问题。 When you have a an http server and you want to load balance incoming connections among multiple processes, that's what the nodejs clustering module does.当您有一个 http 服务器并且想要在多个进程之间对传入连接进行负载平衡时,这就是 nodejs 集群模块所做的。 That is not what you have.那不是你所拥有的。 You have multiple client-side outgoing webSocket connections and you apparently want to apply multiple processes to processing the incoming data.您有多个客户端传出 webSocket 连接,并且您显然希望应用多个进程来处理传入数据。 That's completely different than what the nodejs cluster module does.这与 nodejs 集群模块所做的完全不同。

First, it's important to understand that receiving the data is not a CPU intensive process for nodejs.首先,重要的是要了解接收数据对于 nodejs 来说不是 CPU 密集型过程。 The actual socket processing and receiving of incoming data onto the computer is handled by the OS and is outside the nodejs process.实际的套接字处理和接收到计算机上的传入数据由操作系统处理,并且在 nodejs 进程之外。

So, if you actually need more than one CPU to work on this, it must be to process the incoming data, not to just receive it.因此,如果您实际上需要多个 CPU 来处理此问题,则必须处理传入的数据,而不仅仅是接收数据。

There are several different ways you could structure that.有几种不同的方法可以构建它。

  1. You could have one central process that contains all the webSockets and then have and number of worker processes or worker threads that you pass incoming data to for processing.您可以拥有一个包含所有 webSockets 的中央进程,然后拥有多个工作进程或工作线程,您将传入数据传递给它们进行处理。 This would apply many CPUs to the processing of the data and would allow the load procesisng to be spread among the CPUs regardless of which socket the data arrived on.这将应用许多 CPU 来处理数据,并允许在 CPU 之间分散负载处理,而不管数据到达哪个套接字。

  2. You could create 4 separate child processes and have each child process create one of the four webSocket connections and then have each child process handle just the incoming data for its webSocket.您可以创建 4 个独立的子进程,让每个子进程创建四个 webSocket 连接之一,然后让每个子进程只处理其 webSocket 的传入数据。 This has the disadvantage that it only applies one process to each webSocket and if most of the data comes on one webSocket, then the other processes will be largely idle.这样做的缺点是它只对每个 webSocket 应用一个进程,如果大部分数据来自一个 webSocket,那么其他进程将在很大程度上处于空闲状态。

  3. If one webSocket has a lot more load than the others and for some reason option #1 wouldn't work well, then you could combine #1 and #2.如果一个 webSocket 的负载比其他 webSocket 多得多,并且由于某种原因选项 #1 无法正常工作,那么您可以组合 #1 和 #2。 Create a separate process for each webSocket and then have some worker threads for processing the incoming data for each one.为每个 webSocket 创建一个单独的进程,然后有一些工作线程来处理每个传入的数据。 Create a work queue that incoming data is inserted into and work can be sent to each worker thread as it finishes its previous chunk of data.创建一个工作队列,将传入数据插入其中,并在每个工作线程完成其前一个数据块时将工作发送到每个工作线程。

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

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