Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress behavior difference between grpc-java and grpc-go #7035

Closed
yann9 opened this issue May 13, 2020 · 1 comment
Closed

Compress behavior difference between grpc-java and grpc-go #7035

yann9 opened this issue May 13, 2020 · 1 comment
Labels

Comments

@yann9
Copy link

yann9 commented May 13, 2020

Client:
grpc-java append grpc-accept-encoding header even if compress calloption has not set.

headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);

    byte[] advertisedEncodings =
        InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(decompressorRegistry);
    if (advertisedEncodings.length != 0) {
      headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);
    }

grpc-go append grpc-accept-encoding header only if compress calloption has set.
https://github.com/grpc/grpc-go/blob/a6ab4473c5a469332c1bdee691293affeaaece25/internal/transport/http2_client.go#L444

 if callHdr.SendCompress != "" {
	headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-encoding", Value: callHdr.SendCompress})
	headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-accept-encoding", Value: callHdr.SendCompress})
 }

Server:

grpc-java will check the grpc-accept-encoding header which client send to server and compression is not enabled by default. Compressor will be set with the grpc-accept-encoding message compression method

if (messageAcceptEncoding != null) {

    if (compressor == null) {
      compressor = Codec.Identity.NONE;
    } else {
      if (messageAcceptEncoding != null) {
        // TODO(carl-mastrangelo): remove the string allocation.
        if (!GrpcUtil.iterableContains(
            ACCEPT_ENCODING_SPLITTER.split(new String(messageAcceptEncoding, GrpcUtil.US_ASCII)),
            compressor.getMessageEncoding())) {
          // resort to using no compression.
          compressor = Codec.Identity.NONE;
        }
      } else {
        compressor = Codec.Identity.NONE;
      }
    }

grpc-go will not check the grpc-accept-encoding header which client send to server and compression is enabled by default. Compressor will be set with the grpc-encoding message compression method
https://github.com/grpc/grpc-go/blob/a6ab4473c5a469332c1bdee691293affeaaece25/server.go#L1123

if s.opts.cp != nil {
	cp = s.opts.cp
	stream.SetSendCompress(cp.Type())
} else if rc := stream.RecvCompress(); rc != "" && rc != encoding.Identity {
	// Legacy compressor not specified; attempt to respond with same encoding.
	comp = encoding.GetCompressor(rc)
	if comp != nil {
		stream.SetSendCompress(rc)
	}
} 

Why? Are there any other reasons for the inconsistency?

@yann9 yann9 added the question label May 13, 2020
@ericgribkoff
Copy link
Contributor

I believe the discrepancy is due to grpc/grpc-go#2786. Go only recently (grpc/grpc-go#3139) added the client-side setting of grpc-accept-encoding, and the open issue identifies that some more items still remain to be done there.

Since this seems to be more of a gRPC Go question - as far as I can tell, gRPC Java is compliant with the spec and compression is advertised/configured as expected - I'm going to mark this closed here. If there's other Java-specific concerns feel free to comment and I will reopen; otherwise for the Go side of things adding a comment to grpc/grpc-go#2786 is probably the right place.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants