grpcutil

package
v1.1.64 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: MIT Imports: 12 Imported by: 1

Documentation

Overview

Package grpcutil provides gRPC server implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetClientIP

func GetClientIP(ctx context.Context) net.IP

GetClientIP resolves the IP address (either v4 or v6) of the client. By default, function returns a remote address associated with the socket. In case the "x-forwarded-for" header is specified and parseable - the value of this header is returned.

func NewTokenCredentials

func NewTokenCredentials(token string) credentials.PerRPCCredentials

NewTokenCredentials creates new credentials.PerRPCCredentials instance, appending given token to gRPC call.

Types

type CallMetadata

type CallMetadata struct {
	// IP is an IP address of the client.
	IP net.IP

	// MethodName is the full name of the method.
	MethodName string
}

CallMetadata represents additional information about the gRPC call.

type DuplexStream

type DuplexStream[R any, S any] struct {
	// contains filtered or unexported fields
}

DuplexStream simplifies operation on bidirectional gRPC streams.

func NewDuplexStream

func NewDuplexStream[R any, S any](stream grpc.ServerStream, opts ...DuplexStreamOpt) *DuplexStream[R, S]

NewDuplexStream creates new DuplexStream.

func (*DuplexStream[R, S]) OnEnd

func (ds *DuplexStream[R, S]) OnEnd(handler func(reason error))

OnEnd specifies a handler for stream end event. The handler is called either on stream error or after you call Stop on given stream.

func (*DuplexStream[R, S]) OnReceive

func (ds *DuplexStream[R, S]) OnReceive(handler func(msg *R))

OnReceive specifies a handler for incoming messages. The function will call the handler for all incoming messages sequentially, using the same goroutine for each call.

func (*DuplexStream[R, S]) Send

func (ds *DuplexStream[R, S]) Send(msg *S)

Send sends a new message to the client.

func (*DuplexStream[R, S]) Start

func (ds *DuplexStream[R, S]) Start() (err error)

Start bootstraps goroutines responsible for handling receive and send channels and blocks until either the server (with Stop), or the client interrupts connection.

func (*DuplexStream[R, S]) Stop

func (ds *DuplexStream[R, S]) Stop()

Stop cancels goroutines responsible for handling receive and send channels and unblocks Start.

type DuplexStreamConfig

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

DuplexStreamConfig provides a configuration for DuplexStream.

type DuplexStreamOpt

type DuplexStreamOpt = func(config *DuplexStreamConfig)

DuplexStreamOpt is an option to be passed to NewDuplexStream.

func ReceiveChannelCapacity

func ReceiveChannelCapacity(capacity int64) DuplexStreamOpt

ReceiveChannelCapacity sets a total capacity (in number of messages) of receive channel's buffer.

func SendChannelCapacity

func SendChannelCapacity(capacity int64) DuplexStreamOpt

SendChannelCapacity sets a total capacity (in number of messages) of send channel's buffer.

type Server

type Server struct {
	*grpc.Server
	// contains filtered or unexported fields
}

Server is an object representing grpc.Server and implementing the lifecycle.Service interface.

func NewServer

func NewServer(opts ...ServerOpt) *Server

NewServer create new Server using global configuration and provided options.

func (*Server) Start

func (s *Server) Start() error

Start implements the interface of coreutil.Service.

func (*Server) Stop

func (s *Server) Stop()

Stop implements the interface of coreutil.Service.

type ServerOpt

type ServerOpt = func(*serverConfig)

ServerOpt is an option to be specified to NewServer.

func Address

func Address(address string) ServerOpt

Address is an address to bind to (default: "0.0.0.0:9000").

func EnableAuthMiddleware

func EnableAuthMiddleware[T any](verifier TokenVerifier[T]) ServerOpt

EnableAuthMiddleware makes server use token-based authorization based on passed TokenVerifier.

func EnableAuthMiddlewareFunc

func EnableAuthMiddlewareFunc[T any](verifierFunc TokenVerifierFunc[T]) ServerOpt

EnableAuthMiddlewareFunc makes server use token-based authorization based on passed TokenVerifierFunc.

func ServerOptions

func ServerOptions(opts ...grpc.ServerOption) ServerOpt

ServerOptions allows to specify custom grpc.ServerOption options.

func StreamInterceptor added in v1.1.1

func StreamInterceptor(interceptor grpc.StreamServerInterceptor) ServerOpt

StreamInterceptor adds specified interceptor to the tail of stream interceptors chains.

func UnaryInterceptor added in v1.1.1

func UnaryInterceptor(interceptor grpc.UnaryServerInterceptor) ServerOpt

UnaryInterceptor adds specified interceptor to the tail of unary interceptors chains.

type TokenVerificationResult

type TokenVerificationResult[T any] struct {
	// IsAuthorized defines whether call has been authorized by the middleware.
	IsAuthorized bool

	// SessionData carries additional information about the session, such as account data.
	// It can be set by TokenVerifierFunc.
	SessionData T
}

TokenVerificationResult represents a result of token verification procedure.

func GetTokenVerificationResult

func GetTokenVerificationResult[T any](ctx context.Context) *TokenVerificationResult[T]

GetTokenVerificationResult returns token verification result for a given call.

type TokenVerifier

type TokenVerifier[T any] interface {
	// Verify defines an application-specific procedure for token verification.
	Verify(token string, metadata *CallMetadata) (*TokenVerificationResult[T], error)
}

TokenVerifier defines an interface for application-specific procedure for token verification.

type TokenVerifierFunc

type TokenVerifierFunc[T any] func(token string, metadata *CallMetadata) (*TokenVerificationResult[T], error)

TokenVerifierFunc defines an application-specific procedure for token verification.

Jump to

Keyboard shortcuts

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