HTTP回调URL与WebSocket在异步响应方面有何区别?

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

HTTP Callback URL vs. WebSocket for ansynchronous response?

问题

我有两个服务器:Golang 和 Python(2.7)。Python(Bottle)服务器有一个计算密集型任务需要执行,并公开了一个 RESTful URI 来启动该过程的执行。也就是说,Go 服务器发送:

HTTP GET to myserver.com/data

Python 服务器执行计算,并需要通知 Go 服务器处理完成。有两种设计方式可以实现:

  1. Go 发送一个回调 URL/data 给 Python,Python 通过访问该 URL 来响应。例如:

    HTTP GET | myserver.com/data | Data{callbackURI:goserver.com/process/results, Type: POST, response:"processComplete"}

  2. 从 Python 发送基于 WebSocket 的响应给 Go。

哪种设计更合适?其中一种相对于另一种有什么优缺点?除了错误条件(服务器崩溃等),Python 服务器实际上只需要向客户端“通知”计算完成。这是唯一的响应。

负责 Go 服务器的团队对基于 WebSocket/ajax 的 Go 客户端不太熟悉(我也不熟悉。但我从未写过一行 Go :))。#1 看起来更容易,但我不知道它是否是一种被接受的设计方法,还是只是一种 hack?在这方面,有什么推荐的方式可以继续进行?

英文:

I have two servers: Golang and Python (2.7). The Python (Bottle) server has a computation intensive task to perform and exposes a RESTful URI to start the execution of the process. That is, the Go server sends:

HTTP GET to myserver.com/data

The python server performs the computation and needs to inform the Go server of the completion of the processing. There are two ways in which I see this can be designed:

  1. Go sends a callback URL/data to Python and python responds by hitting that URL. E.g:<br/>

    HTTP GET | myserver.com/data | Data{callbackURI:goserver.com/process/results, Type: POST, response:"processComplete"}

  2. Have a WebSocket based response be sent back from Python to Go.

What would be a more suitable design? Are there pros/cons of doing one over the other? Other than error conditions (server crashed etc.,) the only thing that the Python server needs to actually "inform" the client is about completing the computation. That's the only response.

The team working on the Go server is not very well versed with having a Go client based on websockets/ajax (nor do I. But I've never written a single line of Go HTTP回调URL与WebSocket在异步响应方面有何区别? #1 seems to be easier but am not aware of whether it is an accepted design approach or is it just a hack? What's the recommended way to proceed in this regard?

答案1

得分: 3

如果你想要使用RESTful方式实现,那么当客户端请求HTTP GET myserver.com/data时,服务器应该返回一个202 Accepted状态码:

202 Accepted

**请求已被接受进行处理,但处理尚未完成。**请求可能最终被执行,也可能在实际处理时被拒绝。对于这种异步操作,没有重新发送状态码的机制。

202响应是有意保留的。它的目的是允许服务器接受对某个其他进程的请求(例如,每天只运行一次的批处理进程),而不需要用户代理与服务器的连接持续到进程完成。与此响应一起返回的实体应包括请求当前状态的指示以及指向状态监视器或请求完成的估计时间的指针。

Python服务器可以返回一个预计完成时间(ETA)和一个临时资源的URL,以便请求操作的当前状态(例如:myserver.com/temp_data?processing_status)。然后,Go客户端可以通过请求该资源并读取ETA来等待任务完成。一旦处理完成,Python服务器可以返回一个410 Gone状态,并提供新资源的确切URL。

英文:

If you want to do it RESTful, then when the client requests HTTP GET myserver.com/data the server should return a 202 Accepted status code:

>202 Accepted
>
>The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. There is no facility for re-sending a status code from an asynchronous operation such as this.
>
>The 202 response is intentionally non-committal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed. The entity returned with this response SHOULD include an indication of the request's current status and either a pointer to a status monitor or some estimate of when the user can expect the request to be fulfilled.

The Python server could return an ETA and an URL to a temporary resource to request the current status of the operation (e.g.: myserver.com/temp_data?processing_status). Then it's up to the Go client to wait for the task to fulfill by requesting this resource and reading the ETA. Once the processing is done, the Python server could return a 410 Gone status with the definitive URL of the new resource.

答案2

得分: 2

这取决于信号发送的频率。如果每秒发送多次,保持一个 WebSocket 连接可能更合适。否则,使用选项 #1 会有更少的开销,并且更松散地耦合。

英文:

It depends on how often these signal are being sent. If it's many times per second, keeping a websocket open might make more sense. Otherwise, use option #1 since it will have less overhead and be more loosely coupled.

huangapple
  • 本文由 发表于 2014年7月22日 04:08:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/24873787.html
匿名

发表评论

匿名网友

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

确定