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
Add an example of server side compression support #5358
Conversation
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, |
bd10a4a
to
43736b4
Compare
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, |
43736b4
to
9550a2d
Compare
examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldClient.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldClient.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressionServerInterceptor.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressionServerInterceptor.java
Outdated
Show resolved
Hide resolved
server = ServerBuilder.forPort(port) | ||
.compressorRegistry(CompressorRegistry.getDefaultInstance()) | ||
.decompressorRegistry(DecompressorRegistry.getDefaultInstance()) | ||
.intercept(new CompressionServerInterceptor()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- Is it always safe to cast StreamObserver to ServerCallStreamObserver?
- Can we do it in one place?
- 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
examples/src/main/java/io/grpc/examples/experimental/CompressionServerInterceptor.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldServerPerMethod.java
Outdated
Show resolved
Hide resolved
import io.grpc.stub.StreamObserver; | ||
|
||
/** | ||
* Server that manages startup/shutdown of a {@code Greeter} server. |
There was a problem hiding this comment.
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."
There was a problem hiding this comment.
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.
examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldServerAllMethods.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldServerPerMethod.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldServerPerMethod.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressionServerInterceptor.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressionServerInterceptor.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldServerAllMethods.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressingHelloWorldServerPerMethod.java
Outdated
Show resolved
Hide resolved
examples/src/main/java/io/grpc/examples/experimental/CompressionServerInterceptor.java
Outdated
Show resolved
Hide resolved
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, |
907abf7
to
a111ab7
Compare
@carl-mastrangelo, this looks good for a follow-up review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@ejona86 I'll merge once the tests pass. |
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. |
@sanjaypujare I agree, followup PR okay? |
@carl-mastrangelo sounds good. Who will be contributing the PR? |
@sanjaypujare Since you suggested it, I assume you have something in mind. Let's make sure not to overtax our contributors. |
@AmiDavidW Merged, thanks! |
@carl-mastrangelo @ejona86 Great! Thank you guys for the reviews to make it better! |
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.