Documentation
¶
Overview ¶
Package grpc provides a gRPC binding for endpoints.
Index ¶
- Constants
- func EncodeKeyValue(key, val string) (string, string)
- func Interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...) (resp interface{}, err error)
- type Client
- type ClientFinalizerFunc
- type ClientOption
- func ClientAfter[Request, Response any](after ...ClientResponseFunc) ClientOption[Request, Response]
- func ClientBefore[Request, Response any](before ...ClientRequestFunc) ClientOption[Request, Response]
- func ClientFinalizer[Request, Response any](f ...ClientFinalizerFunc) ClientOption[Request, Response]
- type ClientRequestFunc
- type ClientResponseFunc
- type DecodeRequestFunc
- type DecodeResponseFunc
- type EncodeRequestFunc
- type EncodeResponseFunc
- type Handler
- type Server
- type ServerFinalizerFunc
- type ServerOption
- func ServerAfter[Request, Response any](after ...ServerResponseFunc) ServerOption[Request, Response]
- func ServerBefore[Request, Response any](before ...ServerRequestFunc) ServerOption[Request, Response]
- func ServerErrorHandler[Request, Response any](errorHandler transport.ErrorHandler) ServerOption[Request, Response]
- func ServerErrorLogger[Request, Response any](logger log.Logger) ServerOption[Request, Response]
- func ServerFinalizer[Request, Response any](f ...ServerFinalizerFunc) ServerOption[Request, Response]
- type ServerRequestFunc
- type ServerResponseFunc
Constants ¶
const (
ContextKeyRequestMethod contextKey = iota
)
Variables ¶
This section is empty.
Functions ¶
func EncodeKeyValue ¶
EncodeKeyValue sanitizes a key-value pair for use in gRPC metadata headers.
func Interceptor ¶
func Interceptor( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (resp interface{}, err error)
Interceptor is a grpc UnaryInterceptor that injects the method name into context so it can be consumed by Go kit gRPC middlewares. The Interceptor typically is added at creation time of the grpc-go server. Like this: `grpc.NewServer(grpc.UnaryInterceptor(kitgrpc.Interceptor))`
Types ¶
type Client ¶
type Client[Request, Response any] struct { // contains filtered or unexported fields }
Client wraps a gRPC connection and provides a method that implements endpoint.Endpoint.
func NewClient ¶
func NewClient[Request, Response any]( cc *grpc.ClientConn, serviceName string, method string, enc EncodeRequestFunc[Request], dec DecodeResponseFunc[Response], grpcReply interface{}, options ...ClientOption[Request, Response], ) *Client[Request, Response]
NewClient constructs a usable Client for a single remote endpoint. Pass an zero-value protobuf message of the RPC response type as the grpcReply argument.
type ClientFinalizerFunc ¶
ClientFinalizerFunc can be used to perform work at the end of a client gRPC request, after the response is returned. The principal intended use is for error logging. Additional response parameters are provided in the context under keys with the ContextKeyResponse prefix. Note: err may be nil. There maybe also no additional response parameters depending on when an error occurs.
type ClientOption ¶
ClientOption sets an optional parameter for clients.
func ClientAfter ¶
func ClientAfter[Request, Response any](after ...ClientResponseFunc) ClientOption[Request, Response]
ClientAfter sets the ClientResponseFuncs that are applied to the incoming gRPC response prior to it being decoded. This is useful for obtaining response metadata and adding onto the context prior to decoding.
func ClientBefore ¶
func ClientBefore[Request, Response any](before ...ClientRequestFunc) ClientOption[Request, Response]
ClientBefore sets the RequestFuncs that are applied to the outgoing gRPC request before it's invoked.
func ClientFinalizer ¶
func ClientFinalizer[Request, Response any](f ...ClientFinalizerFunc) ClientOption[Request, Response]
ClientFinalizer is executed at the end of every gRPC request. By default, no finalizer is registered.
type ClientRequestFunc ¶
ClientRequestFunc may take information from context and use it to construct metadata headers to be transported to the server. ClientRequestFuncs are executed after creating the request but prior to sending the gRPC request to the server.
func SetRequestHeader ¶
func SetRequestHeader(key, val string) ClientRequestFunc
SetRequestHeader returns a ClientRequestFunc that sets the specified metadata key-value pair.
type ClientResponseFunc ¶
type ClientResponseFunc func(ctx context.Context, header metadata.MD, trailer metadata.MD) context.Context
ClientResponseFunc may take information from a gRPC metadata header and/or trailer and make the responses available for consumption. ClientResponseFuncs are only executed in clients, after a request has been made, but prior to it being decoded.
type DecodeRequestFunc ¶
DecodeRequestFunc extracts a user-domain request object from a gRPC request. It's designed to be used in gRPC servers, for server-side endpoints. One straightforward DecodeRequestFunc could be something that decodes from the gRPC request message to the concrete request type.
type DecodeResponseFunc ¶
type DecodeResponseFunc[Response any] func(context.Context, interface{}) (response Response, err error)
DecodeResponseFunc extracts a user-domain response object from a gRPC response object. It's designed to be used in gRPC clients, for client-side endpoints. One straightforward DecodeResponseFunc could be something that decodes from the gRPC response message to the concrete response type.
type EncodeRequestFunc ¶
EncodeRequestFunc encodes the passed request object into the gRPC request object. It's designed to be used in gRPC clients, for client-side endpoints. One straightforward EncodeRequestFunc could something that encodes the object directly to the gRPC request message.
type EncodeResponseFunc ¶
type EncodeResponseFunc[Response any] func(context.Context, Response) (response interface{}, err error)
EncodeResponseFunc encodes the passed response object to the gRPC response message. It's designed to be used in gRPC servers, for server-side endpoints. One straightforward EncodeResponseFunc could be something that encodes the object directly to the gRPC response message.
type Handler ¶
type Handler interface {
ServeGRPC(ctx context.Context, request interface{}) (context.Context, interface{}, error)
}
Handler which should be called from the gRPC binding of the service implementation. The incoming request parameter, and returned response parameter, are both gRPC types, not user-domain.
type Server ¶
type Server[Request, Response any] struct { // contains filtered or unexported fields }
Server wraps an endpoint and implements grpc.Handler.
func NewServer ¶
func NewServer[Request, Response any]( e endpoint.Endpoint[Request, Response], dec DecodeRequestFunc[Request], enc EncodeResponseFunc[Response], options ...ServerOption[Request, Response], ) *Server[Request, Response]
NewServer constructs a new server, which implements wraps the provided endpoint and implements the Handler interface. Consumers should write bindings that adapt the concrete gRPC methods from their compiled protobuf definitions to individual handlers. Request and response objects are from the caller business domain, not gRPC request and reply types.
type ServerFinalizerFunc ¶
ServerFinalizerFunc can be used to perform work at the end of an gRPC request, after the response has been written to the client.
type ServerOption ¶
ServerOption sets an optional parameter for servers.
func ServerAfter ¶
func ServerAfter[Request, Response any](after ...ServerResponseFunc) ServerOption[Request, Response]
ServerAfter functions are executed on the gRPC response writer after the endpoint is invoked, but before anything is written to the client.
func ServerBefore ¶
func ServerBefore[Request, Response any](before ...ServerRequestFunc) ServerOption[Request, Response]
ServerBefore functions are executed on the gRPC request object before the request is decoded.
func ServerErrorHandler ¶
func ServerErrorHandler[Request, Response any](errorHandler transport.ErrorHandler) ServerOption[Request, Response]
ServerErrorHandler is used to handle non-terminal errors. By default, non-terminal errors are ignored.
func ServerErrorLogger ¶
func ServerErrorLogger[Request, Response any](logger log.Logger) ServerOption[Request, Response]
ServerErrorLogger is used to log non-terminal errors. By default, no errors are logged. Deprecated: Use ServerErrorHandler instead.
func ServerFinalizer ¶
func ServerFinalizer[Request, Response any](f ...ServerFinalizerFunc) ServerOption[Request, Response]
ServerFinalizer is executed at the end of every gRPC request. By default, no finalizer is registered.
type ServerRequestFunc ¶
ServerRequestFunc may take information from the received metadata header and use it to place items in the request scoped context. ServerRequestFuncs are executed prior to invoking the endpoint.
type ServerResponseFunc ¶
type ServerResponseFunc func(ctx context.Context, header *metadata.MD, trailer *metadata.MD) context.Context
ServerResponseFunc may take information from a request context and use it to manipulate the gRPC response metadata headers and trailers. ResponseFuncs are only executed in servers, after invoking the endpoint but prior to writing a response.
func SetResponseHeader ¶
func SetResponseHeader(key, val string) ServerResponseFunc
SetResponseHeader returns a ResponseFunc that sets the specified metadata key-value pair.
func SetResponseTrailer ¶
func SetResponseTrailer(key, val string) ServerResponseFunc
SetResponseTrailer returns a ResponseFunc that sets the specified metadata key-value pair.