The preferred method for configuring message compression on both clients and servers is to use encoding.RegisterCompressor
to register an implementation of a compression algorithm. See grpc/encoding/gzip/gzip.go
for an example of how to implement one.
Once a compressor has been registered on the client-side, RPCs may be sent using it via the UseCompressor
CallOption
. Remember that CallOption
s may be turned into defaults for all calls from a ClientConn
by using the WithDefaultCallOptions
DialOption
. If UseCompressor
is used and the corresponding compressor has not been installed, an Internal
error will be returned to the application before the RPC is sent.
Server-side, registered compressors will be used automatically to decode request messages and encode the responses. Servers currently always respond using the same compression method specified by the client. If the corresponding compressor has not been registered, an Unimplemented
status will be returned to the client.
There is a deprecated API for setting compression as well. It is not recommended for use. However, if you were previously using it, the following section may be helpful in understanding how it works in combination with the new API.
There are two legacy functions and one new function to configure compression:
func WithCompressor(grpc.Compressor) DialOption {}
func WithDecompressor(grpc.Decompressor) DialOption {}
func UseCompressor(name) CallOption {}
For outgoing requests, the following rules are applied in order:
UseCompressor
is used, messages will be compressed using the compressor named.WithCompressor
is used, messages will be compressed using that compressor implementation.For incoming responses, the following rules are applied in order:
WithDecompressor
is used and it matches the message's encoding, it will be used.Unimplemented
status error will be returned to the application.There are two legacy functions to configure compression:
func RPCCompressor(grpc.Compressor) ServerOption {}
func RPCDecompressor(grpc.Decompressor) ServerOption {}
For incoming requests, the following rules are applied in order:
RPCDecompressor
is used and that decompressor matches the request's encoding: it will be used.Unimplemented
status will be returned to the client.For outgoing responses, the following rules are applied in order:
RPCCompressor
is used, that compressor will be used to compress all response messages.