[grpc-java] Get custom method option from interceptors

427 views
Skip to first unread message

ran...@addepar.com

unread,
Jul 18, 2017, 1:40:11 PM7/18/17
to grpc.io
What's the best way to get custom method option data (https://developers.google.com/protocol-buffers/docs/proto3#custom_options) from server interceptors?

Josh Humphries

unread,
Jul 19, 2017, 10:28:47 AM7/19/17
to ran...@addepar.com, grpc.io
I'm guessing you are interested in doing this with Java (since I see "[grpc-java]" in the subject).

You can query the grpc ServiceDescriptor for its "schema descriptor". For proto-generated services, this will be a ProtoFileDescriptorSupplier, that provides access to the FileDescriptor, which in turn contains everything you're looking for.

The slight trick is that the interceptor, with each call, only sees a MethodDescriptor for the method being invoked, which does not appear to have a reference back to its enclosing ServiceDescriptor. So you'd need to construct a "sidecar" map that allows access to the service descriptors from the interceptor(s).

Here's a gist that demonstrates this: https://gist.github.com/jhump/e8f67087ec5a3918f7b270a4a2b83516 (the sidecar map is the MethodOptionsRegistry).

(Word of warning: I have not tried to actually compile and run the gist, so you may find some small errors. But it should at least be instructive in showing everything you actually need.)



----
Josh Humphries
jh...@bluegosling.com

On Tue, Jul 18, 2017 at 1:40 PM, ran.bi via grpc.io <grp...@googlegroups.com> wrote:
What's the best way to get custom method option data (https://developers.google.com/protocol-buffers/docs/proto3#custom_options) from server interceptors?

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/8756e7c5-a164-4ad3-b4d9-0d4f7b5d2239%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ran Bi

unread,
Jul 19, 2017, 1:30:18 PM7/19/17
to Josh Humphries, grpc.io
Thanks, Josh. It is helpful!

On Wed, Jul 19, 2017 at 7:28 AM Josh Humphries <jh...@bluegosling.com> wrote:
I'm guessing you are interested in doing this with Java (since I see "[grpc-java]" in the subject).

You can query the grpc ServiceDescriptor for its "schema descriptor". For proto-generated services, this will be a ProtoFileDescriptorSupplier, that provides access to the FileDescriptor, which in turn contains everything you're looking for.

The slight trick is that the interceptor, with each call, only sees a MethodDescriptor for the method being invoked, which does not appear to have a reference back to its enclosing ServiceDescriptor. So you'd need to construct a "sidecar" map that allows access to the service descriptors from the interceptor(s).

Here's a gist that demonstrates this: https://gist.github.com/jhump/e8f67087ec5a3918f7b270a4a2b83516 (the sidecar map is the MethodOptionsRegistry).

(Word of warning: I have not tried to actually compile and run the gist, so you may find some small errors. But it should at least be instructive in showing everything you actually need.)



----
Josh Humphries
jh...@bluegosling.com

On Tue, Jul 18, 2017 at 1:40 PM, ran.bi via grpc.io <grp...@googlegroups.com> wrote:
What's the best way to get custom method option data (https://developers.google.com/protocol-buffers/docs/proto3#custom_options) from server interceptors?

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.

Eric Anderson

unread,
Jul 20, 2017, 3:38:07 PM7/20/17
to Ran Bi, Josh Humphries, grpc.io
https://github.com/grpc/grpc-java/pull/3086 will add the details to gRPC's MethodDescriptor as well. It also adds ProtoServiceDescriptorSupplier and ProtoMethodDescriptorSupplier for convenience. (The schema object in the service implements the file and service descriptor suppliers, the schema object in the method implements all three.)

It looks like the review stalled. I've poked it. Since it adds an API used by the generated code we can't mark the API ExperimentalApi, so it required a heavier amount of review.

On Wed, Jul 19, 2017 at 10:30 AM, 'Ran Bi' via grpc.io <grp...@googlegroups.com> wrote:
Thanks, Josh. It is helpful!
On Wed, Jul 19, 2017 at 7:28 AM Josh Humphries <jh...@bluegosling.com> wrote:
I'm guessing you are interested in doing this with Java (since I see "[grpc-java]" in the subject).

You can query the grpc ServiceDescriptor for its "schema descriptor". For proto-generated services, this will be a ProtoFileDescriptorSupplier, that provides access to the FileDescriptor, which in turn contains everything you're looking for.

The slight trick is that the interceptor, with each call, only sees a MethodDescriptor for the method being invoked, which does not appear to have a reference back to its enclosing ServiceDescriptor. So you'd need to construct a "sidecar" map that allows access to the service descriptors from the interceptor(s).

Here's a gist that demonstrates this: https://gist.github.com/jhump/e8f67087ec5a3918f7b270a4a2b83516 (the sidecar map is the MethodOptionsRegistry).

(Word of warning: I have not tried to actually compile and run the gist, so you may find some small errors. But it should at least be instructive in showing everything you actually need.)



----
Josh Humphries
jh...@bluegosling.com

On Tue, Jul 18, 2017 at 1:40 PM, ran.bi via grpc.io <grp...@googlegroups.com> wrote:
What's the best way to get custom method option data (https://developers.google.com/protocol-buffers/docs/proto3#custom_options) from server interceptors?

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/8756e7c5-a164-4ad3-b4d9-0d4f7b5d2239%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
Reply all
Reply to author
Forward
0 new messages