grpc

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: Apache-2.0 Imports: 22 Imported by: 2

Documentation

Overview

Package grpc implements a set of utilities around gRPC + Protobuf to provide low-latency API-contract-driven development.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHeader

func GetHeader(ctx context.Context, header string) ([]string, error)

GetHeader Retrieves a HTTP header key, if available.

func GetHeaderFromContext

func GetHeaderFromContext(ctx context.Context, key string) []string

GetHeaderFromContext retrieves the HTTP header from the context's gRPC metadata.

func GetHeaders

func GetHeaders(ctx context.Context, headers ...string) (map[string][]string, error)

GetHeaders Retrieves all HTTP header keys as a map.

Types

type Client

type Client struct {
	*grpc.ClientConn
}

Client wraps up *grpc.ClientConn.

func NewClient

func NewClient(cfg *ClientConfig, logger *logger.Logger, tracerProvider *tracer.Provider) (*Client, error)

NewClient initialises a GRPC client.

type ClientConfig

type ClientConfig struct {
	// Address is the TCP address to connect to.
	Address string

	// KeepAlive indicates how the GRPC client should configure the connection's keep alive.
	KeepAlive struct {
		// 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.
		ClientParameters struct {
			// If true, client sends keepalive pings even with no active RPCs. If false, when there are
			// no active RPCs, Time and Timeout will be ignored and no keepalive pings will be sent.
			// By default, it is false.
			PermitWithoutStream bool

			// After a duration of this time if the client doesn't see any activity it pings the server
			// to see if the transport is still alive. If set below 10s, a minimum value of 10s will be
			// used instead. By default, it is 10 * time.Second.
			Time time.Duration

			// After having pinged for keepalive check, the client waits for a duration of Timeout and
			// if no activity is seen even after that the connection is closed. By default, it is
			// 20 * time.Second.
			Timeout time.Duration
		}
	}
}

ClientConfig indicates how a GRPC client should be initialised.

type Server

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

Server wraps up *grpc.Server.

func NewServer

func NewServer(c *ServerConfig, logger *logger.Logger, preStartCallback func() error, interceptors ...grpc.UnaryServerInterceptor) (*Server, error)

NewServer initialises a gRPC server.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the server's network address.

func (*Server) GRPCGatewayClients

func (s *Server) GRPCGatewayClients() []string

GRPCGatewayClients indicates the services that the server is proxying via `grpc-gateway`. As a gRPC server isn't meant to proxy any HTTP request, this function is only here to satisfy the `pack.Server` interface.

func (*Server) GRPCGatewayServer

func (s *Server) GRPCGatewayServer() string

GRPCGatewayServer indicates the `grpc-gateway` server that the gRPC service is connected to.

func (*Server) GRPCServer

func (s *Server) GRPCServer() *grpc.Server

GRPCServer returns the internal gRPC server instance.

func (*Server) GracefulShutdownHandler

func (s *Server) GracefulShutdownHandler() error

GracefulShutdownHandler is a function that runs before the gRPC server is gracefully shut down.

func (*Server) GracefulStop

func (s *Server) GracefulStop() error

GracefulStop stops the gRPC server gracefully. It stops the server from accepting new connections and RPCs and blocks until all the pending RPCs are finished.

func (*Server) PreStartCallback

func (s *Server) PreStartCallback() func() error

PreStartCallback is the callback function to trigger right before the server starts running.

func (*Server) Serve

func (s *Server) Serve() error

Serve accepts incoming connections on the listener lis, creating a new ServerTransport and service goroutine for each. The service goroutines read gRPC requests and then call the registered handlers to reply to them. Serve returns when lis.Accept fails with fatal errors. lis will be closed when this method returns. Serve will return a non-nil error unless Stop or GracefulStop is called.

func (*Server) TracerProviderShutdownHandler

func (s *Server) TracerProviderShutdownHandler() error

TracerProviderShutdownHandler is a function that shuts down the tracer's exporter/provider before the gRPC server is gracefully shut down.

func (*Server) Type

func (s *Server) Type() string

Type indicates if the server is gRPC server.

type ServerConfig

type ServerConfig struct {
	// Address is the TCP address to listen on.
	Address string

	// GracefulShutdownHandler is a function that runs before the gRPC server is gracefully shut down.
	GracefulShutdownHandler func() error

	// GRPCGatewayServer indicates the `grpc-gateway` server that the service is connected to.
	GRPCGatewayServer string

	// KeepAlive indicates how the gRPC server should configure the connection's keep alive.
	KeepAlive struct {
		// EnforcementPolicy is used to set keepalive enforcement policy on the server-side. Server
		// will close connection with a client that violates this policy.
		EnforcementPolicy struct {
			// MinTime is the minimum amount of time a client should wait before sending a keepalive
			// ping. By default, it is 5 * time.Second.
			MinTime time.Duration

			// If true, server allows keepalive pings even when there are no active streams(RPCs). If
			// false, and client sends ping when there are no active streams, server will send GOAWAY
			// and close the connection. By default, it is false.
			PermitWithoutStream bool
		}

		// ServerParameters is used to set keepalive and max-age parameters on the server-side.
		ServerParameters struct {
			// MaxConnectionAge is a duration for the maximum amount of time a connection may exist
			// before it will be closed by sending a GoAway. A random jitter of +/-10% will be added
			// to MaxConnectionAge to spread out connection storms.
			MaxConnectionAge time.Duration

			// MaxConnectionIdle is a duration for the amount of time after which an idle connection
			// would be closed by sending a GoAway. Idleness duration is defined since the most recent
			// time the number of outstanding RPCs became zero or the connection establishment.
			MaxConnectionIdle time.Duration

			// After a duration of this time if the server doesn't see any activity it pings the client
			// to see if the transport is still alive. If set below 1s, a minimum value of 1s will be
			// used instead.
			Time time.Duration

			// After having pinged for keepalive check, the server waits for a duration of Timeout and
			// if no activity is seen even after that the connection is closed.
			Timeout time.Duration
		}
	}

	// ReflectionService indicates if the gRPC server should register the reflection service.
	ReflectionService bool

	// TracerProvider is the provider that uses the exporter to push traces to the collector.
	TracerProvider *tracer.Provider

	// TracerProviderShutdownHandler is a function that shuts down the tracer's exporter/provider before
	// the gRPC server is gracefully shut down.
	TracerProviderShutdownHandler func() error
}

ServerConfig indicates how a gRPC server should be initialised.

Jump to

Keyboard shortcuts

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