他如何处理express/nodejs中的请求优先级? [英] How we he handle requests priority in express/nodejs?

查看:67
本文介绍了他如何处理express/nodejs中的请求优先级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道NodeJ可以比普通Java服务器处理更多的请求,但是如何维护传入请求的FIFO类型的结构?

We all know that NodeJs can handle way more requests than normal Java server, but how does one maintain a FIFO kind of structure for incoming requests?

让我们说一下快速销售,在短时间内成千上万的请求轰炸以购买数量有限的东西?

Lets say a flash sale, where thousands of requests bombard in short amount of time to buy something which is of limited quantity??

我们如何确定哪个请求首先获得产品并将产品状态从可用更改为售罄(仅作为示例)?

How do we decide which request gets to get the product first and change the status of the product from available to sold out (just an example)?

谢谢

推荐答案

在node.js中,一次实际上只运行一个请求(Javascript解释器是单线程的),直到您在本机代码中执行了某种异步操作,然后然后另一个请求开始运行,而另一个正在等待I/O.如果所有请求都使用有限的资源,那只是为了查看哪个请求通过您的代码获得足够的资源来获得资源.如果将服务器集群化以增加可伸缩性,则每个集群都运行一个Javascript线程.

In node.js, only one request actually runs at a time (the Javascript interpreter is single threaded) until you hit some sort of async operation in native code and then another request gets to start running while the other one is waiting for I/O. If there is some limited resource that all the requests are after, it is just a race to see which request gets far enough through your code to get the resource. If you cluster your server for added scalability, then each cluster runs one Javascript thread.

如果您使每个传入请求都在队列中等待,直到所有其他请求都完全完成(可以完成的事情),那么您将严重破坏node.js服务器的可伸缩性以及大多数时间会闲置,等待完成一些I/O操作,因此这似乎是不正确的设计.

If you made each incoming request wait in a queue until all other requests that came before it were completely done (something that could be done), then you'd seriously wreck the scalability of your node.js server and most of the time it would be sitting idle waiting for some I/O operation to be done so it seems unlikely that that is the correct design.

可以说是一次大甩卖,其中成千上万的请求在短时间内轰炸多少时间购买数量有限的东西?

Lets say a flash sale, where thousands of requests bombard in short amount of time to buy something which is of limited quantity??

我们如何确定首先获得产品的哪个请求以及将产品的状态从可用更改为售罄(只需例如)?

How do we decide which request gets to get the product first and change the status of the product from available to sold out (just an example)?

这里的通常方案是仅让通过的第一个请求声明资源已拥有(即使可能同时运行多个资源).是否始终是第一个到达您服务器的请求将不为人知,但是它将很接近,并且用户社区不太可能仅由于的差异而知道是否恰好关闭了几毫秒.两个请求的处理速度.

The usual scheme here is to just let the first request that gets through to claim the resource have it (even though multiple resources may be running at the same time). Whether this is always the request that first arrived at your server or not is not going to be known, but it will be close and the user community is unlikely to know if it happened to be off by a few milliseconds just due to the variance in processing speed of two requests.

您将必须确保访问共享资源(如数据库)的代码对于并发是安全的,并且不会对共享数据进行任何麻烦的假设.

You will have to make sure your code that accesses shared resources (like databases) is safe for concurrency and does not make any troublesome assumptions about shared data.

这篇关于他如何处理express/nodejs中的请求优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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