可以将两个http请求放在一起吗?如果可以,nodeJS服务器如何处理它? [英] Can two http request come together? If it can, how nodeJS server handles it?

查看:155
本文介绍了可以将两个http请求放在一起吗?如果可以,nodeJS服务器如何处理它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我在nodeJS上做了一些介绍. 有人问我以下问题:

Yesterday I was giving some presentation on nodeJS. Some one asked me following question:

我们知道nodeJS是一个单线程服务器,有几个请求是 进入服务器,它将所有请求推送到事件循环.如果什么 两个请求正好同时到达服务器,服务器将如何 处理这种情况?

As we know nodeJS is a single threaded server, several request is coming to the server and it pushes all request to event loop. What if two request is coming to server at exact same time, how server will handle this situation?

我猜出一个主意,并回覆了以下内容:

I guessed a thought and replied back following:

我猜 没有两个http请求可以完全相同地发送到服务器 时间,所有请求都通过单个套接字发出,因此它们将排在队列中. Http 请求具有以下格式:

I guess No two http request can come to the server at exact same time, all request come through a single socket so they will be in queue. Http request has following format:

请求的时间戳包含在其标头中,根据其在标头中的时间戳,它们可以被推送到事件循环中.

timestamp of request is contained in it's header and they may be pushed to the event loop depending upon their timestamp in header.

但是我不确定我是对还是错.

but I'm not sure whether I gave him right or wrong answer.

推荐答案

我想没有两个http请求可以完全同时到达服务器, 所有请求都通过管道到达,因此它们将在队列中.

I guess No two http request can come to the server at exact same time, all request come through pipe so they will be in queue.

这部分基本上是正确的.传入的连接进入事件队列,其中一个必须首先放入队列.

This part is basically correct. Incoming connections go into the event queue and one of them has to be placed in the queue first.

如果两个请求恰好同时到达两个服务器怎么办? 服务器会处理这种情况?

What if two request is coming two server at exact same time, how server will handle this situation?

由于服务器在单个进程中侦听单个套接字上的传入TCP连接,因此不能完全同时存在两个传入连接.一个操作系统将由底层操作系统稍早于另一个操作系统进行处理.这样想吧.传入连接是网络连接上的一组数据包.传入的连接之一将比另一个连接先接收数据包.

Since the server is listening for incoming TCP connections on a single socket in a single process, there cannot be two incoming connections at exactly the same time. One will be processed by the underlying operating system slightly before the other one. Think of it this way. An incoming connection is a set of packets over a network connection. One of the incoming connections will have its packets before the other one.

即使您有多个网卡和多个网络链接,因此两个传入的连接实际上可以在同一时刻到达服务器,node.js队列也将通过互斥体和传入的一个来保护并发性.连接将在其他互斥对象之前抢占互斥对象,并在其他互斥对象之前放入事件队列.

Even if you had multiple network cards and multiple network links so two incoming connections could literally arrive at the server at the exact same moment, the node.js queue will be guarded for concurrency by something like a mutex and one of the incoming connections will grab the mutex before the other and get put in the event queue before the other.

首先由OS处理的一个将在另一个之前放入node.js事件队列.当node.js可用于处理事件队列中的下一个项目时,则事件队列中最先进入的那个请求都将首先开始处理.

The one that is processed by the OS first will be put into the node.js event queue before the other one. When node.js is available to process the next item in the event queue, then whichever incoming request was first in the event queue will start processing first.

由于node.js JS执行是单线程的,因此该请求的代码处理将运行其同步代码以完成操作.如果它具有异步操作,则它将启动该异步操作并返回.然后,这将允许处理事件队列中的下一个项目,并且第二个请求的代码将开始运行.它将同步运行直至完成.与第一个请求一样,如果它具有异步操作,则它将启动该异步操作并返回.

Because node.js JS execution is single threaded, the code processing that request will run its synchronous code to completion. If it has an async operation, then it will start that async operation and return. That will then allow the next item in the event queue to be processed and the code for the second request will start running. It will run its synchronous to completion. Like with the first request, if it has an async operation, then it will start that async operation and return.

这时,两个请求都开始了它们的异步操作,然后又返回了,然后才到达事件队列.这些异步操作之一完成后,它将把另一个事件发布到事件队列中,而当node.js的单线程可用时,它将再次处理事件队列中的下一项.如果两个请求都具有大量异步操作,则它们的进度可能会交错,并且都可能在触发异步操作的同时处于运行中"状态,然后返回,直到该异步操作完成,在此过程中,当node.js为免费处理下一个事件.

At that point after both requests have started their async operations and then returned, then its just up to the event queue. When one of those async operations finishes, it will post another event to the event queue and when the single thread of node.js is free, it will again process the next item in the event queue. If both requests have lots of async operations, their progress could interleave and both could be "in-flight" at the same as they fire an async operation and then return until that async operation completes where their processing picks up again when node.js is free to process that next event.

请求的时间戳包含在其标头中,它们可能是 根据报头中的时间戳将其推送到事件循环.

timestamp of request is contained in it's header and they may be pushed to the event loop depending upon their timestamp in header.

这部分不是真的.相同类型的传入事件在到达时被添加到队列中.首先到达的队列首先进入队列-没有任何步骤可以检查时间戳.

This part is not really right. Incoming events of the same type are added to the queue as they arrive. The first ones to arrive go into the queue first - there's isn't any step that examines some timestamp.

这篇关于可以将两个http请求放在一起吗?如果可以,nodeJS服务器如何处理它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