roadrunner_grpc_protobuf (roadrunner_grpc_protobuf v0.1.0-rc.1)

View Source

Protocol Buffers for roadrunner_grpc, with modules generated by gpb.

service/3 builds a service from what a gpb module knows of it, and the module itself is the codec that encodes and decodes its messages:

roadrunner_listener:start_link(my_listener, #{
    port => 50051,
    protocols => [http2],
    body_buffering => manual,
    routes => roadrunner_grpc:routes(#{
        services => [roadrunner_grpc_protobuf:service(helloworld_pb, ~"helloworld.Greeter", greeter)],
        codecs => [roadrunner_grpc_protobuf]
    })
}).

greeter holds the methods, one function per method named after it in snake case (SayHello is say_hello/2), called as roadrunner_grpc_service describes with messages in the shape the gpb module gives them: maps with its maps option, records without.

The gpb module

The gpb module is generated with services (gpb's default) and its introspection functions (also the default). Each method's message types are {GpbModule, MessageName}, which is how the codec knows the module to call: GpbModule:encode_msg(Message, MessageName) and GpbModule:decode_msg(Bytes, MessageName).

Generated with gpb's descriptor option too, the module describes its .proto files, which the service then gives as its files for gRPC's server reflection (roadrunner_grpc_reflection). Two things in gpb's descriptions are put the way protoc writes them, which tools such as grpcurl expect:

  • gpb names a file after its package's path (helloworld/helloworld.proto) yet its imports as written (import "common.proto"): a file another one imports is named as that import names it, so the import is found; a file none imports keeps gpb's name
  • gpb declares a map field's entry as a message of the file (MapFieldEntry_1_3, its key and value required): each map field gets its entry inside its own message, named after the field (labels has LabelsEntry), its key and value optional

Summary

Types

A method's message type: the gpb module and the message's name in it.

How service/4 builds the service

Functions

The message of type MessageName the bytes hold.

The bytes of Message, by the gpb module that knows its type.

The service Service of GpbModule, its methods in Module, for roadrunner_grpc:routes/1's services. Service is its full gRPC name (a binary, such as ~"helloworld.Greeter") or its name in the gpb module (an atom): 'Greeter', or 'helloworld.Greeter' when the module was generated with gpb's use_packages option.

The proto subtype: application/grpc and application/grpc+proto.

Types

message_type()

-type message_type() :: {module(), atom()}.

A method's message type: the gpb module and the message's name in it.

opts()

-type opts() ::
          #{function_names => snake_case | as_is | fun((binary()) -> atom()),
            unexported => error | unimplemented}.

How service/4 builds the service:

  • function_names: how a method's function is named after it, snake_case (the default, SayHello is say_hello), as_is ('SayHello'), or a function from the method's name to the function's
  • unexported: what to do with a method whose function the module does not export, error (the default, the routes fail to compile) or unimplemented (the method is left out, and a call to it is answered UNIMPLEMENTED)

Functions

decode/2

-spec decode(message_type(), binary()) -> term().

The message of type MessageName the bytes hold.

encode/2

-spec encode(message_type(), term()) -> iodata().

The bytes of Message, by the gpb module that knows its type.

service(GpbModule, Service, Module)

-spec service(module(), atom() | binary(), module()) -> roadrunner_grpc_service:descriptor().

service/4 with the default options.

service/4

-spec service(module(), atom() | binary(), module(), opts()) -> roadrunner_grpc_service:descriptor().

The service Service of GpbModule, its methods in Module, for roadrunner_grpc:routes/1's services. Service is its full gRPC name (a binary, such as ~"helloworld.Greeter") or its name in the gpb module (an atom): 'Greeter', or 'helloworld.Greeter' when the module was generated with gpb's use_packages option.

A GpbModule that is not a module that loads, or has no services, raises {invalid_gpb_module, GpbModule}, a Module that is not an atom {invalid_module, Module}, an unknown service {unknown_service, GpbModule, Service}, and two methods whose functions would have the same name {duplicate_function, Function, Methods}. Bad options raise {invalid_grpc_protobuf_opts, Opts} when they are not a map, {invalid_grpc_protobuf_opt, Key, Value} or {unknown_grpc_protobuf_opt, Key}.

subtype()

-spec subtype() -> binary().

The proto subtype: application/grpc and application/grpc+proto.