go - 使用 gRPC : how to get the size of the client-side buffer? 的无缓冲双向数据流

标签 go protocol-buffers grpc

我正在将数据从服务器流式传输到客户端,我希望服务器读取和发送的数据不要超过客户端的缓冲区大小。

给定:

service StreamService {
  rpc Stream(stream Buffer) returns (stream Buffer);
}

message Buffer {
  bytes data = 1;
}

我客户的程序基本上是这样的:

func ReadFromServer(stream StreamService_StreamClient, buf []byte) (n int, err error) {
  // I actually don't need more than len(buf)...
  // How could I send len(buf) while stream is bidirectional...?
  buffer, err := stream.Recv()
  if err != nil {
     return 0,  err
  }
  n = copy(buf, buffer.Data)
  // buf could also be smaller than buffer.Data...
  return n, nil
}

那么当 RPC 的流是双向的,即发送方向被另一个独立的数据流使用时,我如何发送 len(buf) 呢?请注意,我不使用客户端或服务器端缓冲来避免在其中一个终止时丢失数据(我的数据源是 I/O)。

最佳答案

gRPC 没有为此提供任何机制。它仅在发件人需要放慢速度时提供回推。但是内部仍然会发生缓冲,并且不会公开,因为 gRPC 是基于消息的,而不是基于字节的。

您的情况实际上只有两个选项:

  1. 服务器任意分块响应。必要时客户端 Recv() s,任何额外的都是手动管理以备后用。
  2. 客户端发送请求,要求返回准确的金额,然后等待响应。

Note that I don't use client or server-side buffering to avoid loosing data when one of them is terminated (my data-source is an I/O).

这不是它的工作原理。当您执行 Send() 时,无法保证在调用返回时收到它。当您执行 Recv() 时,无法保证在 recv 调用之后收到消息(它可能在调用之前收到)。正在进行缓冲,期间。

关于go - 使用 gRPC : how to get the size of the client-side buffer? 的无缓冲双向数据流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47577031/

相关文章:

python - 看不到输出,只能在终端

string - Golang 不转义字符串变量

file - Golang,倒带文件指针的正确方法

json - 通过 Go 在 Protocol Buffers v3 的 oneOf 字段中使用结构

python - 如何在 Python 中使用 protobuf 映射?

java - "Bridging"来自 gRPC StreamObserver 的 Reactor 的 Flux

go - 带有授权 header 的 307 重定向

go - 未定义 : proto. ProtoPackageIsVersion3

python - 在 Beam 中读取和写入序列化的 protobuf

go - 从不同的包导入原始文件会导致 'missing method protoreflect'