grpcserver

package
v0.39.2-en-pebble Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InterceptorLogger

func InterceptorLogger(l zerolog.Logger) logging.Logger

InterceptorLogger adapts a zerolog.Logger to interceptor's logging.Logger This code is simple enough to be copied and not imported.

func IrrecoverableCtxInjector

func IrrecoverableCtxInjector(signalerCtx *atomic.Pointer[irrecoverable.SignalerContext]) grpc.UnaryServerInterceptor

IrrecoverableCtxInjector injects the irrecoverable signaler context into requests so that the API handler and backend can use the irrecoverable system to handle exceptions.

It injects the signaler context into the original gRPC context via a context value using the key irrecoverable.SignalerContextKey. If signalerCtx is not set yet (because the grpc server has not finished initializing), the original context is passed unchanged.

func LoggingInterceptor

func LoggingInterceptor(log zerolog.Logger) grpc.UnaryServerInterceptor

LoggingInterceptor returns a grpc.UnaryServerInterceptor that logs incoming GRPC request and response

Types

type GrpcServer

type GrpcServer struct {
	component.Component
	// contains filtered or unexported fields
}

GrpcServer wraps `grpc.Server` and allows to manage it using `component.Component` interface. It can be injected into different engines making it possible to use single grpc server for multiple services which live in different modules.

func NewGrpcServer

func NewGrpcServer(log zerolog.Logger,
	grpcListenAddr string,
	grpcServer *grpc.Server,
	grpcSignalerCtx *atomic.Pointer[irrecoverable.SignalerContext],
) *GrpcServer

NewGrpcServer returns a new grpc server.

func (*GrpcServer) GRPCAddress

func (g *GrpcServer) GRPCAddress() net.Addr

GRPCAddress returns the listen address of the GRPC server. Guaranteed to be non-nil after Engine.Ready is closed.

func (*GrpcServer) RegisterService

func (g *GrpcServer) RegisterService(fn func(*grpc.Server))

RegisterService calls the provided function with the grpc server as an argument to register the server with an external service.

type GrpcServerBuilder

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

GrpcServerBuilder created for separating the creation and starting GrpcServer, cause services need to be registered before the server starts.

func NewGrpcServerBuilder

func NewGrpcServerBuilder(log zerolog.Logger,
	gRPCListenAddr string,
	maxMsgSize uint,
	rpcMetricsEnabled bool,
	apiRateLimits map[string]int,
	apiBurstLimits map[string]int,
	opts ...Option,
) *GrpcServerBuilder

NewGrpcServerBuilder creates a new builder for configuring and initializing a gRPC server.

The builder is configured with the provided parameters such as logger, gRPC server address, maximum message size, API rate limits, and additional options. The builder also sets up the necessary interceptors, including handling irrecoverable errors using the irrecoverable.SignalerContext. The gRPC server can be configured with options such as maximum message sizes and interceptors for handling RPC calls.

If RPC metrics are enabled, the builder adds the gRPC Prometheus interceptor for collecting metrics. Additionally, it can enable a state stream interceptor based on the configuration. Rate limiting interceptors can be added based on specified API rate limits. Logging and custom interceptors are applied, and the final gRPC server is returned.

If transport credentials are provided, a secure gRPC server is created; otherwise, an unsecured server is initialized.

Note: The gRPC server is created with the specified options and is ready for further configuration or starting.

func (*GrpcServerBuilder) Build

func (b *GrpcServerBuilder) Build() *GrpcServer

type Option

type Option func(*GrpcServerBuilder)

func WithStreamInterceptor

func WithStreamInterceptor() Option

WithStreamInterceptor sets the StreamInterceptor option to grpc server.

func WithTransportCredentials

func WithTransportCredentials(transportCredentials credentials.TransportCredentials) Option

WithTransportCredentials sets the transport credentials parameters for a grpc server builder.

type RateLimiterInterceptor

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

RateLimiterInterceptor is a gRPC interceptor that applies rate limits to incoming requests.

func NewRateLimiterInterceptor

func NewRateLimiterInterceptor(log zerolog.Logger, apiRateLimits map[string]int, apiBurstLimits map[string]int) *RateLimiterInterceptor

NewRateLimiterInterceptor creates a new rate limiter interceptor with the defined per second rate limits and the optional burst limit for each API.

func (*RateLimiterInterceptor) UnaryServerInterceptor

func (interceptor *RateLimiterInterceptor) UnaryServerInterceptor(
	ctx context.Context,
	req interface{},
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (resp interface{}, err error)

UnaryServerInterceptor returns a grpc.UnaryServerInterceptor that applies rate limits to request based on the limits defined when creating the RateLimiterInterceptor

Jump to

Keyboard shortcuts

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