v1

package
v1.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

type Decoder[ProtoReq, DomainReq any] func(ctx context.Context, r ProtoReq) (DomainReq, error)

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

type Encoder[DomainResp, ProtoResp any] func(ctx context.Context, r DomainResp) (ProtoResp, error)

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

type MsgAssembler[ProtoMsg any] func(msg []byte) ProtoMsg

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL