process

package
v0.0.0-...-f683eec Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRouterKeyRepated = errors.New("router key repeated")
	ErrRouterNotSupport = errors.New("router not found")
	ErrNotFoundRequest  = errors.New("no request packet")
)

路由返回通用错误

Functions

func DefaultDataFilter

func DefaultDataFilter(data []byte, next DataDispatcherFunc) (err error)

DefaultPacketDispatcher default packet dispatch filter

func DefaultPacketFilter

func DefaultPacketFilter(pkg interface{}, next PacketDispatcherFunc) (err error)

DefaultPacketDispatcher default packet dispatch filter

func InstallInnerOptionsWatchDog

func InstallInnerOptionsWatchDog(dog func(cc *InnerOptions))

InstallInnerOptionsWatchDog install watch dog

func InstallProcessOptionsWatchDog

func InstallProcessOptionsWatchDog(dog func(cc *ProcessOptions))

InstallProcessOptionsWatchDog install watch dog

func SetRouter

func SetRouter(r Router)

Types

type AtomicNumber

type AtomicNumber interface {
	// Load atomically loads the wrapped value.
	Load() int64
	// Add atomically adds to the wrapped int64 and returns the new value.
	Add(n int64) int64
	// Sub atomically subtracts from the wrapped int64 and returns the new value.
	Sub(n int64) int64
	// Inc atomically increments the wrapped int64 and returns the new value.
	Inc() int64
	// Dec atomically decrements the wrapped int64 and returns the new value.
	Dec() int64
	// Store atomically stores the passed value.
	Store(n int64)
}

type Context

type Context interface {
	// WithValue wrap context.WithValue
	WithValue(key, value interface{}) Context
	// WithValue wrap context.WithCancel
	WithCancel() (_ Context, cancel func())
	// WithValue wrap context.WithDeadline
	WithDeadline(d time.Time) (_ Context, cancel func())
	// WithValue wrap context.WithTimeout
	WithTimeout(timeout time.Duration) (_ Context, cancel func())
	// Deadline wrap context.Context.Deadline
	Deadline() (deadline time.Time, ok bool)
	// Done wrap context.Context.Done
	Done() <-chan struct{}
	// Err wrap context.Context.Err
	Err() error
	// Value wrap context.Context.Value
	Value(key interface{}) interface{}
	// GetRequestPacket get request packet
	GetRequestPacket() interface{}
	// GetReqeustMD get request metadata
	GetReqeustMD() (metadata.MD, error)
	// Bind use for unmarshal packet body
	Bind(body interface{}) (err error)
	// Respond write response.
	Respond(_ context.Context, body interface{}, md metadata.MD) (err error)
	// Next call next middleware or router func
	Next(nctx Context)
	// Abort stop call next
	Abort()
	// Logger get logger
	Logger() *zaplog.Logger
	// WithLogFields
	WithLogFields(fields ...zap.Field)
	// NewEntry new log entry
	NewEntry(funcName string) *zaplog.LogEntities
}

Context 基础

type ContextPool

type ContextPool interface {
	NewContext(inner *InnerOptions, opts *ProcessOptions, inPkg interface{}, handlers []MiddlewareFunc, loadFlag bool) Context
	FreeContext(Context)
}
var WrapContextPool ContextPool = &wrapContextPool{
	Pool: sync.Pool{
		New: func() interface{} {
			return &WrapContext{}
		},
	},
}

type DataDispatcherFilter

type DataDispatcherFilter func(data []byte, next DataDispatcherFunc) (err error)

PacketUnmarshalFilter

func DataDispatcherChain

func DataDispatcherChain(filters ...DataDispatcherFilter) DataDispatcherFilter

type DataDispatcherFunc

type DataDispatcherFunc func(data []byte) (err error)

DataDispatcherFunc 消息分发 - 未解包

type InnerOption

type InnerOption func(cc *InnerOptions) InnerOption

InnerOption option define

func WithInnerOptionBindData

func WithInnerOptionBindData(v interface{}) InnerOption

bind data

func WithInnerOptionContextPool

func WithInnerOptionContextPool(v ContextPool) InnerOption

Specify Real Context

func WithInnerOptionLoad

func WithInnerOptionLoad(v AtomicNumber) InnerOption

load number interface

func WithInnerOptionOutput

func WithInnerOptionOutput(v io.Writer) InnerOption

Output: write interface(net.Conn)

func WithInnerOptionParentCtx

func WithInnerOptionParentCtx(v context.Context) InnerOption

process context parent

func WithInnerOptionRouter

func WithInnerOptionRouter(v Router) InnerOption

process router.

func WithInnerOptionSequence

func WithInnerOptionSequence(v AtomicNumber) InnerOption

Sequence number

type InnerOptions

type InnerOptions struct {
	// Output: write interface(net.Conn)
	Output io.Writer
	// Specify Real Context
	ContextPool ContextPool
	// process context parent
	ParentCtx context.Context
	// Sequence number
	Sequence AtomicNumber
	// load number interface
	Load AtomicNumber
	// bind data
	BindData interface{}
	// process router.
	Router Router
}

InnerOption use for process

func NewInnerOptions

func NewInnerOptions(opts ...InnerOption) *InnerOptions

NewInnerOptions create options instance.

func (*InnerOptions) ApplyOption

func (cc *InnerOptions) ApplyOption(opts ...InnerOption)

ApplyOption modify options

func (*InnerOptions) GetSetOption

func (cc *InnerOptions) GetSetOption(opt InnerOption) InnerOption

GetSetOption modify and get last option

func (*InnerOptions) SetOption

func (cc *InnerOptions) SetOption(opt InnerOption)

SetOption modify options

type MiddlewareFunc

type MiddlewareFunc func(Context)

MiddlewareFunc is middleware or router functions

type MixRouter

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

MixRouter 混合路由. 优先使用RequestID.(内部功能也使用RequestID) 路由必须先注册,再使用. 已经开始使用之后,路由不能再修改.(内部无锁)

func (*MixRouter) GetHandlers

func (r *MixRouter) GetHandlers(in interface{}) (handlers []RouterFunc, err error)

GetHandlers 获取请求对应处理函数

func (*MixRouter) NoRouter

func (r *MixRouter) NoRouter(rf RouterFunc, mid ...MiddlewareFunc) (err error)

NoRouter 未设置路由请求

func (*MixRouter) Register

func (r *MixRouter) Register(uri interface{}, rf RouterFunc, m ...MiddlewareFunc) (err error)

func (*MixRouter) Use

func (r *MixRouter) Use(m ...MiddlewareFunc)

Use 设置全局中间件

type PacketDispatcherFilter

type PacketDispatcherFilter func(pkg interface{}, next PacketDispatcherFunc) (err error)

PacketUnmarshalFilter

func PacketDispatcherChain

func PacketDispatcherChain(filters ...PacketDispatcherFilter) PacketDispatcherFilter

type PacketDispatcherFunc

type PacketDispatcherFunc func(pkg interface{}) (err error)

PacketDispatcherFunc 消息分发 - 未解包

type Process

type Process struct {
	// config
	Inner  *InnerOptions
	Opts   *ProcessOptions
	Filter ProcessFilter
	// contains filtered or unexported fields
}

Process 通用process 封装

func NewProcess

func NewProcess(inner *InnerOptions, opts *ProcessOptions) Process

func (*Process) OnRead

func (p *Process) OnRead(data []byte) (err error)

OnRead 入口函数。接收数据处理

type ProcessFilter

type ProcessFilter func(pkg interface{}) (filter bool)

type ProcessOption

type ProcessOption func(cc *ProcessOptions) ProcessOption

ProcessOption option define

func WithDispatchDataFilter

func WithDispatchDataFilter(v DataDispatcherFilter) ProcessOption

dispatch packet data filter

func WithDispatchPacketFilter

func WithDispatchPacketFilter(v PacketDispatcherFilter) ProcessOption

dispatch packet struct filter

func WithFrameLogger

func WithFrameLogger(v *zaplog.Logger) ProcessOption

frame log

func WithLoadLimitFilter

func WithLoadLimitFilter(v func(req interface{}, count AtomicNumber) bool) ProcessOption

load limit. return true to ignore packet.

func WithLogger

func WithLogger(v *zaplog.Logger) ProcessOption

log interface

func WithMsgCodec

func WithMsgCodec(v message.Codec) ProcessOption

message codec

func WithPacketCodec

func WithPacketCodec(v packet.Codec) ProcessOption

packet codec

func WithPacketEncode

func WithPacketEncode(v packet.Encoder) ProcessOption

packet encoder

func WithPacketPool

func WithPacketPool(v packet.Pool) ProcessOption

packet pool

func WithPacketWraper

func WithPacketWraper(v packet.ProtocolWraper) ProcessOption

packet wraper

type ProcessOptions

type ProcessOptions struct {
	// log interface
	Logger *zaplog.Logger
	// frame log
	FrameLogger *zaplog.Logger
	// packet pool
	PacketPool packet.Pool
	// packet wraper
	PacketWraper packet.ProtocolWraper
	// packet encoder
	PacketEncode packet.Encoder
	// packet codec
	PacketCodec packet.Codec
	// message codec
	MsgCodec message.Codec
	// dispatch packet data filter
	DispatchDataFilter DataDispatcherFilter
	// dispatch packet struct filter
	DispatchPacketFilter PacketDispatcherFilter
	// load limit. return true to ignore packet.
	LoadLimitFilter func(req interface{}, count AtomicNumber) bool
}

ProcessOption process option

func NewProcessOptions

func NewProcessOptions(opts ...ProcessOption) *ProcessOptions

NewProcessOptions create options instance.

func (*ProcessOptions) ApplyOption

func (cc *ProcessOptions) ApplyOption(opts ...ProcessOption)

ApplyOption modify options

func (*ProcessOptions) GetSetOption

func (cc *ProcessOptions) GetSetOption(opt ProcessOption) ProcessOption

GetSetOption modify and get last option

func (*ProcessOptions) SetOption

func (cc *ProcessOptions) SetOption(opt ProcessOption)

SetOption modify options

type Processer

type Processer interface {
	// OnRead 入口函数。接收数据处理
	OnRead(data []byte) (err error)
}

Process 通用process 封装

type Router

type Router interface {
	// Use 设置中间件,在Use之后注册的接口都会使用此中间件
	Use(m ...MiddlewareFunc)
	// NoRouter 未注册路由的默认处理函数
	NoRouter(rf RouterFunc, mid ...MiddlewareFunc) (err error)
	// Register 注册路由
	Register(uri interface{}, rf RouterFunc, m ...MiddlewareFunc) (err error)
	// GetHandlers 获取处理函数接口
	GetHandlers(p interface{}) (handlers []RouterFunc, err error)
}

Router 路由接口

func GetRouter

func GetRouter() Router

type RouterFunc

type RouterFunc = MiddlewareFunc

RouterFunc router func is MiddlewareFunc

type WrapContext

type WrapContext struct {
	// config
	Inner *InnerOptions
	Opts  *ProcessOptions
	// SrcContext Context parent is link Context
	SrcContext context.Context
	// call chain
	Index    int
	Handlers []MiddlewareFunc
	// input packet
	InPkg    interface{}
	LoadFlag bool
	// Log context
	Log *zaplog.Logger
	// use to free context
	FreeContext Context
}

Context 基础

func (*WrapContext) Abort

func (ctx *WrapContext) Abort()

Abort stop call next

func (*WrapContext) Bind

func (ctx *WrapContext) Bind(body interface{}) (err error)

Bind use for unmarshal packet body

func (*WrapContext) Deadline

func (ctx *WrapContext) Deadline() (deadline time.Time, ok bool)

Deadline wrap context.Context.Deadline

func (*WrapContext) Done

func (ctx *WrapContext) Done() <-chan struct{}

Done wrap context.Context.Done

func (*WrapContext) Err

func (ctx *WrapContext) Err() error

Err wrap context.Context.Err

func (*WrapContext) GetReqeustMD

func (ctx *WrapContext) GetReqeustMD() (metadata.MD, error)

GetReqeustMD get request metadata

func (*WrapContext) GetRequestPacket

func (ctx *WrapContext) GetRequestPacket() interface{}

GetRequestPacket get request packet

func (*WrapContext) Logger

func (ctx *WrapContext) Logger() *zaplog.Logger

Logger get logger

func (*WrapContext) NewEntry

func (ctx *WrapContext) NewEntry(funcName string) *zaplog.LogEntities

NewEntry new log entry

func (*WrapContext) Next

func (ctx *WrapContext) Next(nctx Context)

Next call next middleware or router func

func (*WrapContext) Respond

func (ctx *WrapContext) Respond(_ context.Context, body interface{}, md metadata.MD) (err error)

Respond write response.

func (*WrapContext) Value

func (ctx *WrapContext) Value(key interface{}) interface{}

Value wrap context.Context.Value

func (*WrapContext) WithCancel

func (ctx *WrapContext) WithCancel() (_ Context, cancel func())

WithValue wrap context.WithCancel

func (*WrapContext) WithDeadline

func (ctx *WrapContext) WithDeadline(d time.Time) (_ Context, cancel func())

WithValue wrap context.WithDeadline

func (*WrapContext) WithLogFields

func (ctx *WrapContext) WithLogFields(fields ...zap.Field)

WithLogFields

func (*WrapContext) WithTimeout

func (ctx *WrapContext) WithTimeout(timeout time.Duration) (_ Context, cancel func())

WithValue wrap context.WithTimeout

func (*WrapContext) WithValue

func (ctx *WrapContext) WithValue(key, value interface{}) Context

WithValue wrap context.WithValue

Directories

Path Synopsis
Package metadata define the structure of the metadata supported by gRPC library.
Package metadata define the structure of the metadata supported by gRPC library.

Jump to

Keyboard shortcuts

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