grpc

package module
v0.0.0-...-2ccfbba Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Common helper constructs for running a gRPC server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HealthRequest

func HealthRequest(host string, svc []string, reqIDField string, log zerolog.Logger) error

HealthRequest makes a healthcheck request to gRPC service

func LogStreamInterceptor

func LogStreamInterceptor(l zerolog.Logger, fieldName string, tf TraceField) grpc.StreamServerInterceptor

LogStreamInterceptor returns grpc middleware to log stream method calls

func LogUnaryInterceptor

func LogUnaryInterceptor(l zerolog.Logger, fieldName string, tf TraceField) grpc.UnaryServerInterceptor

LogUnaryInterceptor returns grpc middleware to log unary method calls

func NewClient

func NewClient(server string, grpcOpts ...grpc.DialOption) (*grpc.ClientConn, error)

NewClient constructs a grpc client connection

func RequestID

func RequestID(ctx context.Context, fieldName string) string

RequestID extracts the request id from context, if there is no request id a fresh ID is generated

func TraceID

func TraceID(ctx context.Context, fieldName string) string

TraceID extracts the trace ID from the context, if there is no trace id an empty string is returned

Types

type Option

type Option func(*Server)

An Option function can override configuration options for a server

func WithAddress

func WithAddress(addr string) Option

WithAddress overrides the default configured listen address for a server

func WithLogger

func WithLogger(log zerolog.Logger) Option

WithLogger overrides the logger instance

func WithServer

func WithServer(srv *grpc.Server) Option

WithServer overrides the grpc Server instance

type RegisterServiceFunc

type RegisterServiceFunc func(*grpc.Server) string

RegisterServiceFunc registers a service with the gRPC server returning the service name

Example:

var contentManager = func(srv *grpc.Server) string {
	pb.RegisterContentManagerServer(srv, &content.Manager{})
	return "kit.content.v1.ContentManager"
}

type Server

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

A Server can create and stop a gRPC server

Example:

registerSvc := func(s *grpc.Server) string {
	healthpb.RegisterHealthServer(s, hs)
	return "kit.test.v1.Health"
}
s := grpc.New([]grpc.RegisterServiceFunc{registerSvc})
if err := s.Start(); err != nil {
	// handle server runtime err
}
if err := s.Stop(); err != nil {
	// handle server shutdown err
}

func New

func New(services []RegisterServiceFunc, opts ...Option) *Server

New creates a new gRPC server. Provide a slice of service registers and use Option functions to override defaults.

func (*Server) Start

func (s *Server) Start() error

Start starts serving the gRPC server

func (*Server) Stop

func (s *Server) Stop() error

Stop gracefully stops the grpc server

type TraceField

type TraceField struct {
	// RequestFieldName is the name of the trace id field in the request e.g. x-b3-traceid
	RequestFieldName string
	// LoggingFieldName is the name of the trace field to send to the logger e.g. logging.googleapis.com/trace
	LoggingFieldName string
}

TraceField provides the field names for logging the trace

type WrappedServerStream

type WrappedServerStream struct {
	grpc.ServerStream

	WrappedContext context.Context
}

WrappedServerStream is a thin wrapper around grpc.ServerStream that allows modifying context

func (*WrappedServerStream) Context

func (w *WrappedServerStream) Context() context.Context

Context returns the wrapper's WrappedContext, overwriting the nested grpc.ServerStream.Context()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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