简体   繁体   English

Node.js 如何处理传入的请求?

[英]How does Node.js process incoming requests?

In order to dive into more complex concepts about Node.js, I am doing some research to make sure I understand the principles about the language and the basic building blocks it´s build upon.为了深入了解有关 Node.js 的更复杂的概念,我正在做一些研究,以确保我了解有关该语言的原理及其构建的基本构建块。

As far as I´m concerned, Node.js relies in the reactor pattern to process each incoming request.就我而言,Node.js 依赖于反应器模式来处理每个传入的请求。 This algorithm is based in the Event Demultiplexer algorithm.该算法基于事件解复用器算法。 The second one is in charge of processing the request operation and it´s resources and then, once the operation is finished, it adds the 'result' to the Event Queue .第二个负责处理请求操作及其资源,然后,一旦操作完成,它将“结果”添加到事件队列中 After this process, the Event Loop is now in charge of executing the Handlers for each event.在这个过程之后, 事件循环现在负责为每个事件执行处理程序。

As a single threaded process, I´m struggling to understand how does the Event demultiplexer handle all the incoming operations if the event loop is managing all the event queue tasks in parallel...作为一个单线程进程,如果事件循环并行管理所有事件队列任务,我很难理解事件解复用器如何处理所有传入的操作......

I´ve found this in the Node.js documentation :我在Node.js 文档中找到了这个:

Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background.由于大多数现代内核都是多线程的,它们可以处理在后台执行的多个操作。 When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed.当这些操作之一完成时,内核会通知 Node.js,以便将适当的回调添加到轮询队列中以最终执行。 We'll explain this in further detail later in this topic.我们将在本主题后面更详细地解释这一点。

Does this mean that the Event Demultiplexer tasks are not handled by Node and that Node just gets the result in order to add it to the Event Queue?这是否意味着事件解复用器任务不是由 Node 处理的,而 Node 只是获取结果以便将其添加到事件队列中? Doesn´t that mean that Node is not single threaded?这是否意味着 Node 不是单线程的?

I've found some useful graphics on the web that clearly explain the path that each request follows, but It´s really hard to find some explaining the actual timing in which the thread processes each one.我在网上找到了一些有用的图形,它们清楚地解释了每个请求所遵循的路径,但是很难找到一些解释线程处理每个请求的实际时间的图。

In node.js, it runs your Javascript in a single thread (assuming we're not talking about worker threads or clustering).在 node.js 中,它在单个线程中运行您的 Javascript(假设我们不是在谈论工作线程或集群)。 But, many asynchronous operations in the node.js built-in library such as file I/O or some crypto operations use their own threads to accomplish their task.但是,node.js 内置库中的许多异步操作(例如文件 I/O 或一些加密操作)使用自己的线程来完成任务。 So, when you call an asynchronous operations such as fs.open() to open a file, it transitions to native code, grabs a thread from an internal thread pool and that thread goes about opening the file.因此,当您调用诸如fs.open()类的异步操作来打开文件时,它会转换为本机代码,从内部线程池中获取一个线程,然后该线程开始打开文件。 The fs.open() function then returns back to your Javascript (while the thread continues in the background). fs.open()函数然后返回到您的 Javascript(同时线程在后台继续)。 Sometime later when it finishes its task, the internal thread inserts an event into the nodejs event queue and when nodejs has cycles to check the event queue, it will find that event and run the Javascript callback associated with it to provide the asynchronous result back to your Javascript.稍后当它完成其任务时,内部线程将一个事件插入到 nodejs 事件队列中,当 nodejs 有周期检查事件队列时,它会找到该事件并运行与之关联的 Javascript 回调以将异步结果返回给你的Javascript。

So, even though threads may be involved, your Javascript is all still single threaded through the event loop.因此,即使可能涉及线程,您的 Javascript 仍然是通过事件循环的单线程。

So, nodejs does use native code threads for some internal native code operations.因此,nodejs 确实使用本机代码线程进行一些内部本机代码操作。 Other things like networking and timers are implemented without threads.其他诸如网络和计时器之类的东西是在没有线程的情况下实现的。

Then if I send a request to fetch data from a database in Node, how does the Event Demultiplexer process It?那么如果我在 Node 中发送一个从数据库中获取数据的请求,事件解复用器是如何处理它的呢? Does the main thread stop the event loop to handle that operation and then resumes after the event handler is queued?主线程是否停止事件循环来处理该操作,然后在事件处理程序排队后恢复?

These terms like "Event Demultiplexor" are things you are applying to node.js.诸如“事件解复用器”之类的术语是您应用于 node.js 的东西。 They are not something that node.js uses at all so I'm not entirely sure what you're asking.它们根本不是 node.js 使用的东西,所以我不完全确定你在问什么。

Node.js runs one event at a time. Node.js 一次运行一个事件。 It has no interrupt-driven abilities.它没有中断驱动的能力。 So, it runs one event until that event returns control back to the event loop (by issuing a return from the callback that started everything).因此,它运行一个事件,直到该事件将控制权返回给事件循环(通过从启动一切的回调发出return )。 Now, the original event may not be complete - it may be doing something asynchronous that will trigger another event to announce completion, but it has finished running Javascript for now and has returned control back to the event loop.现在,原始事件可能没有完成——它可能正在做一些异步的事情来触发另一个事件来宣布完成,但它现在已经完成了 Javascript 的运行,并将控制权返回给了事件循环。

When an incoming Fetch request (which is just an incoming http request) arrives at the server, it is first queued by the OS.当传入的 Fetch 请求(只是传入的 http 请求)到达服务器时,它首先由操作系统排队。 Then, when the event loop has a chance to see it, it is added to the node.js event queue and is served in FIFO order when other events that were before it are done processing.然后,当事件循环有机会看到它时,它被添加到 node.js 事件队列中,并在它之前的其他事件完成处理时按 FIFO 顺序提供服务。 Now, the nodejs event loop is not as simple as a single event queue.现在,nodejs 事件循环不像单个事件队列那么简单。 It actually has several different queues for different types of work and has priorities for what gets to run first, but at the simplest level, you can start out thinking of it as a single FIFO queue.它实际上有几个不同的队列用于不同类型的工作,并且对于首先运行的内容具有优先级,但在最简单的层面上,您可以开始将其视为单个 FIFO 队列。 And, nothing is ever interrupted.而且,没有任何事情会被打断。

Pull an event out of the event queue, run the Javascript callback associated with it.从事件队列中拉出一个事件,运行与之关联的 Javascript 回调。 When that callback returns back to the event loop, get the next event and do the same.当该回调返回到事件循环时,获取下一个事件并执行相同的操作。

Events may be added to the event queue either by native code threads (like might be done with file I/O or some crypto operations) or via some polling mechanisms built into the event loop as part of the event loop cycle it checks for certain things that are ready to run (as with networking and timers).事件可以通过本机代码线程(例如可能通过文件 I/O 或某些加密操作完成)或通过事件循环中内置的一些轮询机制添加到事件队列中,作为事件循环周期的一部分,它会检查某些事情准备运行(如网络和计时器)。

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

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