简体   繁体   English

如何在grpc中进行有效负载压缩?

[英]How to do payload compression in grpc?

I am working on a grpc based application, where the request data could be up to 500KB and response could be bigger, and so I would like to compress the data. 我正在开发一个基于grpc的应用程序,其中请求数据可能高达500KB,响应可能更大,所以我想压缩数据。 I have a hard time finding documents/examples in grpc on how to do it. 我很难在grpc中找到有关如何操作的文档/示例。 What does set_compression_algorithm in clientContext do? clientContext中的set_compression_algorithm有什么作用? Do I have to set up something on the server side? 我是否必须在服务器端设置一些东西?

Or should I instead forget about compression on grpc, and do compression/decompression into/out of protobuf messages myself? 或者我应该忘记在grpc上压缩,并自己压缩/解压缩进/出protobuf消息?

The grpc::ClientContext::set_compression_algorithm method selects the algorithm to be used for the client call, that is, the data being sent from the client to the server. grpc::ClientContext::set_compression_algorithm方法选择用于客户端调用的算法,即从客户端发送到服务器的数据。

At the server side, you can control the compression options at channel creation time (that is, to be used for all server calls) via grpc::ServerBuilder::SetCompressionOptions (see https://github.com/grpc/grpc/blob/master/include/grpc++/server_builder.h ), which allows you to: 在服务器端,您可以通过grpc::ServerBuilder::SetCompressionOptions 在通道创建时控制压缩选项(即,用于所有服务器调用)(请参阅https://github.com/grpc/grpc/blob) /master/include/grpc++/server_builder.h ),它允许您:

  1. Select which compression algorithms are supposed by the server. 选择服务器应该使用哪种压缩算法。 By default, all algorithms are enabled. 默认情况下,启用所有算法。
  2. Select which compression algorithm will be used by default for all server responses (provided the peer client supports it. If it doesn't, responses will be sent uncompressed). 选择默认情况下将为所有服务器响应使用的压缩算法(前提是对等客户端支持它。如果不支持,则响应将以未压缩的方式发送)。

For one-off call responses at the server, you can use grpc::ServerContext::set_compression_algorithm or grpc::ServerContext::set_compression_level . 对于服务器上的一次性呼叫响应,您可以使用grpc::ServerContext::set_compression_algorithmgrpc::ServerContext::set_compression_level The latter is recommended, as it selects the best algorithm according to the requested compression level that the client is guaranteed to support. 建议使用后者,因为它根据客户端保证支持的请求压缩级别选择最佳算法。

I'll put together hello-world-style examples in the following days. 接下来的几天我会把你好世界风格的例子放在一起。 I've created an issue to track this: https://github.com/grpc/grpc/issues/6297 Feel free to ping there. 我已经创建了一个跟踪它的问题: https//github.com/grpc/grpc/issues/6297随意ping那里。 I'm also implementing some low-level logging in order to display some compression statistics, for you guys to be able to assert compression is actually working (otherwise everything is completely transparent). 我也正在实现一些低级别的日志记录,以显示一些压缩统计信息,因为你们能够断言压缩实际上正在工作(否则一切都是完全透明的)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM