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

Add an example of server side compression support #5358

Merged
merged 12 commits into from Mar 6, 2019

Conversation

AmiDavidW
Copy link
Contributor

I was looking for a way to enable the compression of server responses, but cannot find a direct answer, as mentioned here.

I figured out a way to get it working and would like to have some comments and feedbacks.

It would be great if we have an officially recommended way to implement it.

@thelinuxfoundation
Copy link

Thank you for your pull request. Before we can look at your contribution, we need to ensure all contributors are covered by a Contributor License Agreement.

After the following items are addressed, please respond with a new comment here, and the automated system will re-verify.

Regards,
CLA GitHub bot

@thelinuxfoundation
Copy link

Thank you for your pull request. Before we can look at your contribution, we need to ensure all contributors are covered by a Contributor License Agreement.

After the following items are addressed, please respond with a new comment here, and the automated system will re-verify.

Regards,
CLA GitHub bot

server = ServerBuilder.forPort(port)
.compressorRegistry(CompressorRegistry.getDefaultInstance())
.decompressorRegistry(DecompressorRegistry.getDefaultInstance())
.intercept(new CompressionServerInterceptor())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be made clear why you are using an interceptor. Frequently individual methods that know they want compression could enable it by casting StreamObserver to ServerCallStreamObserver and calling setCompression("gzip").

I assume you're using an interceptor here to enable it for all methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the alternative. I could not find a recommended way to enable server response compression and figured out to use ServerInterceptor to do that.

Yes, I want to enable it in one place for all the methods.

I tried ServerCallStreamObserver.setCompression("gzip") and it works. I just have some questions:

  1. Is it always safe to cast StreamObserver to ServerCallStreamObserver?
  2. Can we do it in one place?
  3. Do we need to do explicit check to see if the client accept/prefer compression? If yes, how?
    That's related to one of my previous replies.

Thanks again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it always safe to cast StreamObserver to ServerCallStreamObserver?

Yes, it is safe for the StreamObserver passed to the server by gRPC. It is really unfortunate that it is hard to discover and awkward and ugly, but it's what we were forced to do to 1) add features and 2) not break existing code.

Can we do it in one place?

To do it in one place, use the interceptor. If you want to configure it per-method, use the ServerCallStreamObserver.

Do we need to do explicit check to see if the client accept/prefer compression? If yes, how?

No. We do that in gRPC. Although it does seem the Javadoc should be improved to say it is safe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sent out #5401 to improve documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the explanations and documentation improvement. Provide two examples to show both cases: PerMethod and AllMethods.

Copy link
Member

@ejona86 ejona86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good. A few nits, but most of what's left is just adding some documentation so its a bit more clear to users what they're looking at.

import io.grpc.stub.StreamObserver;

/**
* Server that manages startup/shutdown of a {@code Greeter} server.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we need some more documentation. How about adding here: "Uses an interceptor to enable compression for all responses."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as the review comment.

@thelinuxfoundation
Copy link

Thank you for your pull request. Before we can look at your contribution, we need to ensure all contributors are covered by a Contributor License Agreement.

After the following items are addressed, please respond with a new comment here, and the automated system will re-verify.

Regards,
CLA GitHub bot

@ejona86 ejona86 added the kokoro:run Add this label to a PR to tell Kokoro the code is safe and tests can be run label Mar 6, 2019
@grpc-kokoro grpc-kokoro removed kokoro:run Add this label to a PR to tell Kokoro the code is safe and tests can be run labels Mar 6, 2019
@ejona86
Copy link
Member

ejona86 commented Mar 6, 2019

@carl-mastrangelo, this looks good for a follow-up review.

Copy link
Contributor

@carl-mastrangelo carl-mastrangelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@carl-mastrangelo
Copy link
Contributor

@ejona86 I'll merge once the tests pass.

@sanjaypujare
Copy link
Contributor

It will be really nice to have a README.md describing the example. I notice a new directory "experimental" is being created? We can have one readme for all the examples in this directory - but a readme will be highly desirable.

@carl-mastrangelo
Copy link
Contributor

@sanjaypujare I agree, followup PR okay?

@sanjaypujare
Copy link
Contributor

@carl-mastrangelo sounds good. Who will be contributing the PR?

@carl-mastrangelo
Copy link
Contributor

@sanjaypujare Since you suggested it, I assume you have something in mind. Let's make sure not to overtax our contributors.

@carl-mastrangelo carl-mastrangelo merged commit 1284090 into grpc:master Mar 6, 2019
@carl-mastrangelo
Copy link
Contributor

@AmiDavidW Merged, thanks!

@AmiDavidW
Copy link
Contributor Author

@carl-mastrangelo @ejona86 Great! Thank you guys for the reviews to make it better!

@AmiDavidW AmiDavidW deleted the compression_example_server branch March 7, 2019 23:52
@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants