grpc

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package grpc provides a gRPC server and client preconfigured with middleware.

Interceptors handle request-context propagation through metadata, optional JWT authentication, panic recovery and tracing. The server also registers the standard gRPC health service. AppError is mapped to the appropriate gRPC status.

Index

Constants

View Source
const (
	Headers    = "headers"
	AuthHeader = "access-token"
)
View Source
const (
	ErrCodeGrpcClientDial     = "GRPC-001"
	ErrCodeGrpcInvoke         = "GRPC-002"
	ErrCodeGrpcSrvListen      = "GRPC-003"
	ErrCodeGrpcSrvServe       = "GRPC-004"
	ErrCodeGrpcSrvNotReady    = "GRPC-005"
	ErrCodeGrpcClientError    = "GRPC-006"
	ErrCodeGrpcAuthNoMd       = "GRPC-007"
	ErrCodeGrpcAuthNoHeader   = "GRPC-008"
	ErrCodeGrpcAuthParseToken = "GRPC-009"
)
View Source
const (
	TestService_WithError_FullMethodName = "/grpc.TestService/WithError"
	TestService_WithPanic_FullMethodName = "/grpc.TestService/WithPanic"
	TestService_Do_FullMethodName        = "/grpc.TestService/Do"
)

Variables

View Source
var (
	ErrGrpcClientDial = func(cause error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcClientDial, "").Wrap(cause).Err()
	}
	ErrGrpcInvoke = func(cause error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcInvoke, "").Wrap(cause).Err()
	}
	ErrGrpcSrvListen = func(cause error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcSrvListen, "").Wrap(cause).Err()
	}
	ErrGrpcSrvServe = func(cause error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcSrvServe, "").Wrap(cause).Err()
	}
	ErrGrpcSrvNotReady = func(svc string) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcSrvNotReady, "service isn't ready within timeout").F(jet.KV{"svc": svc}).Err()
	}
	ErrGrpcClientError = func(ctx context.Context, cause error, method string) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcClientError, "grpc client error").F(jet.KV{"method": method}).Wrap(cause).Err()
	}
	ErrGrpcAuthNoMd = func(ctx context.Context) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcAuthNoMd, "no metadata").GrpcSt(uint32(codes.Unauthenticated)).Err()
	}
	ErrGrpcAuthNoHeader = func(ctx context.Context) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcAuthNoHeader, "no header").GrpcSt(uint32(codes.Unauthenticated)).Err()
	}
	ErrGrpcAuthParseToken = func(ctx context.Context, cause error) error {
		return jet.NewAppErrBuilder(ErrCodeGrpcAuthParseToken, "no header").Wrap(cause).GrpcSt(uint32(codes.Unauthenticated)).Err()
	}
)
View Source
var File_grpc_errors_proto protoreflect.FileDescriptor
View Source
var File_grpc_tests_proto protoreflect.FileDescriptor
View Source
var TestService_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "grpc.TestService",
	HandlerType: (*TestServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "WithError",
			Handler:    _TestService_WithError_Handler,
		},
		{
			MethodName: "WithPanic",
			Handler:    _TestService_WithPanic_Handler,
		},
		{
			MethodName: "Do",
			Handler:    _TestService_Do_Handler,
		},
	},
	Streams:  []grpc.StreamDesc{},
	Metadata: "grpc/tests.proto",
}

TestService_ServiceDesc is the grpc.ServiceDesc for TestService service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)

Functions

func RegisterTestServiceServer

func RegisterTestServiceServer(s grpc.ServiceRegistrar, srv TestServiceServer)

func ToAppError

func ToAppError(ctx context.Context, method string, err error) error

ToAppError converts gRPC status to AppError

func ToTime

func ToTime(ts *timestamppb.Timestamp) *time.Time

ToTime converts GRPC timestamp to time

func ToTimestamp

func ToTimestamp(t *time.Time) *timestamppb.Timestamp

ToTimestamp converts time to GRPC timestamp

Types

type AppErrorDetails

type AppErrorDetails struct {
	Code   string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"`
	Fields []byte `protobuf:"bytes,2,opt,name=fields,proto3" json:"fields,omitempty"`
	Type   string `protobuf:"bytes,3,opt,name=type,proto3" json:"type,omitempty"`
	// contains filtered or unexported fields
}

func (*AppErrorDetails) Descriptor deprecated

func (*AppErrorDetails) Descriptor() ([]byte, []int)

Deprecated: Use AppErrorDetails.ProtoReflect.Descriptor instead.

func (*AppErrorDetails) GetCode

func (x *AppErrorDetails) GetCode() string

func (*AppErrorDetails) GetFields

func (x *AppErrorDetails) GetFields() []byte

func (*AppErrorDetails) GetType

func (x *AppErrorDetails) GetType() string

func (*AppErrorDetails) ProtoMessage

func (*AppErrorDetails) ProtoMessage()

func (*AppErrorDetails) ProtoReflect

func (x *AppErrorDetails) ProtoReflect() protoreflect.Message

func (*AppErrorDetails) Reset

func (x *AppErrorDetails) Reset()

func (*AppErrorDetails) String

func (x *AppErrorDetails) String() string

type Client

type Client struct {
	Conn *grpc.ClientConn
	// contains filtered or unexported fields
}

Client grpc client

func NewClient

func NewClient(cfg *ClientConfig) (*Client, error)

func (*Client) AwaitReadiness

func (r *Client) AwaitReadiness(timeout time.Duration) bool

type ClientAuthConfig

type ClientAuthConfig struct {
	Enabled     bool   `mapstructure:"enabled"`      // Enabled if true auth header is passed with each call
	TokenSecret string `mapstructure:"token_secret"` // TokenSecret secret to generate token
	TokenTTL    int    `mapstructure:"token_ttl"`    // TokenTTL token time to live
	Caller      string `mapstructure:"caller"`       // Caller name
}

ClientAuthConfig client auth configuration

type ClientConfig

type ClientConfig struct {
	Host string
	Port string
	Auth ClientAuthConfig
}

ClientConfig is gRPC client configuration

type Empty

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

func (*Empty) Descriptor deprecated

func (*Empty) Descriptor() ([]byte, []int)

Deprecated: Use Empty.ProtoReflect.Descriptor instead.

func (*Empty) ProtoMessage

func (*Empty) ProtoMessage()

func (*Empty) ProtoReflect

func (x *Empty) ProtoReflect() protoreflect.Message

func (*Empty) Reset

func (x *Empty) Reset()

func (*Empty) String

func (x *Empty) String() string

type HealthServer

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

HealthServer implements `service Health`.

func NewHealthServer

func NewHealthServer() *HealthServer

NewHealthServer creates a new health server

func (*HealthServer) Check

Check implements `service Health`.

func (*HealthServer) SetServingStatus

func (s *HealthServer) SetServingStatus(service string, status health.HealthCheckResponse_ServingStatus)

SetServingStatus is called when need to reset the serving status of a service or insert a new service entry into the statusMap.

func (*HealthServer) Watch

type Server

type Server struct {
	healthpb.HealthServer
	monitoring.MetricsProvider
	Srv     *grpc.Server
	Service string
	// contains filtered or unexported fields
}

func NewServer

func NewServer(service string, logger jet.CLoggerFunc, config *ServerConfig) (*Server, error)

func (*Server) Close

func (s *Server) Close()

func (*Server) Listen

func (s *Server) Listen(ctx context.Context) error

func (*Server) ListenAsync

func (s *Server) ListenAsync(ctx context.Context)

func (*Server) PanicFunc

func (s *Server) PanicFunc(ctx context.Context, panicCause interface{}) error

type ServerAuthConfig

type ServerAuthConfig struct {
	Enabled bool   // Enabled if true auth is applied
	Secret  string // Secret key
}

ServerAuthConfig authorization config

type ServerConfig

type ServerConfig struct {
	Host  string
	Port  string
	Trace bool
	Auth  ServerAuthConfig
}

