gRPC and messages with extensions

371 views
Skip to first unread message

Michael Gopshtein

unread,
Apr 12, 2016, 11:54:21 AM4/12/16
to grpc.io
I am using gRPC with Java, and the messages that I'm sending may contain extensions. In the Protobuf docs they say:

When parsing a message that might have extensions, you must provide an ExtensionRegistry in which you have registered any extensions that you want to be able to parse.

The question is how I specify the ExtensionRegistry in the code which uses gRPC as a client (and the message parsing is performed inside the gRPC-generated code).

Thanks,
Michael
(also asked on StackOverflow)


Carl Mastrangelo

unread,
Apr 13, 2016, 5:04:31 PM4/13/16
to grpc.io
I don't believe this is currently supported.  We are currently targeting proto3 which mostly does away with extensions in favor of Any types.

Eric Anderson

unread,
Apr 13, 2016, 5:15:20 PM4/13/16
to Carl Mastrangelo, grpc.io
I believe this problem only impacts Java; the other languages that have proto2 support shouldn't have trouble from this because they have global/static extension registries. It seems it would be appropriate to create something similar for protobuf Java. That could either be automatic (say, via ServiceLoader) or via a well-known instance which you would be responsible for registering your extensions with. In either case it'll probably need to be done via an external contribution; we've got many things on our plate and are focusing much more on proto3.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/29c803c0-37f7-419a-8be5-e2a7e861dbf8%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Michael Gopshtein

unread,
Apr 14, 2016, 4:49:45 AM4/14/16
to grpc.io
I understand. Is there a way to receive the message from gRPC as a ByteString and do the parsing by myself then?

Eric Anderson

unread,
Apr 14, 2016, 11:13:56 AM4/14/16
to Michael Gopshtein, grpc.io
On Thu, Apr 14, 2016 at 1:49 AM, Michael Gopshtein <mgops...@gmail.com> wrote:
I understand. Is there a way to receive the message from gRPC as a ByteString and do the parsing by myself then?

You will need to create your own Marshaller, which makes it hard to use our code-generated stubs. That makes things a bit more annoying.

Instead of calling (using the greeter example):
GreeterGrpc.newStub(channel).sayHello(request, observer);

You would have:
MethodDescriptor YOUR_SAY_HELLO = MethodDescriptor.create(
    GreeterGrpc.METHOD_SAY_HELLO.getType(),
    GreeterGrpc.METHOD_SAY_HELLO.getFullMethodName(),
    new YourProtoMarshaller(HelloRequest.getDefaultInstance()),
    new YourProtoMarshaller(HelloReply.getDefaultInstance()));

And then for each call site you would have:
ClientCalls.asyncUnaryCall(
    channel.newCall(YOUR_SAY_HELLO, CallOptions.DEFAULT),
    request, observer);

But that's only client-side. You can reuse the MethodDescriptor no server-side, but creating your own ServerServiceDefinition has a bit more boilerplate because you have to create a ServerCallHandler. Once we have reflection support, you could probably write generic code that fixes things up while also letting you reuse our stubs.

Carl Mastrangelo

unread,
Apr 14, 2016, 1:23:18 PM4/14/16
to grpc.io, mgops...@gmail.com
If you need an example, the proto Json marshaller also needs to add custom serialization and deserialization logic.  You might look at at the marshaller for json, and example of how to use it:

Carl Mastrangelo

unread,
Apr 15, 2016, 6:08:11 PM4/15/16
to grpc.io, mgops...@gmail.com
I also opened up https://github.com/grpc/grpc-java/issues/1678 if you want to follow along. 

Carl Mastrangelo

unread,
Apr 15, 2016, 8:41:46 PM4/15/16
to grpc.io, mgops...@gmail.com
And...  in a fit of Friday fervor, it is now possible to use an extension registry.  

Sync to head (it's in commit https://github.com/grpc/grpc-java/commit/702518af22a22ed192a8f0f7682bd6f1fc753a65 ) and set it with ProtoLiteUtils.setExtensionRegistry.

This API is not stable, and will very likely change once we have a more appropriate long term solution (with custom registries per method, etc.)

Michael Gopshtein

unread,
Apr 17, 2016, 3:08:37 AM4/17/16
to grpc.io, mgops...@gmail.com
Wow, this is great! :) Appreciate the effort, this is really making the process much easier for me. 
Reply all
Reply to author
Forward
0 new messages