Documentation
¶
Overview ¶
Package v1 provides generic gRPC transport layer utilities.
It defines the Decoder, Encoder and Handler contracts with generic type constraints for building type-safe unary gRPC handlers, together with the server-streaming utilities: MsgAssembler and StreamSendMsg (chunked-send primitives), and StreamHandler with its functional counterpart ServeStream (the "compute + chunked send" composition).
Channel-based streaming transport (a channel-driven counterpart of StreamSendMsg and StreamHandler) is planned; see the TODO(channel-transport) markers.
Index ¶
- func ServeStream[ProtoReq, ProtoResp, ProtoMsg any](ctx context.Context, h Handler[ProtoReq, ProtoResp], req ProtoReq, ...) error
- func StreamSendMsg[ProtoMsg any](stream grpc.ServerStream, data []byte, chunksize int, ...) error
- type Decoder
- type Encoder
- type Handler
- type MsgAssembler
- type StreamHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ServeStream ¶
func ServeStream[ProtoReq, ProtoResp, ProtoMsg any]( ctx context.Context, h Handler[ProtoReq, ProtoResp], req ProtoReq, stream grpc.ServerStream, chunksize int, extractor func(ProtoResp) []byte, assembler MsgAssembler[ProtoMsg], ) error
ServeStream computes the full response via the unary Handler pipeline (decode -> endpoint -> encode), then sends it over the server stream in chunks of at most chunksize bytes. It is the functional counterpart of StreamHandler: it keeps Handler unary while encapsulating the "compute + chunked send" composition.
extractor pulls the payload bytes from the computed ProtoResp; assembler wraps each chunk into the stream's protobuf message. Send errors (including io.EOF) are returned unwrapped so callers can inspect them with errors.Is.
func StreamSendMsg ¶
func StreamSendMsg[ProtoMsg any](stream grpc.ServerStream, data []byte, chunksize int, assembler MsgAssembler[ProtoMsg]) error
StreamSendMsg sends data over a gRPC server stream in chunks of at most chunksize bytes. Each chunk is assembled into a protobuf message by the assembler before sending. It is used for chunked unary-over-stream responses and server-side streaming pushes.
Send errors (including io.EOF) are returned unwrapped so that callers can inspect them with errors.Is. Empty data sends no message.
Types ¶
type Decoder ¶
Decoder defines the request decoder interface. It converts a protobuf request type to a domain request type. ProtoReq: protobuf request type DomainReq: domain request type
type Encoder ¶
Encoder defines the response encoder interface. It converts a domain response type to a protobuf response type. DomainResp: domain response type ProtoResp: protobuf response type
type Handler ¶
type Handler[ProtoReq, ProtoResp any] interface { ServeGRPC(ctx context.Context, request ProtoReq) (context.Context, ProtoResp, error) }
Handler defines the gRPC handler interface with generic type constraints. ProtoReq is the protobuf request type, ProtoResp is the protobuf response type.
func NewHandler ¶
func NewHandler[ProtoReq, DomainReq, DomainResp, ProtoResp any](ep epv1.Endpoint[DomainReq, DomainResp], decoder Decoder[ProtoReq, DomainReq], encoder Encoder[DomainResp, ProtoResp]) Handler[ProtoReq, ProtoResp]
NewHandler creates a new gRPC handler with the given endpoint, decoder, and encoder. The handler provides type-safe conversion between protobuf types and domain types.
type MsgAssembler ¶
MsgAssembler assembles a chunk of bytes into a protobuf message. ProtoMsg: protobuf message type carried by the stream
type StreamHandler ¶
type StreamHandler[ProtoReq, ProtoMsg any] interface { ServeGRPCStream(ctx context.Context, request ProtoReq, stream grpc.ServerStream) error }
StreamHandler defines the server-streaming gRPC handler interface. ProtoReq is the protobuf request type; ProtoMsg is the protobuf message type carried by the stream. It encapsulates the full "compute + chunked send" pipeline so that pb-service streaming methods only need to call ServeGRPCStream.
func NewStreamHandler ¶
func NewStreamHandler[ProtoReq, DomainReq, DomainResp, ProtoResp, ProtoMsg any]( ep epv1.Endpoint[DomainReq, DomainResp], decoder Decoder[ProtoReq, DomainReq], encoder Encoder[DomainResp, ProtoResp], chunksize int, extractor func(ProtoResp) []byte, assembler MsgAssembler[ProtoMsg], ) StreamHandler[ProtoReq, ProtoMsg]
NewStreamHandler creates a server-streaming handler. It wires the unary pipeline (endpoint/decoder/encoder) together with the chunked-send concern (chunksize/extractor/assembler), keeping the unary Handler reusable.