Node.js / Express和并行队列(Node.js/Express and parallel queues)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
Node.js / Express和并行队列(Node.js/Express and parallel queues)

我们正在构建一个基于Node.js服务器和Express的基础架构。

在服务器中,发生的情况如下:

服务器接受来自客户端的传入HTTP请求。 服务器生成两个文件(这个操作可以是“相对较长”,也就是说0.1秒左右) 服务器将生成的文件(每个〜20-200 KB)上传到外部CDN 服务器响应客户端,这包括CDN上文件的URI

目前,服务器为每个请求按顺序执行此操作,并且这种方式运行得非常好(Node / Express可以自动处理并发请求)。 但是,随着我们计划增长,并发请求的数量可能会增加,并且我们认为对我们来说,实施处理请求的队列会更好。 否则,我们可能会冒险有太多的任务在同一时间运行,并有太多开放的连接到CDN。 快速响应客户并不是一件相关的事情。

我在想的是在Node服务器中有一个独立的部分,它包含一些 “工作人员”(2-3,但我们将进行测试以确定同时操作的正确数量)。 所以,新流程看起来像这样:

接受来自客户端的请求后,服务器将操作添加到队列中。 有2-3名(待测试)工作人员将队列排除在外并执行所有操作(生成文件并将其上传到CDN)。 当工作人员处理了操作(不管是否在较长时间内停留在队列中),它都会通知节点服务器(一个回调),并且服务器响应客户端(它一直在等待) )。

你对这种方法有什么看法? 你相信这是正确的吗?

最重要的是,这可以在Node / Express中实现吗?

感谢您的时间

We are building an infrastructure which features a Node.js server and Express.

In the server, what is happening is as follow:

The server accepts an incoming HTTP request from client. Server generates two files (this operation can be "relatively long", meaning also 0.1 seconds or so) Server uploads the generated files (~20-200 KB each) to an external CDN Server responds to client, and this includes the URI of the file on the CDN

Currently the server is doing this sequentially for each request, and this works quite well (Node/Express can handle concurrent requests automatically). However, as we plan to grow, the number of concurrent requests may grow higher, and we believe it would be better for us to implement a queue for processing requests. Otherwise, we may risk having too many tasks running at the same time and too many open connections to the CDN. Responding to the client quickly is not a relevant thing.

What I was thinking about is to have a separate part in the Node server that contains a few "workers" (2-3, but we will do tests to determine the correct number of simultaneous operations). So, the new flow would look something like:

After accepting the request from the client, the server adds an operation to a queue. There are 2-3 (to be tested) workers that take elements out of the queue and perform all the operations (generate the files and upload them to the CDN). When the worker has processed the operation (doesn't matter if it stays in the queue for a relatively long time), it notifies the Node server (a callback), and the server responds to the client (which has been waiting in the meanwhile).

What do you think of this approach? Do you believe it is the correct one?

Mostly important, HOW could this be implemented in Node/Express?

Thank you for your time

最满意答案

tldr; 您可以使用本地Node.js 集群模块处理大量并发请求。

一些序言: Node.js本身是单线程的。 它的事件循环能够很好地处理多个请求,即使在其单线程模型中也是如此,这是它的最佳特征之一IMO。

真正的交易:那么,我们如何扩展这个范围,以处理更多的并发连接并使用所有可用的CPU? 使用集群模块 。

这个模块的工作方式与@Qualcuno指出的完全一样,它允许您在主服务器后面创建多个工作者(aka进程)来共享负载并更高效地使用可用的CPU。

根据Node.js官方文档:

因为员工都是独立的流程,所以他们可以根据自己的计划需求被杀死或重新生成,而不会影响其他员工。 只要有一些工作人员还活着,服务器将继续接受连接。

所需示例:

var cluster = require('cluster'); var http = require('http'); var numCPUs = require('os').cpus().length; if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); }); } else { // Workers can share any TCP connection // In this case its a HTTP server http.createServer(function(req, res) { res.writeHead(200); res.end("hello world\n"); }).listen(8000); }

希望这是你所需要的。

评论你是否还有其他问题。

(Answering my own question)

According to this question on Stack Overflow a solution in my case would be to implement a queue using Caolan McMahon's async module.

The main application will create jobs and push them into a queue, which has a limit on the number of concurrent jobs that can run. This allows processing tasks concurrently but with a strict control on the limit. It works like Cocoa's NSOperationQueue on Mac OSX.

更多推荐

本文发布于:2023-04-21 18:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6be24d3f4d04728746a3358c39ad36f2.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   js   Node   Express   queues

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!