如何 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?

查看:13
本文介绍了如何 REST 完全支持创建一个资源,该资源是其他资源的集合并避免由于数据库创建而导致的 HTTP 超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个 Draw 的概念,并且该 Draw 必须始终包含在一个订单中.

In my application I have the concept of a Draw, and that Draw has to always be contained within an Order.

一个 Draw 有一组属性:background_color, font_size, ...

A Draw has a set of attributes: background_color, font_size, ...

引用著名的 REST 论文:

Quoting the famous REST thesis:

任何可以命名的信息都可以是资源:文档或图像,时间服务(例如洛杉矶今天的天气"),其他资源的集合,非虚拟对象(例如一个人),等等.

Any information that can be named can be a resource: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g. 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",
        },
       ]
       }
}

对此的回应如下:

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

这样用户就会知道在数据库中创建了哪些资源.但是,当请求中有很多绘制时,由于所有这些绘制都插入到数据库中,因此响应需要一段时间.想象一下,如果订单有 10.000 次抽奖,则执行 10.000 次插入.

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. Imagine doing 10.000 inserts if an Order has 10.000 draws.

因为我需要向用户提供刚刚创建的绘图的 ID(顺便说一下,创建但未完成,因为在处理订单时,我们实际上使用一些图像处理库构建了绘图),因此他们可以稍后获取它们,我看不出如何以 RESTful 方式处理此问题,避免使 HTTP 请求花费大量时间,但同时为用户提供某种 Id 以进行绘制,以便他们可以获取它们稍后.

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.

你如何处理这种情况?

推荐答案

接受请求批发,排队处理,返回一个代表请求状态的状态 URL.当请求处理完成后,呈现一个表示请求结果的 url.然后,投票.

Accept the request wholesale, queue the processing, return a status URL that represents the state of the request. When the request is finished processing, present a url that represents the results of the request. 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" }

附录:

好吧,有几个选择.

1) 他们可以等待结果处理并在完成后获取 ID,就像现在一样.与我建议的不同之处在于,网络连接的状态与事务的成功或失败无关.

1) They can wait for the result to process and get the IDs when it's done, just like now. 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) 您可以在访问数据库之前预先分配订单 ID,并将其返回给调用者.但请注意,这些资源尚不存在(并且在处理完成之前不会存在).

2) You can pre-assign the order ids before hitting the database, and return those to the caller. But be aware that those resources do not exist yet (and they won't until the processing is completed).

3) 将您的系统加速到超时根本不是问题的地方.

3) Speed up your system to where the timeout is simply not an issue.

这篇关于如何 REST 完全支持创建一个资源,该资源是其他资源的集合并避免由于数据库创建而导致的 HTTP 超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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