简体   繁体   English

如何以REST方式支持创建其他资源的资源,并避免由于数据库创建而导致HTTP超时?

[英]How to RESTfully support the creation of a resource which is a collection of other resources and avoiding HTTP timeouts due to DB creation?

In my application I have the concept of a Draw, and that Draw has to always be contained within an Order. 在我的应用程序中,我具有抽奖的概念,并且该抽奖必须始终包含在订单中。

A Draw has a set of attributes: background_color, font_size, ... 绘图具有一组属性: background_color, font_size, ...

Quoting the famous REST thesis: 引用著名的REST论文:

Any information that can be named can be a resource: a document or image, a temporal service (eg "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (eg a person), and so on. 可以命名的任何信息都可以是资源:文档或图像,临时服务(例如“洛杉矶今天的天气”),其他资源的集合,非虚拟对象(例如人)等。

So, my collection of other resources here would be an Order. 因此,我在这里收集的其他资源将是订单。 An Order is a set of Draws (usually more than thousands). 订单是一组抽奖(通常超过数千)。 I want to let the User create an Order with several Draws, and here is my first approach: 我想让用户创建带有多个抽奖的订单,这是我的第一种方法:

{
    "order": {
      "background_color" : "rgb(255,255,255)", "font_size" : 10,
      "draws_attributes": [{
            "background_color" : "rgb(0,0,0)", "font_size" : 14
        }, {
           "other_attribute" : "value",
        },
       ]
       }
}

A response to this would look like this: 对此的响应如下所示:

"order": {
          "id" : 30,
          "draws": [{
                "id" : 4
            }, {
               "id" : 5
            },
           ]
           }
    }

So the User would know which resources have been created in the DB. 因此,用户将知道已在数据库中创建了哪些资源。 However, when there are many draws in the request, since all those draws are inserted in the DB, the response takes a while. 但是,当请求中有许多绘图时,由于所有这些绘图都已插入到DB中,因此响应需要一段时间。 Imagine doing 10.000 inserts if an Order has 10.000 draws. 想象一下,如果一个订单有10.000次抽奖,则进行10.000次插入。

Since I need to give the User the ID of the draws that were just created (by the way, created but not finished, because when the Order is processed we actually build the Draw with some image manipulation libraries), so they can fetch them later, I fail to see how to deal with this in a RESTful way, avoiding to make the HTTP request take a lot time, but at the same time giving the User some kind of Ids for the draws, so they can fetch them later. 由于我需要为用户提供刚刚创建的绘图的ID(顺便说一句,已创建但尚未完成,因为在处理订单时,我们实际上是使用一些图像处理库来构建绘图的),因此以后可以获取它们,我看不到如何以RESTful方式处理此问题,避免使HTTP请求花费很多时间,但同时为用户提供了一些ID以供绘制,以便他们稍后可以获取它们。

How do you deal with this kind of situations? 您如何处理这种情况?

Accept the request wholesale, queue the processing, return a status URL that represents the state of the request. 接受请求批发,对处理进行排队,返回代表请求状态的状态URL。 When the request is finished processing, present a url that represents the results of the request. 请求完成处理后,请提供一个代表请求结果的URL。 Then, poll. 然后,轮询。

POST /submitOrder

301
Location: http://host.com/orderstatus/1234

GET /orderstatus/1234

200
{ status:"PROCESSING", msg: "Request still processing"}

...

GET /orderstaus/1234

200
{ status:"COMPLETED", msg: "Request completed", rel="http://host.com/orderresults/3456" }

Addenda: 附加物:

Well, there's a few options. 好吧,有一些选择。

1) They can wait for the result to process and get the IDs when it's done, just like now. 1)就像现在一样,他们可以等待结果处理并获得ID。 The difference with what I suggested is that the state of the network connection is not tied to the success or failure of the transaction. 与我建议的区别在于,网络连接的状态与事务的成功或失败无关。

2) You can pre-assign the order ids before hitting the database, and return those to the caller. 2)您可以在访问数据库之前预先分配订单ID,然后将其返回给调用方。 But be aware that those resources do not exist yet (and they won't until the processing is completed). 但是请注意,这些资源尚不存在(并且直到处理完成时它们才会出现)。

3) Speed up your system to where the timeout is simply not an issue. 3)将系统加速到超时根本不是问题的地方。

I think your exposed granularity is too fine - does the user need to be able to modify each Draw separately? 我认为您的公开粒度太好-用户是否需要能够分别修改每个Draw If not, then present a document that represents an Order , and that contains naturally the Draw s. 如果不是,则提供代表Order的文档,该文档自然包含Draw

Will you need to query specific Draw s from the database based on specific criteria that are unrelated to the Order ? 您是否需要根据与Order无关的特定条件从数据库中查询特定的Draw If not, then represent all the Draw s as a single blob that is part of a row that represents the Order . 如果不是,则将所有Draw表示为单个斑点,该斑点是表示Order的行的一部分。

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

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