grpc

package
v0.0.0-...-863def0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package grpc provides a gRPC binding for endpoints.

Index

Constants

View Source
const (
	// ContextKeyRequestMethod ctx key request method
	ContextKeyRequestMethod contextKey = iota
	ContextKeyIsClientStream
	ContextKeyIsServerStream
)

Variables

This section is empty.

Functions

func EncodeKeyValue

func EncodeKeyValue(key, val string) (string, string)

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 micro gRPC middlewares. The Interceptor typically is added at creation time of the grpc-go server. Like this: `grpc.NewServer(grpc.UnaryInterceptor(grpc.Interceptor))`

func StreamServerInterceptor

func StreamServerInterceptor(
	ctx context.Context, srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo,
	handler grpc.StreamHandler,
) error

StreamHandler defines the handler called by gRPC server to complete the execution of a streaming RPC.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps a gRPC connection and provides a method that implements endpoint.Endpoint.

func NewClient

func NewClient(
	cc *grpc.ClientConn,
	serviceName string,
	method string,
	encRequestStream EncodeRequestStream,
	decResponseStream DecodeResponseStream,
	grpcReply interface{},
	options ...ClientOption,
) *Client

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.

func (Client) Endpoint

func (c Client) Endpoint() endpoint.Endpoint

Endpoint returns a usable endpoint that will invoke the gRPC specified by the client.

type ClientFinalizerFunc

type ClientFinalizerFunc func(ctx context.Context, err error)

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

type ClientOption func(*Client)

ClientOption sets an optional parameter for clients.

func ClientAfter

func ClientAfter(after ...ClientResponseFunc) ClientOption

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(before ...ClientRequestFunc) ClientOption

ClientBefore sets the RequestFuncs that are applied to the outgoing gRPC request before it's invoked.

func ClientFinalizer

func ClientFinalizer(f ...ClientFinalizerFunc) ClientOption

ClientFinalizer is executed at the end of every gRPC request. By default, no finalizer is registered.

type ClientRequestFunc

type ClientRequestFunc func(context.Context, *metadata.MD) context.Context

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

type DecodeRequestFunc func(context.Context, interface{}) (request interface{}, err error)

DecodeRequestFunc func impl DecodeRequestStream

func (DecodeRequestFunc) DecodeRequest

func (fn DecodeRequestFunc) DecodeRequest(ctx context.Context, req interface{}) (request interface{}, err error)

DecodeRequest

type DecodeRequestStream

type DecodeRequestStream interface {
	DecodeRequest(context.Context, interface{}) (request interface{}, err error)
}

DecodeRequestStream request decode func interface

type DecodeResponseFunc

type DecodeResponseFunc func(context.Context, interface{}) (response interface{}, err error)

DecodeResponseFunc func impl DecodeResponseStream

func (DecodeResponseFunc) DecodeResponse

func (fn DecodeResponseFunc) DecodeResponse(ctx context.Context, res interface{}) (response interface{}, err error)

DecodeResponse

type DecodeResponseStream

type DecodeResponseStream interface {
	DecodeResponse(context.Context, interface{}) (response interface{}, err error)
}

DecodeResponseStream response decode func interface

type EncodeRequestFunc

type EncodeRequestFunc func(context.Context, interface{}) (request interface{}, err error)

EncodeRequestFunc impl EncodeRequestStream

func (EncodeRequestFunc) EncodeRequest

func (fn EncodeRequestFunc) EncodeRequest(ctx context.Context, req interface{}) (request interface{}, err error)

EncodeRequest function interface

type EncodeRequestStream

type EncodeRequestStream interface {
	EncodeRequest(context.Context, interface{}) (request interface{}, err error)
}

EncodeRequestStream request encode func interface

type EncodeResponseFunc

type EncodeResponseFunc func(context.Context, interface{}) (response interface{}, err error)

EncodeResponseFunc func impl EncodeResponseStream

func (EncodeResponseFunc) EncodeResponse

func (fn EncodeResponseFunc) EncodeResponse(ctx context.Context, res interface{}) (response interface{}, err error)

EncodeResponse

type EncodeResponseStream

type EncodeResponseStream interface {
	EncodeResponse(context.Context, interface{}) (response interface{}, err error)
}

EncodeResponseStream response encode func interface

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 struct {
	// contains filtered or unexported fields
}

Server wraps an endpoint and implements grpc.Handler.

func NewServer

func NewServer(
	e endpoint.Endpoint,
	decodeRequestStream DecodeRequestStream,
	encodeResponseStream EncodeResponseStream,
	options ...ServerOption,
) *Server

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.

func (Server) ServeGRPC

func (s Server) ServeGRPC(ctx context.Context, req interface{}) (retctx context.Context, resp interface{}, err error)

ServeGRPC implements the Handler interface.

type ServerFinalizerFunc

type ServerFinalizerFunc func(ctx context.Context, err error)

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

type ServerOption func(*Server)

ServerOption sets an optional parameter for servers.

func ServerAfter

func ServerAfter(after ...ServerResponseFunc) ServerOption

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(before ...ServerRequestFunc) ServerOption

ServerBefore functions are executed on the gRPC request object before the request is decoded.

func ServerErrorHandler

func ServerErrorHandler(errorHandler transport.ErrorHandler) ServerOption

ServerErrorHandler is used to handle non-terminal errors. By default, non-terminal errors are ignored.

func ServerErrorLogger

func ServerErrorLogger(logger log.Logger) ServerOption

ServerErrorLogger is used to log non-terminal errors. By default, no errors are logged. Deprecated: Use ServerErrorHandler instead.

func ServerFinalizer

func ServerFinalizer(f ...ServerFinalizerFunc) ServerOption

ServerFinalizer is executed at the end of every gRPC request. By default, no finalizer is registered.

type ServerRequestFunc

type ServerRequestFunc func(context.Context, metadata.MD) context.Context

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.

Directories

Path Synopsis
cmd
pb

Jump to

Keyboard shortcuts

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