grpc

package
v1.0.1-0...-336119c Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2023 License: MIT Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxConnectionAge is the duration a connection may exist before shutdown
	MaxConnectionAge = 600 * time.Second
	// MaxConnectionAgeGrace is the maximum duration a
	// connection will be kept alive for outstanding RPCs to complete
	MaxConnectionAgeGrace = 60 * time.Second
	// KeepAliveTime is the period after which a keepalive ping is sent on the
	// transport
	KeepAliveTime = 1200 * time.Second
)
View Source
const (
	XRequestIDMetadataKey = "x-request-id"
)

Variables

This section is empty.

Functions

func Context

func Context(ctx context.Context) context.Context

Context sets a requestID into the parent context and return the new context that can be used in case there is no request ID. In case the parent context contains a requestID then it is returned as the newly created context, otherwise a new context is created with a requestID

func FromContext

func FromContext(ctx context.Context) string

FromContext return the request ID set in context

func GetBufDialer

func GetBufDialer(listener *bufconn.Listener) func(context.Context, string) (net.Conn, error)

func GetClientConn

func GetClientConn(ctx context.Context, addr string) (*grpc.ClientConn, error)

GetClientConn return a grpc client connection

func GetInProcessClientConn

func GetInProcessClientConn(ctx context.Context, listener *bufconn.Listener, options []grpc.DialOption) (*grpc.ClientConn, error)

GetInProcessClientConn creates an in-process grpc client

func GetInProcessServer

func GetInProcessServer(options []grpc.ServerOption) (*grpc.Server, *bufconn.Listener)

GetInProcessServer creates an in-process grpc server

func NewRecoveryStreamInterceptor

func NewRecoveryStreamInterceptor() grpc.StreamServerInterceptor

NewRecoveryStreamInterceptor recovers from an unexpected panic Recovery handlers should typically be last in the chain so that other middleware (e.g. logging) can operate on the recovered state instead of being directly affected by any panic

func NewRecoveryUnaryInterceptor

func NewRecoveryUnaryInterceptor() grpc.UnaryServerInterceptor

NewRecoveryUnaryInterceptor recovers from an unexpected panic Recovery handlers should typically be last in the chain so that other middleware (e.g. logging) can operate on the recovered state instead of being directly affected by any panic

func NewRequestIDStreamClientInterceptor

func NewRequestIDStreamClientInterceptor() grpc.StreamClientInterceptor

NewRequestIDStreamClientInterceptor creates a new request ID stream client interceptor. This interceptor adds a request ID to each outgoing context

func NewRequestIDStreamServerInterceptor

func NewRequestIDStreamServerInterceptor() grpc.StreamServerInterceptor

NewRequestIDStreamServerInterceptor creates a new request ID interceptor. This interceptor adds a request ID to each grpc request

func NewRequestIDUnaryClientInterceptor

func NewRequestIDUnaryClientInterceptor() grpc.UnaryClientInterceptor

NewRequestIDUnaryClientInterceptor creates a new request ID unary client interceptor. This interceptor adds a request ID to each outgoing context

func NewRequestIDUnaryServerInterceptor

func NewRequestIDUnaryServerInterceptor() grpc.UnaryServerInterceptor

NewRequestIDUnaryServerInterceptor creates a new request ID interceptor. This interceptor adds a request ID to each grpc request

func NewTracingClientStreamInterceptor

func NewTracingClientStreamInterceptor(opts ...otelgrpc.Option) grpc.StreamClientInterceptor

NewTracingClientStreamInterceptor helps gather traces and metrics from any grpc stream client request. Make sure to start the TracerProvider to connect to an OLTP connector

func NewTracingClientUnaryInterceptor

func NewTracingClientUnaryInterceptor(opts ...otelgrpc.Option) grpc.UnaryClientInterceptor

NewTracingClientUnaryInterceptor helps gather traces and metrics from any grpc unary client request. Make sure to start the TracerProvider to connect to an OLTP connector

func NewTracingStreamInterceptor

func NewTracingStreamInterceptor(opts ...otelgrpc.Option) grpc.StreamServerInterceptor

