grpc

package
v0.0.0-...-eaa3807 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2024 License: MIT Imports: 32 Imported by: 3

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
)

Variables

This section is empty.

Functions

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 NewClientMetricStreamInterceptor

func NewClientMetricStreamInterceptor() grpc.StreamClientInterceptor

NewClientMetricStreamInterceptor creates a grpc client metric stream interceptor

func NewClientMetricUnaryInterceptor

func NewClientMetricUnaryInterceptor() grpc.UnaryClientInterceptor

NewClientMetricUnaryInterceptor creates a grpc client metric unary interceptor

func NewMetricStreamInterceptor

func NewMetricStreamInterceptor() grpc.StreamServerInterceptor

NewMetricStreamInterceptor returns a grpc metric stream interceptor

func NewMetricUnaryInterceptor

func NewMetricUnaryInterceptor() grpc.UnaryServerInterceptor

NewMetricUnaryInterceptor returns a grpc metric unary interceptor

func NewRateLimitStreamClientInterceptor

func NewRateLimitStreamClientInterceptor(rateLimiter Limiter) grpc.StreamClientInterceptor

NewRateLimitStreamClientInterceptor return stream client unary interceptor that limit requests.

func NewRateLimitStreamServerInterceptor

func NewRateLimitStreamServerInterceptor(rateLimiter Limiter) grpc.StreamServerInterceptor

NewRateLimitStreamServerInterceptor returns a new stream server interceptors that performs request rate limiting.

func NewRateLimitUnaryClientInterceptor

func NewRateLimitUnaryClientInterceptor(rateLimiter Limiter) grpc.UnaryClientInterceptor

NewRateLimitUnaryClientInterceptor return client unary interceptor that limit requests.

func NewRateLimitUnaryServerInterceptor

func NewRateLimitUnaryServerInterceptor(rateLimiter Limiter) grpc.UnaryServerInterceptor

NewRateLimitUnaryServerInterceptor returns a new unary server interceptors that performs request rate limiting.

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

func TestClientConn

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

TestClientConn creates an in-process grpc client

func TestServer

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

TestServer creates an in-process grpc server

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) ClientConn

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

ClientConn returns the client connection to the server

func (*ClientBuilder) TLSClientConn

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

TLSClientConn 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         int32  // 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
	MetricsEnabled   bool   // MetricsEnabled checks whether metrics should be enabled or not
	MetricsPort      int
}

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 Limiter

type Limiter interface {
	Check(ctx context.Context) bool
}

Limiter defines the interface to perform request rate limiting. If Check function return true, the request will be rejected. Otherwise, the request will pass.

type RateLimiter

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

RateLimiter implements Limiter interface.

func NewRateLimiter

func NewRateLimiter(requestCount int, limitPeriod time.Duration) *RateLimiter

NewRateLimiter return new go-grpc Limiter, specified the number of requests you want to limit as well as the limit period.

func (*RateLimiter) Check

func (l *RateLimiter) Check(ctx context.Context) bool

Check applies the rate limit

type Server

type Server interface {
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
	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 NewServerBuilder

func NewServerBuilder() *ServerBuilder

NewServerBuilder creates an instance of ServerBuilder

func NewServerBuilderFromConfig

func NewServerBuilderFromConfig(cfg *Config) *ServerBuilder

NewServerBuilderFromConfig returns a grpcserver.ServerBuilder given a grpc config

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) 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(ctx context.Context) error

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

Jump to

Keyboard shortcuts

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