grpc-java compression and decompression

926 views
Skip to first unread message

Gokul Reddy

unread,
Jul 1, 2021, 3:19:35 PM7/1/21
to grpc.io
Hi team,

I have been trying to understand the grpc java compression decompression logic, but am having hard time on how to have server to compress response messages based on what client accepts. Here is my understanding so far - 


1. On the client side - using the following link I am setting the compression type to gzip. This is compressing all messages sent from client to server
https://grpc.github.io/grpc-java/javadoc/io/grpc/stub/AbstractStub.html#withCompression-java.lang.String-
2. For messages back from server - I have figured out a way to add interceptor in server builder to add compression with type gzip. This does perform compressing, but this is introducing a constraint on server. If for example I specify 'snappy' (custom codec) here in place of gzip in server builder, (client has default compressor registry, so supports identity and gzip only) my understanding is negotiation fails as 'snappy' is not supported by client and falls back to identity. But in such cases I would want server to do gzip compression rather than identity. Is this possible ? By this server compresses messages based on what client accepts


Thanks in advance!


Thanks and regards,
Gokul

sanjay...@google.com

unread,
Jul 1, 2021, 9:20:43 PM7/1/21
to grpc.io
Couple of points:

>  I have figured out a way to add interceptor in server builder to add compression with type gzip

You don't need an interceptor to add custom codec compression on the server side. Just use ServerBuilder.compressorRegistry(CompressorRegistry) to use your own compressor registry with custom codecs (snappy and gzip) added.

> But in such cases I would want server to do gzip compression rather than identity. Is this possible ?

Unfortunately not because of this code. In that case you can use an interceptor to check the accept-encoding header from the client and based on that set the snappy compression on the call only if client accepts it.
Reply all
Reply to author
Forward
0 new messages