NewTracingStreamInterceptor helps gather traces and metrics from any grpc stream server request. Make sure to start the TracerProvider to connect to an OLTP connector

func NewTracingUnaryInterceptor

func NewTracingUnaryInterceptor(opts ...otelgrpc.Option) grpc.UnaryServerInterceptor

NewTracingUnaryInterceptor helps gather traces and metrics from any grpc unary server request. Make sure to start the TracerProvider to connect to an OLTP connector

Types

type ClientBuilder

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

ClientBuilder is grpc client builder

func NewClientBuilder

func NewClientBuilder() *ClientBuilder

NewClientBuilder creates an instance of ClientBuilder

func (*ClientBuilder) GetConn

func (b *ClientBuilder) GetConn(ctx context.Context, addr string) (*grpc.ClientConn, error)

GetConn returns the client connection to the server

func (*ClientBuilder) GetTLSConn

func (b *ClientBuilder) GetTLSConn(ctx context.Context, addr string) (*grpc.ClientConn, error)

GetTLSConn returns client connection to the server

func (*ClientBuilder) WithBlock

func (b *ClientBuilder) WithBlock() *ClientBuilder

WithBlock the dialing blocks until the underlying connection is up. Without this, Dial returns immediately and connecting the server happens in background.

func (*ClientBuilder) WithClientTransportCredentials

func (b *ClientBuilder) WithClientTransportCredentials(insecureSkipVerify bool, certPool *x509.CertPool) *ClientBuilder

WithClientTransportCredentials builds transport credentials for a gRPC client using the given properties.

func (*ClientBuilder) WithDefaultStreamInterceptors

func (b *ClientBuilder) WithDefaultStreamInterceptors() *ClientBuilder

WithDefaultStreamInterceptors sets the default stream interceptors for the grpc server

func (*ClientBuilder) WithDefaultUnaryInterceptors

func (b *ClientBuilder) WithDefaultUnaryInterceptors() *ClientBuilder

WithDefaultUnaryInterceptors sets the default unary interceptors for the grpc server

func (*ClientBuilder) WithInsecure

func (b *ClientBuilder) WithInsecure() *ClientBuilder

WithInsecure set the connection as insecure

func (*ClientBuilder) WithKeepAliveParams

func (b *ClientBuilder) WithKeepAliveParams(params keepalive.ClientParameters) *ClientBuilder

WithKeepAliveParams set the keep alive params ClientParameters is used to set keepalive parameters on the client-side. These configure how the client will actively probe to notice when a connection is broken and send pings so intermediaries will be aware of the liveness of the connection. Make sure these parameters are set in coordination with the keepalive policy on the server, as incompatible settings can result in closing of connection.

func (*ClientBuilder) WithOptions

func (b *ClientBuilder) WithOptions(opts ...grpc.DialOption) *ClientBuilder

WithOptions set dial options

func (*ClientBuilder) WithStreamInterceptors

func (b *ClientBuilder) WithStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) *ClientBuilder

WithStreamInterceptors set a list of interceptors to the Grpc client for stream connection By default, gRPC doesn't allow one to have more than one interceptor either on the client nor on the server side. By using `grpc_middleware` we are able to provides convenient method to add a list of interceptors

func (*ClientBuilder) WithUnaryInterceptors

func (b *ClientBuilder) WithUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) *ClientBuilder

WithUnaryInterceptors set a list of interceptors to the Grpc client for unary connection By default, gRPC doesn't allow one to have more than one interceptor either on the client nor on the server side. By using `grpc_middleware` we are able to provides convenient method to add a list of interceptors

type Config

type Config struct {
	ServiceName      string     // ServiceName is the name given that will show in the traces
	GrpcHost         string     // GrpcHost is the gRPC host
	GrpcPort         int        // GrpcPort is the gRPC port used to received and handle gRPC requests
	TraceEnabled     bool       // TraceEnabled checks whether tracing should be enabled or not
	TraceURL         string     // TraceURL is the OTLP collector url.
	EnableReflection bool       // EnableReflection this is useful or local dev testing
	Logger           log.Logger // Logger defines the logger
}

Config represent the grpc option