ServerConfig represents gRPC server configuration

type TestServiceClient

type TestServiceClient interface {
	WithError(ctx context.Context, in *WithErrorRequest, opts ...grpc.CallOption) (*WithErrorResponse, error)
	WithPanic(ctx context.Context, in *WithPanicRequest, opts ...grpc.CallOption) (*WithPanicResponse, error)
	Do(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*Empty, error)
}

TestServiceClient is the client API for TestService service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.

type TestServiceServer

type TestServiceServer interface {
	WithError(context.Context, *WithErrorRequest) (*WithErrorResponse, error)
	WithPanic(context.Context, *WithPanicRequest) (*WithPanicResponse, error)
	Do(context.Context, *Empty) (*Empty, error)
	// contains filtered or unexported methods
}

TestServiceServer is the server API for TestService service. All implementations must embed UnimplementedTestServiceServer for forward compatibility

type UnimplementedTestServiceServer

type UnimplementedTestServiceServer struct {
}

UnimplementedTestServiceServer must be embedded to have forward compatible implementations.

func (UnimplementedTestServiceServer) Do

func (UnimplementedTestServiceServer) WithError

func (UnimplementedTestServiceServer) WithPanic

type UnsafeTestServiceServer

type UnsafeTestServiceServer interface {
	// contains filtered or unexported methods
}

UnsafeTestServiceServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to TestServiceServer will result in compilation errors.

type WithErrorRequest

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

func (*WithErrorRequest) Descriptor deprecated

func (*WithErrorRequest) Descriptor() ([]byte, []int)

Deprecated: Use WithErrorRequest.ProtoReflect.Descriptor instead.

func (*WithErrorRequest) ProtoMessage

func (*WithErrorRequest) ProtoMessage()

func (*WithErrorRequest) ProtoReflect

func (x *WithErrorRequest) ProtoReflect() protoreflect.Message

func (*WithErrorRequest) Reset

func (x *WithErrorRequest) Reset()

func (*WithErrorRequest) String

func (x *WithErrorRequest) String() string

type WithErrorResponse

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

func (*WithErrorResponse) Descriptor deprecated

func (*WithErrorResponse) Descriptor() ([]byte, []int)

Deprecated: Use WithErrorResponse.ProtoReflect.Descriptor instead.

func (*WithErrorResponse) ProtoMessage

func (*WithErrorResponse) ProtoMessage()

func (*WithErrorResponse) ProtoReflect

func (x *WithErrorResponse) ProtoReflect() protoreflect.Message

func (*WithErrorResponse) Reset

func (x *WithErrorResponse) Reset()

func (*WithErrorResponse) String

func (x *WithErrorResponse) String() string

type WithPanicRequest

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

func (*WithPanicRequest) Descriptor deprecated

func (*WithPanicRequest) Descriptor() ([]byte, []int)

Deprecated: Use WithPanicRequest.ProtoReflect.Descriptor instead.

func (*WithPanicRequest) ProtoMessage

func (*WithPanicRequest) ProtoMessage()

func (*WithPanicRequest) ProtoReflect

func (x *WithPanicRequest) ProtoReflect() protoreflect.Message

func (*WithPanicRequest) Reset

func (x *WithPanicRequest) Reset()

func (*WithPanicRequest) String

func (x *WithPanicRequest) String() string

type WithPanicResponse

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

func (*WithPanicResponse) Descriptor deprecated

func (*WithPanicResponse) Descriptor() ([]byte, []int)

Deprecated: Use WithPanicResponse.ProtoReflect.Descriptor instead.

func (*WithPanicResponse) ProtoMessage

func (*WithPanicResponse) ProtoMessage()

func (*WithPanicResponse) ProtoReflect

func (x *WithPanicResponse) ProtoReflect() protoreflect.Message

func (*WithPanicResponse) Reset

func (x *WithPanicResponse) Reset()

func (*WithPanicResponse) String

func (x *WithPanicResponse) String() string

Jump to

Keyboard shortcuts

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