使用HTTP进行异步RPC回调

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

Async RPC with callback over HTTP

问题

我正在设计一个具有阻塞功能的 SDK(使用 Golang),它应该以异步方式运行,并通过回调返回结果。

我一直在使用 Gorilla Toolkit RPC 库进行尝试,这个库非常好,但我没有找到一种返回异步调用值的方法。

也许我漏掉了什么,RPC 模型不支持异步调用,我的意思是所有调用都是阻塞的,即使是很长时间。如果是这样的话,那么我假设没有 HTTP 超时,对吗?

我应该如何实现通过回调返回异步 RPC 调用的返回值?

**澄清一下:**由于我将从另一种语言通过 HTTP 调用 RPC,所以我不能使用典型的 Go Channel 方法进行回调。所以我的问题是,在进行 RPC 时是否有一种在 HTTP 上实现回调的方法,或者调用是否应该一直阻塞,直到返回一个值为止?

英文:

I am in the process of designing an SDK (in Golang) with blocking functionality, which should be run asynchronously with the result being returned via a callback.

I have been playing with the Gorilla Toolkit RPC library which is great but I don't see a way of returning values of async calls.

Perhaps I am missing something and the RPC model does not support async calls, by that I mean all calls are blocking, even for a long time. If that is the case, i'm assuming there is no HTTP timeout then?

How can I implement async RPC calls with a return value being returned via a callback.

For clarification: I cannot use the typical Go Channel method for callbacks as I will be calling the RPC from another language over HTTP. So my question is, is there a way to implement callbacks over HTTP when doing RPC or should the calls just block, even for a long time until a value is ready to be returned?

答案1

得分: 2

我不了解Gorilla,但在Go的标准库的net/rpc包中,你会找到这个方法net/rpc/Go。它可以异步地进行RPC调用,你可以将一个通道作为参数传递给它,这样当远程过程调用完成时,你就可以获取到RPC的回复。

net/rpc/Call也可以进行RPC调用,但它会阻塞直到调用完成。如果你知道如何使用通道,我相信你可以通过Go方法来实现你想要的功能。

英文:

I don't know about Gorilla, but in the net/rpc package of the standard library of Go you'll find this method net/rpc/Go. It makes a RPC call asynchronously, you give it a channel as parameter so you can retrieve the RPC reply when the Remote Procedure Call is finished.

net/rpc/Call also makes a RPC call but it is blocking until the call is finished. If you know how to use channels I'm sure you'll get what you want with the Go method.

答案2

得分: 0

你可以看一下grpc。这个:

  • 有一个原生的Go实现

  • 对于大多数的"Java, Python等"值都有实现

  • 使用HTTP2进行通信(虽然不完全是HTTP,但非常相似)

  • 是完全异步的(示例是C++)。

英文:

You might take a look at grpc. This:

  • Has a native go implementation

  • Has a implementations for "Java, Python, etc" for most values of 'etc'

  • Works over HTTP2 (OK, not quite http, but pretty similar)

  • Is fully asynchronous (example happens to be C++).

huangapple
  • 本文由 发表于 2016年4月22日 16:21:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/36788606.html
匿名

发表评论

匿名网友

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

确定