grpc

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2022 License: MIT Imports: 34 Imported by: 0

Documentation

Overview

Package grpc implements a smart wrapper over the grpc ecosystem. It provides bootstrapping for launching a production ready grpc server alongside a grpc gateway server.

It also wraps the most common grpc parameters used in the company in the options.

Index

Constants

This section is empty.

Variables

View Source
var ErrServerClosed = errors.New("go-commons.grpc: server closed")

ErrServerClosed indicates that the operation is now illegal because of the server has been closed.

Functions

This section is empty.

Types

type Server

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

Server holds the grpc and gateway underlying servers. It starts and stops both of them together. In case one of the server fails the other one is closed.

func NewServer

func NewServer(opt ...ServerOption) (*Server, error)

NewServer creates Server with both the grpc server and, if it's the case, the gateway server.

The servers have not started to accept requests yet.

func (*Server) Close

func (s *Server) Close() error

Close closes both underlying servers. Safe to use concurrently and can be called multiple times.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts accepting incoming connections on both servers. If one of the servers encounters an error, both are stopped.

type ServerOption

type ServerOption interface {
	// contains filtered or unexported methods
}

A ServerOption sets options such as credentials, codec and keepalive parameters, etc.

The main purpose of Options in this package is to wrap the most common grpc parameter used in the company in order to provide the developers with a single point of entry for configuring different projects grpc servers.

func WithAddress

func WithAddress(a string) ServerOption

WithAddress configures the Server to listen to the given address in case of the Gateway server. And in case of the grpc server it uses the same address but port-1.

func WithDebug

func WithDebug(logger *zap.Logger) ServerOption

WithDebug enables debugLogger logging for the servers.

func WithDebugStandardLibraryEndpoints

func WithDebugStandardLibraryEndpoints() ServerOption

WithDebugStandardLibraryEndpoints registers the debug routes from the standard library to the gateway.

func WithGRPCListener

func WithGRPCListener(lis net.Listener) ServerOption

WithGRPCListener configures the GRPC server to use the given net.Listener instead of configuring an address. ! Prefer to use this only in testing.

func WithGRPCServerOptions

func WithGRPCServerOptions(opts []grpc.ServerOption) ServerOption

WithGRPCServerOptions configures the grpc server to use the given grpc server options.

func WithHTTPMiddlewares

func WithHTTPMiddlewares(mw chi.Middlewares) ServerOption

WithHTTPMiddlewares configures the Gateway Server http handler to use the provided middlewares.

func WithMuxOptions

func WithMuxOptions(opts []runtime.ServeMuxOption) ServerOption

WithMuxOptions configures the underlying runtime.ServeMux of the Gateway Server. The ServeMux as a handler for the http server.

func WithNoGateway

func WithNoGateway() ServerOption

WithNoGateway disables the gateway server. ! Prefer to use this only in testing.

func WithRegisterGatewayFunc

func WithRegisterGatewayFunc(f registerGatewayFunc) ServerOption

WithRegisterGatewayFunc registers a GRPC service to the Gateway server.

func WithRegisterServerFunc

func WithRegisterServerFunc(f registerServerFunc) ServerOption

WithRegisterServerFunc registers a GRPC service to the GRPC server.

func WithTracing

func WithTracing() ServerOption

WithTracing enables tracing for both servers.

func WithUnaryServerInterceptor

func WithUnaryServerInterceptor(
	unaryInterceptor grpc.UnaryServerInterceptor,
) ServerOption

WithUnaryServerInterceptor adds an interceptor to the GRPC server.

func WithUnaryServerInterceptorAuthFunc

func WithUnaryServerInterceptorAuthFunc(
	authFunc grpc_auth.AuthFunc,
) ServerOption

WithUnaryServerInterceptorAuthFunc adds an interceptor to the GRPC server that executes a per-request auth.

func WithUnaryServerInterceptorCodeGen

func WithUnaryServerInterceptorCodeGen() ServerOption

WithUnaryServerInterceptorCodeGen adds an interceptor to the GRPC server that exports log fields from requests.

func WithUnaryServerInterceptorHandleErr

func WithUnaryServerInterceptorHandleErr(
	handleErr func(context.Context, error) error,
) ServerOption

WithUnaryServerInterceptorHandleErr adds an interceptor to the GRPC server that intercepts and handles the error returned by the handler.

func WithUnaryServerInterceptorLogger

func WithUnaryServerInterceptorLogger(logger *zap.Logger) ServerOption

WithUnaryServerInterceptorLogger adds an interceptor to the GRPC server that adds the given zap.Logger to the context.

func WithUnaryServerInterceptorRecovery

func WithUnaryServerInterceptorRecovery(
	recoveryHandler grpc_recovery.RecoveryHandlerFunc,
) ServerOption

WithUnaryServerInterceptorRecovery adds an interceptor to the GRPC server that recovers from panics.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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