简体   繁体   English

构建需要很长时间才能响应的 API 的最佳实践是什么?

[英]What is the best practice for building an API that takes a long time to respond?

I am building an API endpoint that has to call multiple external services/DBs and I do not want my users to have to wait for this process to take place, however, the result of this process is essential for my users.我正在构建一个 API 端点,它必须调用多个外部服务/数据库,我不希望我的用户必须等待这个过程发生,但是,这个过程的结果对我的用户来说是必不可少的。

My first thought is to add the request to a queue and return immediately, then at some later time, the user can query a different endpoint for the result.我的第一个想法是将请求添加到队列并立即返回,然后在稍后的某个时间,用户可以查询不同的端点以获取结果。

Is there a better way to go about this?有没有更好的方法来 go 关于这个? Should there be a webhook response instead of asking users to query the API twice?是否应该有一个 webhook 响应而不是要求用户查询 API 两次?

Three main ways I've seen:我见过的三种主要方式:

  1. Client sends the API request and immediately gets back a job number.客户端发送 API 请求并立即返回一个作业号。 The client can then send a different API request with that job number every so often (every minute or so depending upon how long the usual result takes to get) so check on the progress.然后,客户端可以每隔一段时间(每分钟左右,取决于通常的结果需要多长时间)发送带有该作业编号的不同 API 请求,以便检查进度。 On one of those checks the job will be done and the get the data.在其中一项检查中,将完成工作并获取数据。

  2. Client makes awebSocket or socket.io connection.客户端建立webSocketsocket.io连接。 Client sends a request over that websocket/socket.io connection.客户端通过该 websocket/socket.io 连接发送请求。 Server starts working on the result.服务器开始处理结果。 When the result is done, it is immediately sent over the webSocket/socket.io connection back to the client.结果完成后,会立即通过 webSocket/socket.io 连接将其发送回客户端。 The client can then keep the websocket/socket.io connection connected for other requests or close the connection.然后客户端可以为其他请求保持 websocket/socket.io 连接连接或关闭连接。

  3. Use Server-Sent events .使用服务器发送的事件 Send, the query and then when the result is done, the server can send it back on that same connection.发送查询,然后当结果完成后,服务器可以在同一连接上将其发回。

I don't think there's a "best practice" among these three as each have some advantages and some other uses which may be relevant.我认为这三个之间没有“最佳实践”,因为每个都有一些优势和一些可能相关的其他用途。 The polling option #1 is the lowest common denominator and will work in any situation, but requires a polling strategy by the client and may have some latency (result ready before client polls).轮询选项 #1 是最小的公分母,可以在任何情况下工作,但需要客户端的轮询策略并且可能有一些延迟(在客户端轮询之前准备好结果)。

The choices #2 and #3 are both very efficient and their general technology may have other uses also.选择#2 和#3 都非常有效,它们的通用技术也可能有其他用途。

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

相关问题 长时间运行的节点 REST API 需要很长时间才能响应 - Long running Node REST API takes much time to respond FB.getLoginStatus需要很长时间才能响应 - FB.getLoginStatus takes a really long time to respond 如何使用jasmine来测试需要很长时间才能响应的异步函数? - How to use jasmine to test an async function that takes a long time to respond? 使用 react 调用 API 的最佳实践是什么 - What's the best practice to call API with react Promise.All 中长承诺链的最佳实践是什么 - What is the best practice for long promise chainings within Promise.All 在JavaScript中构建HTML的最佳实践 - Best practice for HTML building in JavaScript 使用ng-repeat AngularJS构建大型表后,垃圾收集在Internet Explorer中花费很长时间 - Garbage collection takes a very long time in Internet Explorer after building a large table with ng-repeat AngularJS 服务工作者缓存需要时间来响应延迟 - service worker cache takes time to respond with latency 可选链接是在 React 中加载 API 的最佳实践吗? 还有哪些其他选择? - Is optional chaining the best practice for loading an API in React? What other options are there? API设计-存储诸如HTML之类的富文本的最佳实践是什么 - API Design - What are the best practice to store Rich text like HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM