infector

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2022 License: MIT Imports: 11 Imported by: 0

README

go-infector

🌈 control timeout and retry in distributed microservice !!!

feature:

  • easy api
  • gin interceptor
  • grpc interceptor
  • http middleware
  • redis hook

Usage

Please review the code.

Example

request trace:

client -> proxy -> user

🔥 easy full example 🔥

Documentation

Index

Constants

View Source
const (
	RetryUnknown string = "unknown"
	RetryOn      string = "on"
	RetryOff     string = "off"
)

Variables

View Source
var (
	ErrInvalidTimeout  = errors.New("invalid timeout")
	ErrInvalidDeadline = errors.New("invalid deadline")
	ErrInvalidRetry    = errors.New("invalid retry")
)
View Source
var (
	ErrHeaderRequestTimeout = errors.New("the timeout-ms value in header is 0, not enough time.")
	ErrRequestTimeout       = errors.New("request timeout.")
)

Functions

func GinMiddleware

func GinMiddleware(opts ...optionGinFunc) gin.HandlerFunc

GinMiddleware gin middleware

func GrpcServerInterceptor

func GrpcServerInterceptor(opts ...optionGrpcUnaryFunc) grpc.UnaryServerInterceptor

GrpcServerInterceptor grpc server wrapper

func HttpMiddleware

func HttpMiddleware(next http.Handler) http.Handler

HttpMiddleware

func InjectHeaderCtx

func InjectHeaderCtx(ctx context.Context, _header interface{}, retry string)

func NewRedisHook

func NewRedisHook() redis.Hook

func ParseHeader

func ParseHeader(hdr interface{}) (time.Duration, string, error)

ParseHeader

func SetDefaultGrpcOption

func SetDefaultGrpcOption(dop GrpcUnaryOption)

SetDefaultGrpcOption

func SetDefaultHttpOption

func SetDefaultHttpOption(ho HttpOption)

SetDefaultHttpOption the function call before app register middleware.

func SetLogger

func SetLogger(logger Logger)

func SetPrefixKey

func SetPrefixKey(pre string)

func WithGinLeastQuota

func WithGinLeastQuota(quota time.Duration) optionGinFunc

func WithGinResponse

func WithGinResponse(header http.Header, obj interface{}) optionGinFunc

func WithGrpcLeastQuota

func WithGrpcLeastQuota(quota time.Duration) optionGrpcUnaryFunc

func WithGrpcResponse

func WithGrpcResponse(header http.Header, obj interface{}) optionGrpcUnaryFunc

Types

type Entry

type Entry struct {
	Timeout  time.Duration
	Deadline time.Time
	Retry    string
}

Entry

func ParseEntry

func ParseEntry(ctx context.Context, header interface{}) (*Entry, error)

ParseEntry

type GrpcUnaryOption

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

type HttpOption

type HttpOption struct {
	LeastQuota time.Duration
	// contains filtered or unexported fields
}

type Logger

type Logger interface {
	Error(string)
}

logger

type Mapper

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

Mapper match with http.Header, grpc metadata.md, map[string]string, map[string]interface{}

func WrapMapper

func WrapMapper(container interface{}) Mapper

WrapMapper

func (*Mapper) Get

func (ma *Mapper) Get(k string) string

Get get value by key

func (*Mapper) Set

func (ma *Mapper) Set(k, v string)

Set set value by k,v

type NullLogger

type NullLogger struct{}

func (*NullLogger) Error

func (l *NullLogger) Error(msg string)

type NullSpanContext

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

func (*NullSpanContext) Cancel

func (sc *NullSpanContext) Cancel() bool

func (*NullSpanContext) GetGrpcMetadata

func (sc *NullSpanContext) GetGrpcMetadata(mds ...metadata.MD) metadata.MD

func (*NullSpanContext) GetHttpMetadata

func (sc *NullSpanContext) GetHttpMetadata(hdrs ...http.Header) http.Header

func (*NullSpanContext) NotTimeout

func (sc *NullSpanContext) NotTimeout() bool

func (*NullSpanContext) ReachTimeout

func (sc *NullSpanContext) ReachTimeout() bool

type RedisHook

type RedisHook struct{}

func (RedisHook) AfterProcess

func (hook RedisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error

func (RedisHook) AfterProcessPipeline

func (hook RedisHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error

func (RedisHook) BeforeProcess

func (hook RedisHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error)

func (RedisHook) BeforeProcessPipeline

func (hook RedisHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error)

type SpanContext

type SpanContext struct {
	TimeExists bool
	Deadline   time.Time
	Timeout    time.Duration

	RetryFlag string
	// contains filtered or unexported fields
}

SpanContext

func NewSpanContext

func NewSpanContext(ctx context.Context, timeout time.Duration, retry string) *SpanContext

NewSpanContext

func ParseSpanFromCtx

func ParseSpanFromCtx(ctx context.Context) (*SpanContext, error)

ParseSpanFromCtx

func ParseSpanFromHeader

func ParseSpanFromHeader(ctx context.Context, header interface{}) (*SpanContext, error)

ParseSpanFromHeader header type is in the range of http.header, grpc.metadata and map.

func (*SpanContext) Cancel

func (sc *SpanContext) Cancel()

cancel

func (*SpanContext) ContinueRetry

func (sc *SpanContext) ContinueRetry() bool

ContinueRetry

func (*SpanContext) GetCancel

func (sc *SpanContext) GetCancel() context.CancelFunc

GetCancel

func (*SpanContext) GetContext

func (sc *SpanContext) GetContext() context.Context

GetContext

func (*SpanContext) GetContextCancel

func (sc *SpanContext) GetContextCancel() (context.Context, context.CancelFunc)

GetContextCancel

func (*SpanContext) GetEntry

func (sc *SpanContext) GetEntry() *Entry

GetEntry get entry structure

func (*SpanContext) GetGrpcMetadata

func (sc *SpanContext) GetGrpcMetadata(mds ...metadata.MD) metadata.MD

GetGrpcMetadata inject infector'args to header, then return the header.

func (*SpanContext) GetHttpHeader

func (sc *SpanContext) GetHttpHeader(hdrs ...http.Header) http.Header

GetHttpHeader inject infector'args to header, then return the header. if don't input custom header, new header.

func (*SpanContext) InjectHeader

func (sc *SpanContext) InjectHeader(header interface{})

InjectHeader

func (*SpanContext) IsRetryON

func (sc *SpanContext) IsRetryON() bool

IsRetryON

func (*SpanContext) IsRetryOff

func (sc *SpanContext) IsRetryOff() bool

IsRetryOff

func (*SpanContext) IsRetryUnknown

func (sc *SpanContext) IsRetryUnknown() bool

IsRetryUnknown

func (*SpanContext) NotTimeout

func (sc *SpanContext) NotTimeout() bool

NotTimeout

func (*SpanContext) PromiseLeastQuota

func (sc *SpanContext) PromiseLeastQuota(quota time.Duration) bool

PromiseLeastQuota

func (*SpanContext) ReachTimeout

func (sc *SpanContext) ReachTimeout() bool

ReachTimeout

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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