livery_grpc_server (livery_grpc v0.1.1)
View SourcegRPC 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:
- Validates the request is gRPC (
POSTwith anapplication/grpccontent type) and matches the:pathto a method. - Reads the request frames from the body, decompresses and decodes them into protobuf messages.
- Invokes the callback (
Handler:Function(...)) for the method's call kind. - Writes response frames and ends the stream with
grpc-status(and an optionalgrpc-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
Types
-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()}}.
-type ctx() :: #{metadata := [{binary(), binary()}], method := livery_grpc_service:method(), deadline := timeout(), reflection => term(), req := livery_req:req()}.
-type server_opts() :: #{compression => livery_grpc_compression:algorithm(), reflection => term()}.
Functions
-spec handler(#{binary() => {livery_grpc_service:method(), module()}}) -> fun((livery_req:req()) -> livery_resp:resp()).
handler/2 with default options.
-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).