定义一个既有流式又有一元响应的单个RPC

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

Defining a single RPC with both a streaming and unary response

问题

可以定义一个单一的proto3 RPC,它可以具有流式响应或一元响应吗?就像我定义RPC时可以明确指定流关键字是可选的吗?

如果请求流式传输,那么响应内容将被流式传输回客户端。如果没有请求流式传输,那么将返回一个包含所有内容的单一响应。

还有其他选项,比如为流式传输与非流式传输分别定义两个单独的RPC,或者在流中返回一个单一的响应(尽管在我看来客户端仍然需要将其视为流式API,不确定那方面的影响)。

获取此行为的正确方法是什么?

英文:

Is it possible to define a single proto3 RPC that can have either a streaming response or a unary response? Like when I define the RPC I can make it clear that the stream keyword is optional?

If streaming is requested, then the response content will be streamed back to the client. If streaming is not requested, then a single response with all content will be returned.

There's other options, like defining two separate RPCs for stream vs. no stream or just return a single response within the stream (the client would still need to consider it a streaming API though AFAICT, not sure about the implications there).

What's the right way to get this behaviour?

答案1

得分: 3

gRPC特定的语法允许stream仅应用于rpc定义,而没有optional stream

您可以返回一个包含单个消息的流。这种情况更适合客户端不知道服务器可能返回多少消息。

您可以定义两个rpc(一元和服务器流式传输)。这种情况更适合客户端知道它将收到单个消息响应。

英文:

The gRPC-specific syntax permits stream to only be applied to rpc definitions and there's no optional stream.

You could return a stream containing a single message. This scenario would be better suited to the client being unaware of how many messages the server may return.

You could define two rpcs (unary and server streaming). This scenario would be better suited to the client knowing that it will receive a single message response.

huangapple
  • 本文由 发表于 2023年6月26日 23:05:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557938.html
匿名

发表评论

匿名网友

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

确定