如何确保REST API按顺序执行传入的请求

huangapple go评论74阅读模式
英文:

How to ensure REST APIs to execute the incoming requests in a sequenced manner

问题

我们有处理来自客户端的并发请求的要求,这些请求是发往我们托管在Jetty服务器上的REST API的,但所有请求都应按顺序执行,

Web服务器能否保证按收到的顺序顺序执行请求?如果不能,有什么解决办法?

假设我们按顺序收到API的请求。

我们对控制器方法进行了同步,以确保每个请求创建的线程按顺序执行,但是如何确保线程按顺序进入控制器。

我们的应用程序是一个独立的应用程序,只运行一个Jetty实例,没有负载均衡器,也没有实现缓存。
英文:

We have a requirement of handling concurrent requests coming from a client to our REST API's which is hosted on the Jetty server, but all the requests should be executed in a sequenced manner that

Does web server guarantee the sequential execution of the requests it receives? If not, what could be the solution?

Assume that we receive the request in sequential order to our APIs.

We Synchronized the Controller method to make sure the threads created per request execute in a sequential manner, but how to guarantee threads come in a sequence to the controller.

Ours is a standalone application and only one Jetty instance is running and no load-balancers, cache implemented.

</details>


# 答案1
**得分**: 1

Jetty将按照客户端要求的顺序处理请求,使用HTTP/1.1和HTTP/2定义的标准行为。

如果是在HTTP/1.1上的单个持久连接,则请求将按照它们在该HTTP/1.1连接上发送的顺序进行处理。

如果客户端使用多个HTTP/1.1连接,则每个单独的连接都会独立处理,就像它们是不同的唯一客户端。

如果客户端使用HTTP/2,则请求会进行多路复用,并且在完整的请求行和头部在该HTTP/2会话上接收时进行处理。

请注意,上述陈述中的客户端是指与Jetty交互的任何HTTP客户端。如果您在Jetty前面有负载均衡器、防火墙、缓存或TLS/SSL卸载器,则该机器将变成您的客户端。确保您正确配置该机器上的连接处理,以满足您的需求。

基本上,Jetty遵守各种HTTP规范,所需的行为完全由客户端控制。如果客户端使用单个HTTP/1.1连接并在其中发送请求流水线,则将获得所需的最终结果。但如果您在浏览器上执行JavaScript,则所有AJAX调用将通过多个活动浏览器连接并行发送。

另一种选择是,在API中人为地强制排序,其中在先前响应中提供的唯一标识符必须在下一个API调用中提供,以便按照所需的顺序进行处理。

您可能希读取过去的答案,以更深入地了解您的非常复杂的要求。

* [https://stackoverflow.com/questions/16288102-is-it-possible-to-receive-out-of-order-responses-with-http](https://stackoverflow.com/questions/16288102-is-it-possible-to-receive-out-of-order-responses-with-http)

<details>
<summary>英文:</summary>

Jetty will process the requests in whatever order the client asks for them, using the standard behavior defined for HTTP/1.1 and HTTP/2.

If it&#39;s a single persistent connection on HTTP/1.1 then the requests are handled in the order they are sent on that HTTP/1.1 connection.

If the client uses multiple HTTP/1.1 connections then each individual connection is handled independently as if they are different unique clients.

If the client uses HTTP/2 then the requests are multiplexed and are handled when the entire request line and headers are received on that HTTP/2 session.

Note that the client in the above statements is whatever HTTP client is talking to Jetty.  If you have a load balancer, or firewall, or cache, or TLS/SSL offloader in front of Jetty, then that machine becomes your client.  Make sure you properly configure the connection handling on that machine to satisfy your needs as well.

Basically, the various HTTP specs are honored by Jetty, and the behavior you are looking for is 100% controlled by the client, if the client uses a single HTTP/1.1 connection and sends the request pipelined within it then you&#39;ll get your desired end result, but if you are javascript on a browser, then you&#39;ll get all of the AJAX calls sent in parallel across multiple active browser connections.

Another option, is that you can also artificially force the ordering into your API where a unique identifier provided in a prior response must be given to the next API call in order to progress along the desired ordered calls.

You might want to read past answers to gain more of an insight to your very difficult requirement.

* https://stackoverflow.com/questions/16288102/is-it-possible-to-receive-out-of-order-responses-with-http


</details>



# 答案2
**得分**: 0

也许不必同步控制器方法,而是在全局顺序信号量上实现一些超时的主动等待。类似于:

```python
STATIC INT SEMAPHORE := START_SEQUENCE
WHILE (INCOMING_REQUEST.ID != SEMAPHORE) {
  TIMEDOUT_WAIT;
}
PROCESS(INCOMING_REQUEST)
SEMAPHORE := SEMAPHORE+1

这是一个简单的解决方案,仍然需要实现一些错误处理。使用这个解决方案时,您还需要对请求进行排序。

英文:

Maybe instead of synchronize the controller method, implement some timed out active waiting on a global sequential semaphore. Something like:

STATIC INT SEMAPHORE := START_SEQUENCE
WHILE (INCOMING_REQUEST.ID != SEMAPHORE) {
  TIMEDOUT_WAIT;
}
PROCESS(INCOMING_REQUEST)
SEMAPHORE := SEMAPHORE+1

This is a simplistic solution, still need to implement some error handling.
With this solution, you also need to sequence the requests

huangapple
  • 本文由 发表于 2020年8月25日 19:26:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63577824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定