type ConnectionBuilder

type ConnectionBuilder interface {
	WithOptions(opts ...grpc.DialOption)
	WithInsecure()
	WithUnaryInterceptors(interceptors []grpc.UnaryClientInterceptor)
	WithStreamInterceptors(interceptors []grpc.StreamClientInterceptor)
	WithKeepAliveParams(params keepalive.ClientParameters)
	GetConn(ctx context.Context, addr string) (*grpc.ClientConn, error)
	GetTLSConn(ctx context.Context, addr string) (*grpc.ClientConn, error)
}

ConnectionBuilder is a builder to create GRPC connection to the GRPC Server

type InProcessServer

type InProcessServer interface {
	Start() error
	RegisterService(reg func(*grpc.Server))
	Cleanup()
	GetListener() *bufconn.Listener
}

InProcessServer server interface

type InProcessServerBuilder

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

InProcessServerBuilder in-processing grpc server builder

func NewInProcessServerBuilder

func NewInProcessServerBuilder() *InProcessServerBuilder

NewInProcessServerBuilder creates an instance of InProcessServerBuilder

func (*InProcessServerBuilder) Build

Build is responsible for building a Fiji GRPC server

func (*InProcessServerBuilder) WithOption

WithOption configures how we set up the connection.

func (*InProcessServerBuilder) WithStreamInterceptors

func (sb *InProcessServerBuilder) WithStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) *InProcessServerBuilder

WithStreamInterceptors set a list of interceptors to the Grpc server for stream connection By default, gRPC doesn't allow one to have more than one interceptor either on the client nor on the server side. By using `grpcMiddleware` we are able to provides convenient method to add a list of interceptors

func (*InProcessServerBuilder) WithTLSCert

WithTLSCert sets credentials for server connections

func (*InProcessServerBuilder) WithUnaryInterceptors

func (sb *InProcessServerBuilder) WithUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) *InProcessServerBuilder

WithUnaryInterceptors set a list of interceptors to the Grpc server for unary connection By default, gRPC doesn't allow one to have more than one interceptor either on the client nor on the server side. By using `grpcMiddleware` we are able to provides convenient method to add a list of interceptors

type Server

type Server interface {
	Start(ctx context.Context)
	Stop(ctx context.Context)
	AwaitTermination(ctx context.Context)
	GetListener() net.Listener
	GetServer() *grpc.Server
}

Server will be implemented by the grpcServer

type ServerBuilder

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

ServerBuilder helps build a grpc grpcServer

func GetServerBuilder

func GetServerBuilder(cfg *Config) *ServerBuilder

GetServerBuilder returns a grpcserver.ServerBuilder given a grpc config

func NewServerBuilder

func NewServerBuilder() *ServerBuilder

NewServerBuilder creates an instance of ServerBuilder

func (*ServerBuilder) Build

func (sb *ServerBuilder) Build() (Server, error)

Build is responsible for building a GRPC grpcServer

func (*ServerBuilder) WithDefaultKeepAlive

func (sb *ServerBuilder) WithDefaultKeepAlive() *ServerBuilder

WithDefaultKeepAlive is used to set the default keep alive parameters on the grpcServer-side

func (*ServerBuilder) WithDefaultStreamInterceptors

func (sb *ServerBuilder) WithDefaultStreamInterceptors() *ServerBuilder

WithDefaultStreamInterceptors sets the default stream interceptors for the grpc grpcServer

func (*ServerBuilder) WithDefaultUnaryInterceptors

func (sb *ServerBuilder) WithDefaultUnaryInterceptors() *ServerBuilder

WithDefaultUnaryInterceptors sets the default unary interceptors for the grpc grpcServer

func (*ServerBuilder) WithHealthCheck

func (sb *ServerBuilder) WithHealthCheck(enabled bool) *ServerBuilder

WithHealthCheck enables the default health check service

func (*ServerBuilder) WithHost

func (sb *ServerBuilder) WithHost(host string) *ServerBuilder

WithHost sets the grpc service host

func (*ServerBuilder) WithKeepAlive

