micro

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvokeGRPCClientIP get grpc request client ip fail.
	ErrInvokeGRPCClientIP = errors.New("invoke from context failed")

	// ErrPeerAddressNil gRPC peer address is nil.
	ErrPeerAddressNil = errors.New("peer address is nil")
)

Functions

func GetCtxValue

func GetCtxValue(ctx context.Context, key CtxKey) interface{}

GetCtxValue returns ctx when you set key/value into ctx

func GetGRPCClientIP

func GetGRPCClientIP(ctx context.Context) (string, error)

GetGRPCClientIP get client ip address from context

func GetSliceFromMD

func GetSliceFromMD(md metadata.MD, key CtxKey) []string

GetSliceFromMD returns []string from md

func GetStringFromMD

func GetStringFromMD(md metadata.MD, key CtxKey) string

GetStringFromMD returns string from md

func IncomingMD

func IncomingMD(ctx context.Context) metadata.MD

IncomingMD returns metadata.MD from incoming ctx get request metadata this method is mainly used at the server end to get the relevant metadata data

func Md5

func Md5(str string) string

Md5 md5 func

func OutgoingMD

func OutgoingMD(ctx context.Context) metadata.MD

OutgoingMD returns metadata.MD from outgoing ctx Use this method when you pass ctx to a downstream service

func RandInt64

func RandInt64(min, max int64) int64

RandInt64 crete a num [m,n]

func RndUUID

func RndUUID() string

RndUUID realizes unique uuid based on time ns and random number There is no duplication of uuid on a single machine If you want to generate non-duplicate uuid on a distributed architecture Just add some custom strings in front of rndStr Return format: eba1e8cd-0460-4910-49c6-44bdf3cf024d

func RndUUIDMd5

func RndUUIDMd5() string

RndUUIDMd5 make an uuid

func SetCtxValue

func SetCtxValue(ctx context.Context, key CtxKey, val interface{}) context.Context

SetCtxValue returns ctx when you set key/value into ctx

func Uuid

func Uuid() string

Uuid uuid of version4 eg:eba1e8cd0460491049c644bdf3cf024d

Types

type CtxKey

type CtxKey string

CtxKey ctx key type.

const (
	// XRequestID request_id
	XRequestID CtxKey = "x-request-id"

	// ClientIP grpc client_ip
	ClientIP CtxKey = "client-ip"

	// RequestMethod request method
	RequestMethod CtxKey = "request_method"

	// RequestURI request uri
	RequestURI CtxKey = "request_uri"
)

func (CtxKey) String

func (c CtxKey) String() string

String returns string

type Logger

type Logger interface {
	Printf(string, ...interface{})
}

Logger is logger interface.

type LoggerFunc

type LoggerFunc func(string, ...interface{})

LoggerFunc is a bridge between Logger and any third party logger.

func (LoggerFunc) Printf

func (f LoggerFunc) Printf(msg string, args ...interface{})

Printf implements Logger interface.

type Option

type Option func(s *Service)

Option for grpc service option

func WithEnablePrometheus

func WithEnablePrometheus() Option

WithEnablePrometheus enable prometheus

func WithEnableRequestAccess

func WithEnableRequestAccess() Option

WithEnableRequestAccess request access log config

func WithEnableRequestValidator

func WithEnableRequestValidator() Option

WithEnableRequestValidator set request validator

func WithGRPCNetwork

func WithGRPCNetwork(network string) Option

WithGRPCNetwork set gRPC start network type

func WithGRPCServerOption

func WithGRPCServerOption(serverOption ...grpc.ServerOption) Option

WithGRPCServerOption returns an Option to append a gRPC server option

func WithInterruptSignals

func WithInterruptSignals(signal ...os.Signal) Option

WithInterruptSignals returns an Option to append a interrupt signal

func WithLogger

func WithLogger(logger Logger) Option

WithLogger uses the provided logger

func WithRecovery

func WithRecovery(f func()) Option

WithRecovery service recover func

func WithShutdownFunc

func WithShutdownFunc(f func()) Option

WithShutdownFunc returns an Option to register a function which will be called when server shutdown

func WithShutdownTimeout

func WithShutdownTimeout(timeout time.Duration) Option

WithShutdownTimeout returns an Option to set the timeout before the server shutdown abruptly

func WithStreamInterceptor

func WithStreamInterceptor(streamInterceptor ...grpc.StreamServerInterceptor) Option

WithStreamInterceptor returns an Option to append some streamInterceptor

func WithUnaryInterceptor

func WithUnaryInterceptor(unaryInterceptor ...grpc.UnaryServerInterceptor) Option

WithUnaryInterceptor returns an Option to append some unaryInterceptor

type Service

type Service struct {
	GRPCServer *grpc.Server // gRPC server
	// contains filtered or unexported fields
}

Service gRPC microservice struct

func NewService

func NewService(address string, opts ...Option) *Service

NewService create a grpc service instance

func (*Service) GetPid

func (s *Service) GetPid() int

GetPid gets the process id of server

func (*Service) Run

func (s *Service) Run() error

Run start gRPC service

Jump to

Keyboard shortcuts

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