grpc

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2021 License: MIT Imports: 25 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncryptedErrorScrubber added in v0.2.0

func EncryptedErrorScrubber(key string) func(error) error

EncryptedErrorScrubber replaces unknown or internal errors with a generic error object. The original error message is encrypted and added to the error message in the status.Details

func ListenAndServe

func ListenAndServe(ctx context.Context, addr string, srv *grpc.Server) error

ListenAndServe serves an gRPC server over TCP

func New

func New(cfg *Config) (*grpc.Server, error)

New creates a new gRPC server with the given options

Example:

srv, _ := server.New(&server.Config{
  Options: []server.Option{
   server.WithCredentials("cert.pem","key.pem","ca.pem"),
   server.WithTracing("opentracing:6818", "my-service")
   server.WithLogging("my-service"),
   server.WithMetrics("my-service"),
   server.WithRecovery(),
   server.WithReflection()),
  },
  Extras: []grpc.ServerOption{
   grpc.MaxReceiveMsgSize(4<<12),
   grpc.MaxSendMsgSize(4<<12),
  },
  Register: func(srv, *grpc.Server){
   myservice.RegisterMyFooServiceServer(srv, myFooServiceImpl)
   myservice.RegisterMyBarServiceServer(srv, myBarServiceImpl)
  },
}

server.ListenAndServe(":3001", srv)

func NoopErrorScrubber added in v0.2.0

func NoopErrorScrubber(err error) error

NoopErrorScrubber returns the error unmodified, this can be useful in debug or development environments

func SimpleErrorScrubber added in v0.2.0

func SimpleErrorScrubber(err error) error

SimpleErrorScrubber replaces unknown or internal errors with a generic error object

func StreamErrorInterceptor added in v0.2.0

func StreamErrorInterceptor(scrubber func(error) error) grpc.StreamServerInterceptor

StreamErrorInterceptor ensure internal or unknown errors do not leak information

func StreamServerInterceptor

func StreamServerInterceptor() grpc.StreamServerInterceptor

StreamServerInterceptor returns a new streaming server interceptor that adds logrus.Entry to the context.

func UnaryErrorInterceptor added in v0.2.0

func UnaryErrorInterceptor(scrubber func(error) error) grpc.UnaryServerInterceptor

UnaryErrorInterceptor ensures internal or unknown errors do not leak information

func UnaryServerInterceptor

func UnaryServerInterceptor() grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptors that adds logrus.Entry to the context.

Types

type Config

type Config struct {
	Options  []Option
	Extras   []grpc.ServerOption
	Register func(*grpc.Server)
}

Config contains the configuration for your server. See New() for example usage

type Option

type Option interface {
	GetOptions() (grpc.ServerOption, grpc.StreamServerInterceptor, grpc.UnaryServerInterceptor, error)
	PostProcess(s *grpc.Server) error
}

Option is the interface for the package supplied configuration helpers

func WithCredentials

func WithCredentials(cert, key, ca string) Option

WithCredentials configures the server to use the given cert/key combination. If a ca file is supplied it is used to verify clients which now are required to have a certificate.

func WithErrorScrubbing added in v0.2.0

func WithErrorScrubbing(scrubber func(error) error) Option

WithErrorScrubbing ensures that internal or unknown errors do not leak information. You may pass a function to customize the scrubbing behavior. The package provides two simple scrubbers: SimpleErrorScrubber, EncryptedErrorScrubber, and NoopErrorScrubber for convenience.

func WithLogging

func WithLogging(app string) Option

WithLogging configures a logrus logger to log all gRPC requests with duration and return status

func WithMDLogging

func WithMDLogging() Option

WithMDLogging configures a logrus logger to log all gRPC requests with duration and return status

func WithMetrics

func WithMetrics() Option

WithMetrics configures the server to collect usage metrics in prometheus format

func WithRecovery

func WithRecovery() Option

WithRecovery recovers the server from panics caused inside of the gRPC calls

func WithReflection

func WithReflection() Option

WithReflection configures the server to provide an API for external tools to get informations about the used services and types. It enables a service which returns the proto definitions for the methods, so that you can use tools like [grpcurl](https://github.com/fullstorydev/grpcurl) without the need to supply the protofile to interact with the service.

func WithTracing

func WithTracing(serverDeprecated, serviceName string) Option

WithTracing configures the server to support tracing in the opentracing format. It attaches a root span to the context of all incoming requests.

The tracer is configured via the JAEGER_* env variables.

The server variable is deprecated and will be ignored.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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