func (sb *ServerBuilder) WithKeepAlive(serverParams keepalive.ServerParameters) *ServerBuilder

WithKeepAlive is used to set keepalive and max-age parameters on the grpcServer-side.

func (*ServerBuilder) WithLogger

func (sb *ServerBuilder) WithLogger(logger log.Logger) *ServerBuilder

WithLogger sets the logger

func (*ServerBuilder) WithMetricsEnabled

func (sb *ServerBuilder) WithMetricsEnabled(enabled bool) *ServerBuilder

WithMetricsEnabled enable grpc metrics

func (*ServerBuilder) WithOption

func (sb *ServerBuilder) WithOption(o grpc.ServerOption) *ServerBuilder

WithOption adds a grpc service option

func (*ServerBuilder) WithPort

func (sb *ServerBuilder) WithPort(port int) *ServerBuilder

WithPort sets the grpc service port

func (*ServerBuilder) WithReflection

func (sb *ServerBuilder) WithReflection(enabled bool) *ServerBuilder

WithReflection enables the reflection gRPC RunnableService Reflection provides information about publicly-accessible gRPC services on a grpcServer, and assists clients at runtime to construct RPC requests and responses without precompiled service information. It is used by gRPC CLI, which can be used to introspect grpcServer protos and send/receive test RPCs. Warning! We should not have this enabled in production

func (*ServerBuilder) WithService

func (sb *ServerBuilder) WithService(service serviceRegistry) *ServerBuilder

WithService registers service with gRPC grpcServer

func (*ServerBuilder) WithServiceName

func (sb *ServerBuilder) WithServiceName(serviceName string) *ServerBuilder

WithServiceName sets the service name

func (*ServerBuilder) WithShutdownHook

func (sb *ServerBuilder) WithShutdownHook(fn ShutdownHook) *ServerBuilder

WithShutdownHook sets the shutdown hook

func (*ServerBuilder) WithStreamInterceptors

func (sb *ServerBuilder) WithStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) *ServerBuilder

WithStreamInterceptors set a list of interceptors to the Grpc grpcServer for stream connection By default, gRPC doesn't allow one to have more than one interceptor either on the client nor on the grpcServer side. By using `grpcMiddleware` we are able to provides convenient method to add a list of interceptors

func (*ServerBuilder) WithTLSCert

func (sb *ServerBuilder) WithTLSCert(cert *tls.Certificate) *ServerBuilder

WithTLSCert sets credentials for grpcServer connections

func (*ServerBuilder) WithTraceURL

func (sb *ServerBuilder) WithTraceURL(traceURL string) *ServerBuilder

WithTraceURL sets the tracing URL

func (*ServerBuilder) WithTracingEnabled

func (sb *ServerBuilder) WithTracingEnabled(enabled bool) *ServerBuilder

WithTracingEnabled enables tracing

func (*ServerBuilder) WithUnaryInterceptors

func (sb *ServerBuilder) WithUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) *ServerBuilder

WithUnaryInterceptors set a list of interceptors to the Grpc grpcServer for unary connection By default, gRPC doesn't allow one to have more than one interceptor either on the client nor on the grpcServer side. By using `grpc_middleware` we are able to provides convenient method to add a list of interceptors

type ShutdownHook

type ShutdownHook func()

ShutdownHook is used to perform some cleaning before stopping the long-running grpcServer

type TraceProvider

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

TraceProvider is a wrapper around the open telemetry tracer.Provider It helps initialize an OTLP exporter, and configures the corresponding trace provider

func NewTraceProvider

func NewTraceProvider(exporterEndPoint, serviceName string) *TraceProvider

NewTraceProvider creates a new instance of TraceProvider

func (*TraceProvider) Deregister

func (p *TraceProvider) Deregister(ctx context.Context) error

Deregister will flush any remaining spans and shut down the exporter.

func (*TraceProvider) Register

func (p *TraceProvider) Register(ctx context.Context) error

Register initializes an OTLP exporter, and configures the corresponding trace provider

type XRequestIDKey

type XRequestIDKey struct{}

XRequestIDKey is used to store the x-request-id into the grpc context

Jump to

Keyboard shortcuts

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