livery_grpc_server (livery_grpc v0.1.0)

View Source

gRPC request dispatch for a livery handler.

handler/2 turns a routing index (path -> {method, callback module}) into a fun((livery_req:req()) -> livery_resp:resp()) that a dedicated gRPC h2 listener runs for every request. It:

  1. Validates the request is gRPC (POST with an application/grpc content type) and matches the :path to a method.
  2. Reads the request frames from the body, decompresses and decodes them into protobuf messages.
  3. Invokes the callback (Handler:Function(...)) for the method's call kind.
  4. Writes response frames and ends the stream with grpc-status (and an optional grpc-message) in the trailers.

Every gRPC response is HTTP 200; the call outcome travels in the trailers, per the gRPC-over-HTTP/2 spec. A request that fails before dispatch (wrong content type, unknown method) gets a Trailers-Only reply: a single HEADERS block carrying the status.

All four call kinds are supported. Client-streaming and bidirectional read requests through a livery_grpc_stream handle and (for bidi) send replies through it, interleaved, inside the chunked response producer.

Summary

Functions

handler/2 with default options.

Build the gRPC request handler from a routing index.

Types

callback_result()

-type callback_result() ::
          {ok, map() | tuple()} |
          ok |
          {error, livery_grpc_status:status()} |
          {error, {livery_grpc_status:status(), binary()}} |
          {error, {livery_grpc_status:status(), binary(), binary()}}.

ctx()

-type ctx() ::
          #{metadata := [{binary(), binary()}],
            method := livery_grpc_service:method(),
            deadline := timeout(),
            reflection => term(),
            req := livery_req:req()}.

server_opts()

-type server_opts() :: #{compression => livery_grpc_compression:algorithm(), reflection => term()}.

Functions

handler(Index)

-spec handler(#{binary() => {livery_grpc_service:method(), module()}}) ->
                 fun((livery_req:req()) -> livery_resp:resp()).

handler/2 with default options.

handler(Index, Opts)

-spec handler(#{binary() => {livery_grpc_service:method(), module()}}, server_opts()) ->
                 fun((livery_req:req()) -> livery_resp:resp()).

Build the gRPC request handler from a routing index.

Index maps a wire path to {Method, Handler} (see livery_grpc_service:index/1). Opts may set compression for the outbound encoding (default identity).