使用 REST 实现长时间运行的搜索的最佳实践 [英] Best practice for implementing long-running searches with REST

查看:43
本文介绍了使用 REST 实现长时间运行的搜索的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 REST 服务的一部分,我需要实现搜索调用.基本思想是用户可以发布一个新的搜索,并获取结果:

As part of a REST Service, I need to implement a search call. The basic idea is that the user can POST a new search, and GET the results:

POST http://localhost/api/search
GET http://localhost/api/search?id=123

但是,我的搜索可能会运行几分钟,并在完成之前返回部分结果.也就是说,GET-Request 将返回如下内容:

However, my search may run a few minutes, and return partial results until it is done. That is, the GET-Request would return something like:

status: running
results: a, b, c.

而下一个 GET 请求可能会返回

while the next GET-Request might return

status: completed
results: a, b, c, d, e.

这与 RESTful GET 请求的语义相矛盾.多次调用时,请求应始终返回相同的结果.例如,当用户使用缓存代理时,完整的结果可能永远不会传递给用户.

This contradicts the semantics of a RESTful GET request. The request should always return the same result when called several times. For example, when the user uses a caching proxy, the full results might never be delivered to the user.

问题:有没有什么办法可以为长时间运行的搜索提供真正的 RESTful 实现?

Question: Is there any way to provide a truly RESTful implementation for long running searches with partial results?

推荐答案

在执行搜索时,您可以设置适当的响应头(例如 Expiresmax-age>) 表示不应缓存响应 (HTTP/1.1 14.9.313.4).

While the search is executing, you could set the appropriate response headers (e.g. Expires or max-age) to indicate that the response should not be cached (HTTP/1.1 14.9.3, 13.4).

搜索结果完成后,您可以发送更合适的 Expires/max-age 标头以允许或扩展结果的可缓存性.

Once the search result is complete, you could then send a more appropriate Expires / max-age header to allow or extend the cacheability of the result.

重新查询资源的负担将由客户端承担,直到其搜索状态完成.客户端可能会使用 Expires 标头的值来确定何时应该重新查询更新的结果.

The burden would be on the client to re-query the resource until its search status is complete. The client could maybe use the value of the Expires header to determine when it should re-query for updated results.

除此之外,您还可以使用自定义 2XX 状态代码来指示结果尚未完成.也许是 HTTP/1.1 299 In Progress,或者任何有意义的东西.该规范表明 HTTP 状态代码是可扩展的.

Alongside of this, you could also use a custom 2XX status code to indicate that the result is not yet complete. Maybe a HTTP/1.1 299 In Progress, or whatever makes sense. The spec indicates that HTTP status codes are extensible.

作为记录,您的声明:

这与 RESTful GET 请求的语义相矛盾.多次调用时,请求应始终返回相同的结果.

This contradicts the semantics of a RESTful GET request. The request should always return the same result when called several times.

不适用于 GET 请求 - 资源可以更改.GET 请求是幂等的仅意味着 ...N > 0 个相同请求的副作用与单个请求相同".[规范]

is not true for GET requests - resources can change. That GET requests are idempotent only means that "...the side-effects of N > 0 identical requests is the same as for a single request". [spec]

这篇关于使用 REST 实现长时间运行的搜索的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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