简体   繁体   English

延迟节点中传入HTTP请求的处理

[英]Defer handling of incoming HTTP requests in Node

I am deliberately keeping this at a high level because I suspect I'm missing some fundamental concept here. 我故意将其保持在较高的水平,因为我怀疑这里缺少一些基本概念。

I am running a Node/Express server. 我正在运行节点/ Express服务器。 I want this server to effectively act as a throttle, so I want to store (in memory or another process, like Redis) incoming http requests to be handled (with response.end()) at a later time. 我希望该服务器有效地起到节流阀的作用,因此我想存储(在内存或其他进程(如Redis)中) 传入的 HTTP请求,以便稍后处理(使用response.end())。 Is this possible? 这可能吗?

Is this possible? 这可能吗?

No. You could store state/information about the request anywhere you want, but the request itself will fundamentally be tied back to some file descriptor that ties back to an actual open socket handling the request, and there's really no way to move that around. 不需要。您可以在任意位置存储有关请求的状态/信息,但是请求本身将基本上绑定到某个文件描述符,该文件描述符与处理该请求的实际打开套接字相关联,并且实际上没有办法进行移动。

Is this possible? 这可能吗?

Yes, but with some limitations. 是的,但有一些限制。

First off, if you aren't going to change the design of the client making the request, you will HAVE to respond on the very socket that the HTTP connection came in on. 首先,如果您不打算更改发出请求的客户端的设计,则必须在HTTP连接进入的套接字上进行响应。 An HTTP connection is a live socket connection from client to server. HTTP连接是从客户端到服务器的实时套接字连接。 The response must be sent on that socket. 响应必须在该套接字上发送。 That means if you're going to wait awhile before responding that you will have to keep that socket alive and find a way to respond before the socket hits a timeout and gives up waiting for the response. 这意味着,如果要在响应之前等待一会儿,则必须使该套接字保持活动状态,并找到一种方法来在套接字达到超时并放弃等待响应之前进行响应。 Browsers will all have some sort of client side timeout set and if they don't receive a response within that amount of time, they will close the socket and assume the request just wasn't successful. 浏览器都将设置某种客户端超时设置,如果它们在该时间内没有收到响应,它们将关闭套接字并假定请求未成功。 You would have to either respond in less time than this timeout or you would have to coerce the client into lengthening it's timeout by dribbling bits and pieces of some sort of response out slowly to make the client think that data is coming (I haven't tried this myself). 您可能需要在比此超时时间更短的时间内做出响应,或者您必须通过缓慢地滴加某种形式的响应来强迫客户端延长超时时间,以使客户端认为数据即将到来(我没有我自己尝试过)。

FYI, incoming connections cannot be "stored" in another process or on disk. 仅供参考,传入的连接不能“存储”在另一个进程或磁盘中。 You will have to keep track of them in your node/express server. 您将必须在节点/快速服务器中跟踪它们。


If you can change the design of the client, then there are all sorts of things you could do. 如果您可以更改客户端的设计,那么您可以做各种各样的事情。 For example, the client could make a request and the response it gets immediately could be a unique ID and a time value. 例如,客户端可以发出请求,并且它立即获得的响应可以是唯一的ID和时间值。 This tells the client that it's response has been "deferred" until later. 这告诉客户端其响应已“推迟”到以后。 Meanwhile, the server stores the request and sometime before the scheduled deferred time, it can compute that response. 同时,服务器存储该请求,并且在计划的延迟时间之前的某个时间,它可以计算该响应。 Then, when the client checks back in again and asks for the response for a particular unique ID, the server can then return that response. 然后,当客户端再次签回并要求提供特定唯一ID的响应时,服务器可以返回该响应。


If you can use a socket.io (or even a plain webSocket) connection, then you can do it a little easier. 如果您可以使用socket.io(甚至是普通的webSocket)连接,则可以轻松一点。 Since the webSocket-based connection is persistent, the client can make the request and then sometime later whenever the server feels like it, it can send the response to that client's webSocket connection. 由于基于WebSocket的连接是持久的,因此客户端可以发出请求,然后在服务器感到满意后的某个时候,它将响应发送到该客户端的webSocket连接。

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

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